I have a question concerning time conversion in Python/Pandas. The following is not entirely clear to me.
t1 = datetime.datetime(2021,9,15,6,0)
tt1 = t1.timestamp()
pd.to_datetime(tt1,unit='s')
[1] Timestamp('2021-09-15 04:00:00')
What do I have to change to get the following result:
[1] Timestamp('2021-09-15 06:00:00')
Thank you!
CodePudding user response:
You have to set the timezone to UTC in t1
because you use naive datetime and when you use pd.to_datetime
with a float, Pandas assumes the number is relative to 1970-01-01 00:00:00 0000
and not from your local time zone. There is no information about your timezone in 1631678400.0.
t1 = datetime.datetime(2021,9,15,6,0)
tt1 = t1.replace(tzinfo=datetime.timezone.utc).timestamp()
print(pd.to_datetime(tt1,unit='s'))
# Output
2021-09-15 06:00:00
CodePudding user response:
You can offset your time difference to GMT with
pd.to_datetime(tt1, unit='s') pd.Timedelta('02:00:00')
See this answer for more details.