I was tinkering around with converting pandas.Timestamp
to the built in python datetime
. I tried the following:
pandas_time = pd.Timestamp(year=2020, day=4, month=2, hour=0, minute=0, second=1)
pandas_ts = pandas_time.timestamp()
datetime_time = datetime.datetime.fromtimestamp(pandas_ts)
datetime_ts = datetime_time.timestamp()
Looking at the variables gives this:
pandas_time: 2020-02-04 00:00:01
datetime_time: 2020-02-04 01:00:01
pandas_ts: 1580774401.0
datetime_ts: 1580774401.0
So they both have the same timestamp but the dates differ by one hour. When I tried it the other way around, I got this:
datetime_time = datetime.datetime(year=2020, day=4, month=2, hour=0, minute=0, second=1)
datetime_ts = datetime_time.timestamp()
pandas_time = pd.Timestamp(datetime_time)
pandas_ts = pandas_time.timestamp()
pandas_time: 2020-02-04 00:00:01
datetime_time: 2020-02-04 00:00:01
pandas_ts: 1580774401.0
datetime_ts: 1580770801.0
Now the dates are the same but the timestamps differ by 3600 (1 hour).
I do know that I could use pandas to_pydatetime
for conversion from pandas Timestamp to python datetime but I'm wondering why this difference occurs. Are their starting points defined differently? And if so, why?