Skip to content

NaT in TimedeltaIndex + Timestamp overflows #17991

@shoyer

Description

@shoyer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions