inspiredd
inspiredd

Reputation: 217

convert local timestamp to UTC python

I need to create new pandas column based on timezone_x column :

df4['column'] = df4['local'].dt.tz_localize(df4['timezone_x']).dt.tz_convert('utc')

but I have an error :

TypeError: <class 'pandas.core.series.Series'>

Upvotes: 0

Views: 80

Answers (1)

LockeErasmus
LockeErasmus

Reputation: 116

I made some assumptions about your problem (poorly asked question), but apply is propably what you want to use.

df["column"] = pd.to_datetime(df[["local", "timezone_x"]].apply(lambda row: row['local'].tz_localize(row['timezone_x']), axis=1), utc=True)

Whole code I tried for basically making up your problem:

import pandas as pd
df = pd.DataFrame(
    {"local": ["2020-05-07T10:20:20", "2001-05-13T07:30:35", "1999-12-08"],
     "timezone_x": ["US/Eastern", "Europe/Vienna", "America/New_York"]}
)
df["local"] = pd.to_datetime(df["local"])
df["column"] = pd.to_datetime(df[["local", "timezone_x"]].apply(lambda row: row['local'].tz_localize(row['timezone_x']), axis=1), utc=True)

python: 3.5.5 Anaconda, pandas: 0.23.3

Upvotes: 1

Related Questions