I have a string containing a UTC datetime
utc_str = '2017-11-21T23:00+0100'
which in my local time (Europe/Berlin) is:
local_time = '2017-11-22 00:00'
And is the desired value I would like to obtain from utc_string
.
I can convert utc_string
to local_time
just fine using:
import datetime as dt
utc_time = dt.datetime.strptime(date_str, '%Y-%m-%dT%H:%M%z')
local_time = utc_time.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Europe/Berlin'))
print(local_time.strftime('%Y-%m-%d %H:%M'))
>>> 2017-11-22 00:00
However, when I use Pandas
, I get a different result. It doesn't seem to apply the UTC offset:
import pandas as pd
pd_date = pd.to_datetime(date_str, utc=True)
print(pd_date.strftime('%Y-%m-%d %H:%M'))
>>> '2017-11-21 22:00'
And naively if I try to do the same process as with the datetime
module,
the results are still off:
pd_date = pd.to_datetime(date_str, utc=True)
pd_date = pd_date.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('Europe/Berlin'))
print(pd_date.strftime('%Y-%m-%d %H:%M'))
>>> '2017-11-21 23:00'
Is there something I am not understanding? Am I using pd.to_datetime
or something else wrong? On Python 3.6, Windows 7.