Skip to content Skip to sidebar Skip to footer

Difference Between Pandas Timestamp And Datetime

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

Solution 1:

If you look at the datetime documentation it is written for .fromtimestamp(timestamp)

classmethod date.fromtimestamp(timestamp) Return the local date corresponding to the POSIX timestamp, such as is returned by time.time().

So it returns local date. That's it. So you need to tell it to use utc explicitly whereas pandas uses utc by default.

pandas_time = pd.Timestamp(year=2020, month=2, day=4,  hour=0, minute=0, second=1)
pandas_ts = pandas_time.timestamp()
datetime_time = datetime.datetime.fromtimestamp(pandas_ts, tz=timezone.utc)
datetime_ts = datetime_time.timestamp()

Similarly for the second case

datetime_time = datetime.datetime(year=2020, day=4, month=2, hour=0, minute=0, second=1, tzinfo=timezone.utc)
datetime_ts = datetime_time.timestamp()
pandas_time = pd.Timestamp(datetime_time)
pandas_ts = pandas_time.timestamp()

From your question it seems you are living in a UTC+1 country :p

Post a Comment for "Difference Between Pandas Timestamp And Datetime"