Why is Timestamp doing this so differently from datetime? (converting back and forth to timestamp)

51 views
Skip to first unread message

Johann Petrak

unread,
Dec 14, 2022, 11:21:46 AM12/14/22
to PyData
I stumbled over the following behavior which I find weird: this is in the context
of having to convert back and forth between a python timestamp (float) and a
pandas Timestamp. Here is an illustration:

print("now:                 ", now)
print("fromtimestamp:       ", pd.Timestamp.fromtimestamp(now).timestamp())
print("fromtimestamp utc:   ", pd.Timestamp.fromtimestamp(now, datetime.timezone.utc).timestamp())
print("fromtimestamp local: ", pd.Timestamp.fromtimestamp(now, tz_cet).timestamp())

gives

now:                 1671028217.7562604
fromtimestamp:       1671031817.75626
fromtimestamp utc:   1671028217.75626
fromtimestamp local: 1671028217.75626

So fromtimestamp(ts).timestamp != ts

On the other hand, doing exactly the same using Python datetime:

print("now:                 ", now)
print("fromtimestamp:       ", datetime.datetime.fromtimestamp(now).timestamp())
print("fromtimestamp utc:   ", datetime.datetime.fromtimestamp(now, datetime.timezone.utc).timestamp())
print("fromtimestamp local: ", datetime.datetime.fromtimestamp(now, tz_cet).timestamp())

gives

now:                  1671028217.7562604
fromtimestamp:        1671028217.75626
fromtimestamp utc:    1671028217.75626
fromtimestamp local:  1671028217.75626

in this case fromtimestamp(ts).timestamp() == ts

Why this odd difference in behavior, is this really deliberate?

Datetime, fromtimestamp converts into a naive local time object, then timestamp()
assumes local time if the object is naive, and the result looks reasonable and correct.

With pandas, unfortunately the documentation is much less detailed, but apparently
it first converts the timestamp into a naive local time object, but when
converting back it assumes that the naive object is UTC and gives me the timestamp
of that time instead (which in my case has the offset of CET added).

In other words, with datetime fromtimestamp(x).timestamp() will always yield x, no
matter which time zone one is in, while pandas fromtimestamp(x).timestamp() will
always yield x+tzoffset which in my view is not a good idea.

Can somebody explain this to me?

Cheers,
   Johann
Reply all
Reply to author
Forward
0 new messages