0

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

pierre_j
  • 895
  • 2
  • 11
  • 26

2 Answers2

0

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]
Andy L.
  • 24,909
  • 4
  • 17
  • 29
  • Hi, thanks to answer. Unfortunately, this is not what I am looking for. This timezone has DST. So offset, can be -1 hour, as on the example given, or -2 hours. I would like to simply apply the offset given. I am so surprised so maybe I don't understand: why the offset I get cannot be applied as is: -1h. In your code, you offset by -2 hours. If tzoffset is -1, to get UTC time, you should substract 1 hour to current time. Is the timezone correctly initialized? – pierre_j Dec 27 '19 at 20:37