Skip to content

Commit 82c9547

Browse files
jbrockmendeljreback
authored andcommitted
BUG: np.datetime64 - TimedeltaArray (#29558)
1 parent 5b580fb commit 82c9547

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

doc/source/whatsnew/v1.0.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ Datetimelike
318318

319319
Timedelta
320320
^^^^^^^^^
321-
321+
- Bug in subtracting a :class:`TimedeltaIndex` or :class:`TimedeltaArray` from a ``np.datetime64`` object (:issue:`29558`)
322322
-
323323
-
324324

pandas/core/arrays/datetimelike.py

+3
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,9 @@ def __rsub__(self, other):
13031303
if is_datetime64_any_dtype(other) and is_timedelta64_dtype(self.dtype):
13041304
# ndarray[datetime64] cannot be subtracted from self, so
13051305
# we need to wrap in DatetimeArray/Index and flip the operation
1306+
if lib.is_scalar(other):
1307+
# i.e. np.datetime64 object
1308+
return Timestamp(other) - self
13061309
if not isinstance(other, DatetimeLikeArrayMixin):
13071310
# Avoid down-casting DatetimeIndex
13081311
from pandas.core.arrays import DatetimeArray

pandas/tests/arithmetic/test_timedelta64.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,16 @@ def test_td64arr_add_timestamp(self, box_with_array, tz_naive_fixture):
896896
result = other + idx
897897
tm.assert_equal(result, expected)
898898

899-
def test_td64arr_add_sub_timestamp(self, box_with_array):
900-
# GH#11925
901-
ts = Timestamp("2012-01-01")
902-
# TODO: parametrize over types of datetime scalar?
903-
899+
@pytest.mark.parametrize(
900+
"ts",
901+
[
902+
Timestamp("2012-01-01"),
903+
Timestamp("2012-01-01").to_pydatetime(),
904+
Timestamp("2012-01-01").to_datetime64(),
905+
],
906+
)
907+
def test_td64arr_add_sub_datetimelike_scalar(self, ts, box_with_array):
908+
# GH#11925, GH#29558
904909
tdi = timedelta_range("1 day", periods=3)
905910
expected = pd.date_range("2012-01-02", periods=3)
906911

0 commit comments

Comments
 (0)