Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas timestamp and python datetime interpret timezone differently

I don't understand why a isn't the same as b:

import pandas as pd
from datetime import datetime
import pytz

here = pytz.timezone('Europe/Amsterdam')

a = pd.Timestamp('2018-4-9', tz=here).to_pydatetime() 
# datetime.datetime(2018, 4, 9, 0, 0, tzinfo=<DstTzInfo'Europe/Amsterdam' CEST+2:00:00 DST>)
b = datetime(2018, 4, 9, 0, tzinfo=here)
# datetime.datetime(2018, 4, 9, 0, 0, tzinfo=<DstTzInfo 'Europe/Amsterdam' LMT+0:20:00 STD>)

print(b-a)
# returns 01:40:00
like image 851
Tony Avatar asked May 04 '26 09:05

Tony


2 Answers

From this stackoverflow post I learned that tzinfo doesn't work well for some timezones and that could be the reason for the wrong result. pytz doc:

Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.

The solution is to use localize or astimezone:

import pandas as pd
from datetime import datetime
import pytz

here = pytz.timezone('Europe/Amsterdam')

a = pd.Timestamp('2018-4-9', tz=here).to_pydatetime() 
# datetime.datetime(2018, 4, 9, 0, 0, tzinfo=<DstTzInfo'Europe/Amsterdam' CEST+2:00:00 DST>)
b = here.localize(datetime(2018, 4, 9))
# datetime.datetime(2018, 4, 9, 0, 0, tzinfo=<DstTzInfo 'Europe/Amsterdam' CEST+2:00:00 DST>)

print(b-a)
# returns 00:00:00
like image 114
João Abrantes Avatar answered May 05 '26 23:05

João Abrantes


If you look at a and b,

a
datetime.datetime(2018, 4, 9, 0, 0, tzinfo=<DstTzInfo 'Europe/Amsterdam' CEST+2:00:00 DST>)

verus

b
datetime.datetime(2018, 4, 9, 0, 0, tzinfo=<DstTzInfo 'Europe/Amsterdam' LMT+0:20:00 STD>)

CEST European Central Summer Time

vs

LMT Local Mean Time

like image 43
Scott Boston Avatar answered May 06 '26 00:05

Scott Boston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!