I have a data frame with multiple users and timezones, like such:
cols = ['user', 'zone_name', 'utc_datetime']
data = [
[1, 'Europe/Amsterdam', pd.to_datetime('2019-11-13 11:14:15')],
[2, 'Europe/London', pd.to_datetime('2019-11-13 11:14:15')],
]
df = pd.DataFrame(data, columns=cols)
Based on this other post, I apply the following change to get the user local datetime:
df['local_datetime'] = df.groupby('zone_name')[
'utc_datetime'
].transform(lambda x: x.dt.tz_localize(x.name))
Which outputs this:
user zone_name utc_datetime local_datetime
1 Europe/Amsterdam 2019-11-13 11:14:15 2019-11-13 11:14:15+01:00
2 Europe/London 2019-11-13 11:14:15 2019-11-13 11:14:15+00:00
However, the local_datetime
column is an object
and I cannot find a way to get it as datetime64[ns]
and in the following format (desired output):
user zone_name utc_datetime local_datetime
1 Europe/Amsterdam 2019-11-13 11:14:15 2019-11-13 12:14:15
2 Europe/London 2019-11-13 11:14:15 2019-11-13 11:14:15