(Datetime|Timedelta)Index +/- np.nan · Issue #19274 · pandas-dev/pandas (original) (raw)
AFAICT this is now the last thing standing between us and getting rid of ops._Op and ops._TimeOp. Series ops currently case np.nan to pd.NaT whereas Index ops treat it as a float and raise a TypeError.
tdi = pd.TimedeltaIndex(['1 Day'])
ser = pd.Series(tdi)
>>> tdi + np.nan
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'TimedeltaIndex' and 'float'
>>> ser + np.nan
0 NaT
dtype: timedelta64[ns]
Note also that the behavior of Series[datetime64] has changed since 0.22.0:
0.22.0:
dti = pd.date_range('2016-01-01', periods=2)
ser = pd.Series(dti)
>>> dti - np.nan
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'DatetimeIndex' and 'float'
>>> ser - np.nan
0 NaT
1 NaT
dtype: timedelta64[ns]
# Both raise on addition, assuming that np.nan gets cast to pd.NaT wearing its datetime hat.
ATM ser - np.nan
also raises, since that now delegates to the DatetimeIndex op.