Timestamp subtraction of NaT with timezones #11718

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

Comments

Projects
None yet
3 participants
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
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
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 added this to the 0.18.0 milestone Nov 29, 2015

Contributor

jreback commented Nov 30, 2015

some of this is being addressed by #11564

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

Member

sinhrks commented Jan 28, 2016

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

@jreback jreback modified the milestone: Next Major Release, 0.18.0 Jan 30, 2016

@jreback jreback modified the milestone: 0.18.0, Next Major Release Feb 8, 2016

sinhrks closed this in d838db7 Feb 11, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment