Skip to content

Timestamp subtraction of NaT with timezones #11718

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

Closed
kdebrab opened this issue Nov 28, 2015 · 5 comments
Closed

Timestamp subtraction of NaT with timezones #11718

kdebrab opened this issue Nov 28, 2015 · 5 comments
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Timezones Timezone data dtype
Milestone

Comments

@kdebrab
Copy link
Contributor

kdebrab commented Nov 28, 2015

In pandas 0.17.1, a TypeError is returned when trying to subtract a timezone-aware timestamp from a NaT timestamp:

In [1]: import pandas as pd

In [2]: pd.Timestamp(None, tz='utc') - pd.Timestamp('now', tz='utc')
Traceback (most recent call last):

  File "<ipython-input-2-5e0738cec5fa>", line 1, in <module>
    pd.Timestamp(None, tz='utc') - pd.Timestamp('now', tz='utc')

  File "pandas\tslib.pyx", line 1099, in pandas.tslib._NaT.__sub__ (pandas\tslib.c:21618)

  File "pandas\tslib.pyx", line 1026, in pandas.tslib._Timestamp.__sub__ (pandas\tslib.c:20036)

TypeError: Timestamp subtraction must have the same timezones or no timezones

Subtracting a timezone unaware timestamp from a NaT timestamp is no problem. Also subtracting a NaT from a timezone aware timestamp works:

In [3]: pd.Timestamp(None) - pd.Timestamp('now')
Out[3]: NaT

In [4]: pd.Timestamp('now', tz='utc') - pd.Timestamp(None, tz='utc')
Out[4]: NaT

In [5]: pd.Timestamp('now', tz='utc') - pd.Timestamp(None)
Out[5]: NaT
@sinhrks sinhrks added Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Timezones Timezone data dtype labels Nov 29, 2015
@sinhrks
Copy link
Member

sinhrks commented Nov 29, 2015

Thanks for the report. There seems to be several problems related to this. PR is appreciated!

# OK
pd.Timestamp('2011-01-01', tz='UTC') - pd.NaT
# NaT  

pd.DatetimeIndex(['2011-01-01'], tz='UTC') - pd.NaT
# TypeError: Timestamp subtraction must have the same timezones or no timezones

pd.NaT - pd.Timestamp('2011-01-01', tz='UTC')
# TypeError: Timestamp subtraction must have the same timezones or no timezones

pd.NaT - pd.DatetimeIndex(['2011-01-01'], tz='UTC')
# TypeError: Timestamp subtraction must have the same timezones or no timezones

# NG
pd.DatetimeIndex(['2011-01-01']) - pd.NaT
# TimedeltaIndex(['-91777 days +00:12:43.145224'], dtype='timedelta64[ns]', freq=None)

# OK
pd.NaT - pd.DatetimeIndex(['2011-01-01'])
# NaT

# OK
pd.NaT - pd.Timestamp('2011-01-01')
# NaT

# OK
pd.Timestamp('2011-01-01') - pd.NaT
# NaT

@jreback
Copy link
Contributor

jreback commented Nov 29, 2015

iirc there is an issue open about this - we need to have arithmetic methods on the NaT scalar itself

@jreback jreback added this to the 0.18.0 milestone Nov 29, 2015
@jreback
Copy link
Contributor

jreback commented Nov 30, 2015

some of this is being addressed by #11564

@jreback
Copy link
Contributor

jreback commented Jan 24, 2016

@sinhrks / @kawochen

can you update this and see what we have left (e.g. that #11564) didn't already cover

@sinhrks
Copy link
Member

sinhrks commented Jan 28, 2016

Looks no changes from the output attached above. I'll take a look.

@jreback jreback modified the milestones: Next Major Release, 0.18.0 Jan 30, 2016
@jreback jreback modified the milestones: 0.18.0, Next Major Release Feb 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate Timezones Timezone data dtype
Projects
None yet
Development

No branches or pull requests

3 participants