Skip to content

Commit d330416

Browse files
jbrockmendeljreback
authored andcommitted
BUG: Fix NaT +/- DTA/TDA (#27740)
1 parent d44fb07 commit d330416

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

pandas/_libs/tslibs/nattype.pyx

+6-3
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ cdef class _NaT(datetime):
123123
return c_NaT
124124
elif getattr(other, '_typ', None) in ['dateoffset', 'series',
125125
'period', 'datetimeindex',
126-
'timedeltaindex']:
126+
'datetimearray',
127+
'timedeltaindex',
128+
'timedeltaarray']:
127129
# Duplicate logic in _Timestamp.__add__ to avoid needing
128130
# to subclass; allows us to @final(_Timestamp.__add__)
129131
return NotImplemented
@@ -151,9 +153,10 @@ cdef class _NaT(datetime):
151153
return self + neg_other
152154

153155
elif getattr(other, '_typ', None) in ['period', 'series',
154-
'periodindex', 'dateoffset']:
156+
'periodindex', 'dateoffset',
157+
'datetimearray',
158+
'timedeltaarray']:
155159
return NotImplemented
156-
157160
return NaT
158161

159162
def __pos__(self):

pandas/tests/scalar/test_nat.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
from pandas._libs.tslibs import iNaT
88
import pandas.compat as compat
99

10+
from pandas.core.dtypes.common import is_datetime64_any_dtype
11+
1012
from pandas import (
1113
DatetimeIndex,
1214
Index,
@@ -18,7 +20,7 @@
1820
Timestamp,
1921
isna,
2022
)
21-
from pandas.core.arrays import PeriodArray
23+
from pandas.core.arrays import DatetimeArray, PeriodArray, TimedeltaArray
2224
from pandas.util import testing as tm
2325

2426

@@ -397,7 +399,9 @@ def test_nat_rfloordiv_timedelta(val, expected):
397399
"value",
398400
[
399401
DatetimeIndex(["2011-01-01", "2011-01-02"], name="x"),
400-
DatetimeIndex(["2011-01-01", "2011-01-02"], name="x"),
402+
DatetimeIndex(["2011-01-01", "2011-01-02"], tz="US/Eastern", name="x"),
403+
DatetimeArray._from_sequence(["2011-01-01", "2011-01-02"]),
404+
DatetimeArray._from_sequence(["2011-01-01", "2011-01-02"], tz="US/Pacific"),
401405
TimedeltaIndex(["1 day", "2 day"], name="x"),
402406
],
403407
)
@@ -406,19 +410,24 @@ def test_nat_arithmetic_index(op_name, value):
406410
exp_name = "x"
407411
exp_data = [NaT] * 2
408412

409-
if isinstance(value, DatetimeIndex) and "plus" in op_name:
410-
expected = DatetimeIndex(exp_data, name=exp_name, tz=value.tz)
413+
if is_datetime64_any_dtype(value.dtype) and "plus" in op_name:
414+
expected = DatetimeIndex(exp_data, tz=value.tz, name=exp_name)
411415
else:
412416
expected = TimedeltaIndex(exp_data, name=exp_name)
413417

414-
tm.assert_index_equal(_ops[op_name](NaT, value), expected)
418+
if not isinstance(value, Index):
419+
expected = expected.array
420+
421+
op = _ops[op_name]
422+
result = op(NaT, value)
423+
tm.assert_equal(result, expected)
415424

416425

417426
@pytest.mark.parametrize(
418427
"op_name",
419428
["left_plus_right", "right_plus_left", "left_minus_right", "right_minus_left"],
420429
)
421-
@pytest.mark.parametrize("box", [TimedeltaIndex, Series])
430+
@pytest.mark.parametrize("box", [TimedeltaIndex, Series, TimedeltaArray._from_sequence])
422431
def test_nat_arithmetic_td64_vector(op_name, box):
423432
# see gh-19124
424433
vec = box(["1 day", "2 day"], dtype="timedelta64[ns]")

0 commit comments

Comments
 (0)