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

NaT in TimedeltaIndex + Timestamp overflows #17991

Closed
shoyer opened this issue Oct 26, 2017 · 3 comments · Fixed by #18020
Closed

NaT in TimedeltaIndex + Timestamp overflows #17991

shoyer opened this issue Oct 26, 2017 · 3 comments · Fixed by #18020
Labels
Bug Timedelta Timedelta data type
Milestone

Comments

@shoyer
Copy link
Member

shoyer commented Oct 26, 2017

With pandas=0.20.3.

Adding a NaT in a TimedeltaIndex to a Timestamp results in an OverflowError:

In [9]: pd.to_timedelta(['24658 days 11:15:00', 'NaT']) + pd.Timestamp('1950-01-01')
---------------------------------------------------------------------------
OverflowError                             Traceback (most recent call last)
<ipython-input-9-cc287bf4c401> in <module>()
----> 1 pd.to_timedelta(['24658 days 11:15:00', 'NaT']) + pd.Timestamp('1950-01-01')

~/conda/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/indexes/datetimelike.py in __add__(self, other)
    658                 return self.shift(other)
    659             elif isinstance(other, (Timestamp, datetime)):
--> 660                 return self._add_datelike(other)
    661             else:  # pragma: no cover
    662                 return NotImplemented

~/conda/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/indexes/timedeltas.py in _add_datelike(self, other)
    354             other = Timestamp(other)
    355             i8 = self.asi8
--> 356             result = checked_add_with_arr(i8, other.value)
    357             result = self._maybe_mask_results(result, fill_value=iNaT)
    358         return DatetimeIndex(result, name=self.name, copy=False)

~/conda/envs/xarray-py36/lib/python3.6/site-packages/pandas/core/algorithms.py in checked_add_with_arr(arr, b, arr_mask, b_mask)
    889
    890     if to_raise:
--> 891         raise OverflowError("Overflow in int64 addition")
    892     return arr + b
    893

OverflowError: Overflow in int64 addition

But NaT should actually be masked out instead. This doesn't overflow when adding the individual scalars:

In [11]: pd.NaT + pd.Timestamp('1950-01-01')
Out[11]: NaT

In [12]: pd.Timedelta('24658 days 11:15:00') + pd.Timestamp('1950-01-01')
Out[12]: Timestamp('2017-07-06 11:15:00')

xref pydata/xarray#1662

@jschendel
Copy link
Member

Interestingly enough, this appears to work for 1970 and onwards:

In [19]: pd.to_timedelta(['24658 days 11:15:00', 'NaT']) + pd.Timestamp('1970-01-01')
Out[19]: DatetimeIndex(['2037-07-06 11:15:00', 'NaT'], dtype='datetime64[ns]', freq=None)

In [20]: pd.to_timedelta(['24658 days 11:15:00', 'NaT']) + pd.Timestamp('1969-12-31')
---------------------------------------------------------------------------
OverflowError: Overflow in int64 addition

@jreback
Copy link
Contributor

jreback commented Oct 26, 2017

this is a duplicate issue - if u would have a look for the match

@jbrockmendel
Copy link
Member

Straightforward fix in TimedeltaIndex._add_datelike: result = checked_add_with_arr(i8, other.value) becomes result = checked_add_with_arr(i8, other.value, arr_mask=self._isnan). I'll put together a PR. Any other test cases while we're at it?

jbrockmendel added a commit to jbrockmendel/pandas that referenced this issue Oct 29, 2017
WIP filling out a test matrix of arithmetic ops

closes pandas-dev#17991
@jreback jreback added this to the 0.21.1 milestone Oct 31, 2017
jbrockmendel added a commit to jbrockmendel/pandas that referenced this issue Nov 6, 2017
TomAugspurger pushed a commit to TomAugspurger/pandas that referenced this issue Dec 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants