From 68ea8e6dcae8006adac1336586e9e383bd4c68d6 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 16:44:48 -0800 Subject: [PATCH 1/2] BUG: np.datetime64 - TimedeltaArray --- pandas/core/arrays/datetimelike.py | 3 +++ pandas/tests/arithmetic/test_timedelta64.py | 13 +++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index f93db4695d38f..497f33f0f4704 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -1303,6 +1303,9 @@ def __rsub__(self, other): if is_datetime64_any_dtype(other) and is_timedelta64_dtype(self.dtype): # ndarray[datetime64] cannot be subtracted from self, so # we need to wrap in DatetimeArray/Index and flip the operation + if lib.is_scalar(other): + # i.e. np.datetime64 object + return Timestamp(other) - self if not isinstance(other, DatetimeLikeArrayMixin): # Avoid down-casting DatetimeIndex from pandas.core.arrays import DatetimeArray diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index ecb07fa49036a..336a2135200d4 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -896,11 +896,16 @@ def test_td64arr_add_timestamp(self, box_with_array, tz_naive_fixture): result = other + idx tm.assert_equal(result, expected) - def test_td64arr_add_sub_timestamp(self, box_with_array): + @pytest.mark.parametrize( + "ts", + [ + Timestamp("2012-01-01"), + Timestamp("2012-01-01").to_pydatetime(), + Timestamp("2012-01-01").to_datetime64(), + ], + ) + def test_td64arr_add_sub_datetimelike_scalar(self, ts, box_with_array): # GH#11925 - ts = Timestamp("2012-01-01") - # TODO: parametrize over types of datetime scalar? - tdi = timedelta_range("1 day", periods=3) expected = pd.date_range("2012-01-02", periods=3) From 422d3275918dc135cdf57538de8a931352cd2dcc Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 11 Nov 2019 16:46:23 -0800 Subject: [PATCH 2/2] Whatsnew --- doc/source/whatsnew/v1.0.0.rst | 2 +- pandas/tests/arithmetic/test_timedelta64.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.0.0.rst b/doc/source/whatsnew/v1.0.0.rst index cd012fe755337..5a5584f89d3a1 100644 --- a/doc/source/whatsnew/v1.0.0.rst +++ b/doc/source/whatsnew/v1.0.0.rst @@ -318,7 +318,7 @@ Datetimelike Timedelta ^^^^^^^^^ - +- Bug in subtracting a :class:`TimedeltaIndex` or :class:`TimedeltaArray` from a ``np.datetime64`` object (:issue:`29558`) - - diff --git a/pandas/tests/arithmetic/test_timedelta64.py b/pandas/tests/arithmetic/test_timedelta64.py index 336a2135200d4..d45daf9ab8433 100644 --- a/pandas/tests/arithmetic/test_timedelta64.py +++ b/pandas/tests/arithmetic/test_timedelta64.py @@ -905,7 +905,7 @@ def test_td64arr_add_timestamp(self, box_with_array, tz_naive_fixture): ], ) def test_td64arr_add_sub_datetimelike_scalar(self, ts, box_with_array): - # GH#11925 + # GH#11925, GH#29558 tdi = timedelta_range("1 day", periods=3) expected = pd.date_range("2012-01-02", periods=3)