I am kind of completely lost...
I have instanced a panda dataframe in python with read_csv() function. I had to extract in a list the column containing timestamps and make some cleaning. This list now looks like:
0 Sat Mar 30 2019 21:00:00 GMT+0100
1 Sat Mar 30 2019 22:00:00 GMT+0100
2 Sat Mar 30 2019 23:00:00 GMT+0100
...
I convert it back to 'datetime' object and add it back in my dataframe with following command:
df['date'] = pd.to_datetime(my_timestamps)
df['date'] now looks like:
0 2019-03-30 21:00:00-01:00
1 2019-03-30 22:00:00-01:00
2 2019-03-30 23:00:00-01:00
Before or after, I would like to actually apply the timezone offset, so as to have:
0 2019-03-30 20:00:00+00:00
1 2019-03-30 21:00:00+00:00
2 2019-03-30 22:00:00+00:00
Please, how can I obtain that?
I thank you in advance for your help.
Have a good evening,
Bests,
Pierrot
Try pd.DateOffset
on hours
with utc=True
as follows
df['date'] = pd.to_datetime(my_timestamps, utc=True) - pd.DateOffset(hours=2)
Out[860]:
0 2019-03-30 20:00:00+00:00
1 2019-03-30 21:00:00+00:00
2 2019-03-30 22:00:00+00:00
Name: 0, dtype: datetime64[ns, UTC]
If you want to strip the timezone completely, try this
import pytz
df['date'] = (pd.to_datetime(my_timestamps).dt.tz_convert(pytz.FixedOffset(-120))
.dt.tz_localize(None))
Out[863]:
0 2019-03-30 20:00:00
1 2019-03-30 21:00:00
2 2019-03-30 22:00:00
Name: 0, dtype: datetime64[ns]
This trouble is a known trouble with dateutil.
See:
https://github.com/dateutil/dateutil/issues/70
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.