Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamp.timestamp() inconsistent with Datetime.timestamp() #22450

Open
patrickspry opened this issue Aug 21, 2018 · 5 comments
Open

Timestamp.timestamp() inconsistent with Datetime.timestamp() #22450

patrickspry opened this issue Aug 21, 2018 · 5 comments
Labels
Compat pandas objects compatability with Numpy or Python functions Deprecate Functionality to remove in pandas Needs Discussion Requires discussion from core team before further action Timeseries

Comments

@patrickspry
Copy link

patrickspry commented Aug 21, 2018

Code Sample, a copy-pastable example if possible

import datetime as dt
import pandas as pd

t = dt.datetime.now()
ts1 = t.timestamp()
ts2 = pd.Timestamp(t).timestamp()

print((ts1 - ts2) / 3600)

Problem description

Pandas and Datetime evidently have different defaults when handling time objects for which tzinfo=None. This changed somewhere in between pandas 0.19 and 0.23 (upgrading broke a bunch of my tests). I think keeping consistency between these two libraries would remove some of the confusion in the bizarrely complicated area that is python time-handling.

In the example, the two timestamp floats should match. Currently, my understanding is the datetime library defaults to the current timezone whereas Pandas assumes GMT. There isn't information in the Pandas API documentation regarding how Timestamp.timestamp() handles naive Timestamps (link).

@chris-b1
Copy link
Contributor

ref #17906 (@jbrockmendel)

def timestamp(self):

What python does here seems strange? But I suppose should be consistent since we only have that method for compat.

import datetime as dt
import pandas as pd

t = dt.datetime.now()
ts1 = t.timestamp()
ts2 = pd.Timestamp(t).tz_localize('US/Central').timestamp()

print((ts1 - ts2) / 3600)
# 0.0

@chris-b1 chris-b1 added Timeseries Timezones Timezone data dtype labels Aug 21, 2018
@jbrockmendel
Copy link
Member

IIRC the thought process in the current implementation revolved around the stdlib version being nonsensical; "unix timestamp if if were local" isn't a concept I've seen elsewhere. The decision was that pd.Timestamp.now().timestamp should match time.time().

That said, differing from the stdlib is certainly not ideal. Personally I prefer the More Correct implementation we have, but understand if others prefer the More Compatible implementation.

@jbrockmendel
Copy link
Member

Discussion in #29779 included idea of putting a note in the Timestamp.timestamp docstring (currently we have a note in a comment)

@mroeschke mroeschke added Compat pandas objects compatability with Numpy or Python functions Bug labels Apr 1, 2020
@mroeschke mroeschke removed Compat pandas objects compatability with Numpy or Python functions Timezones Timezone data dtype labels Apr 10, 2020
@torbiak
Copy link

torbiak commented Mar 26, 2021

pandas.Timestamp.timestamp() behaviour is at odds with pretty much everything else I use, AFAICT. Any place that I specify times, if I don't add a timezone it's assumed that that it's in the local timezone. And if I want timezone-naive timestamps to be interpreted as UTC I can configure my OS's timezone (or on POSIX change the TZ env var).

@jbrockmendel, you say "The decision was that pd.Timestamp.now().timestamp should match time.time().", but it doesn't, at least not for Pandas 1.2.3 and Python 3.9.2 and at a -0600 UTC offset:

print((time.time()), pd.Timestamp.now().timestamp())
1616724259.7153366 1616702659.715341

I think the idea in stdlib and elsewhere is less "unix timestamp if if were local", which I agree makes no sense, and more "interpret this timestamp as being in my local timezone".

@jbrockmendel
Copy link
Member

At this point I think a) the stdlib behavior is decidedly unhelpful, b) since the method is so little used, there's not much harm in deprecating then changing ours to match

@mroeschke mroeschke added Deprecate Functionality to remove in pandas Needs Discussion Requires discussion from core team before further action and removed Bug labels Jun 22, 2021
@jbrockmendel jbrockmendel added the Compat pandas objects compatability with Numpy or Python functions label Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Compat pandas objects compatability with Numpy or Python functions Deprecate Functionality to remove in pandas Needs Discussion Requires discussion from core team before further action Timeseries
Projects
None yet
Development

No branches or pull requests

5 participants