Skip to content

BUG: Timespan.timestamp() surprisinly different from datetime.datetime.timestamp() for naive timestamps #50298

Open
@johann-petrak

Description

@johann-petrak

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

now = time.time()

pdts = pd.Timestamp.fromtimestamp(now)    # naive TS 
print("PD", pdts)
pyts = datetime.datetime.fromtimestamp(now)  # naive TS
print("PY", pyts)

print("PD", pdts.timestamp())   # we get back what we put in
print("PY", pyts.timestamp())   # get back modified by local TZ offset! 

print("DIFF", pdts.timestamp()-pyts.timestamp())

# Output:
# PD 2022-12-16 16:44:38.732732
# PY 2022-12-16 16:44:38.732732
# PD 1671209078.732732
# PY 1671205478.732732
# DIFF 3600.0

Issue Description

I found it very surprising that pd.Timestamp.timestamp() behaves differently from datetime.datetime.timestamp() and in a very unfortunate way: basically it means that with naive timestamps:

pd.Timestamp.fromtimestamp(ts).timestamp() != ts

in all timezones except UTC, while

datetime.datetime.fromtimestamp(ts) == ts

always.

Expected Behavior

Since Timestamp is meant to behave similar to datetime, this should also be the case for the timestamp() method by default.

Sadly the documentation does not point out those differences prominently enough, e.g. this page: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Timestamp.html
merely states "timestamp Return POSIX timestamp as float." which is really incorrect as it returns the POSIX timestamp adjusted by the local timezone offset as a float if the timestamp is naive.

Installed Versions

Tested with versions 1.5.2 and 2.0.0.dev0+933.g70eef55e43

Metadata

Metadata

Assignees

No one assigned

    Labels

    API - ConsistencyInternal Consistency of API/BehaviorTimestamppd.Timestamp and associated methods

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions