Skip to content

Commit 0ab8eb2

Browse files
jbrockmendeljreback
authored andcommitted
TST: Extend datetime64 arith tests to array classes, fix several broken cases (#23771)
1 parent e538182 commit 0ab8eb2

File tree

9 files changed

+420
-327
lines changed

9 files changed

+420
-327
lines changed

pandas/core/arrays/datetimelike.py

+8
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,10 @@ def __add__(self, other):
727727
else: # pragma: no cover
728728
return NotImplemented
729729

730+
if is_timedelta64_dtype(result) and isinstance(result, np.ndarray):
731+
from pandas.core.arrays import TimedeltaArrayMixin
732+
# TODO: infer freq?
733+
return TimedeltaArrayMixin(result)
730734
return result
731735

732736
cls.__add__ = __add__
@@ -791,6 +795,10 @@ def __sub__(self, other):
791795
else: # pragma: no cover
792796
return NotImplemented
793797

798+
if is_timedelta64_dtype(result) and isinstance(result, np.ndarray):
799+
from pandas.core.arrays import TimedeltaArrayMixin
800+
# TODO: infer freq?
801+
return TimedeltaArrayMixin(result)
794802
return result
795803

796804
cls.__sub__ = __sub__

pandas/core/arrays/datetimes.py

+5
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,11 @@ def __new__(cls, values, freq=None, tz=None, dtype=None):
222222
# if dtype has an embedded tz, capture it
223223
tz = dtl.validate_tz_from_dtype(dtype, tz)
224224

225+
if is_object_dtype(values):
226+
# kludge; dispatch until the DatetimeArray constructor is complete
227+
from pandas import DatetimeIndex
228+
values = DatetimeIndex(values, freq=freq, tz=tz)
229+
225230
if isinstance(values, ABCSeries):
226231
# extract to ndarray or DatetimeIndex
227232
values = values._values

pandas/core/arrays/timedeltas.py

+5
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ def _evaluate_with_timedelta_like(self, other, op):
300300

301301
return NotImplemented
302302

303+
def __neg__(self):
304+
if self.freq is not None:
305+
return type(self)(-self._data, freq=-self.freq)
306+
return type(self)(-self._data)
307+
303308
# ----------------------------------------------------------------
304309
# Conversion Methods - Vectorized analogues of Timedelta methods
305310

pandas/core/ops.py

+4
Original file line numberDiff line numberDiff line change
@@ -947,6 +947,10 @@ def should_series_dispatch(left, right, op):
947947
# numpy integer dtypes as timedelta64 dtypes in this scenario
948948
return True
949949

950+
if is_datetime64_dtype(ldtype) and is_object_dtype(rdtype):
951+
# in particular case where right is an array of DateOffsets
952+
return True
953+
950954
return False
951955

952956

pandas/tests/arithmetic/conftest.py

-8
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@ def numeric_idx(request):
5757
return request.param
5858

5959

60-
@pytest.fixture
61-
def tdser():
62-
"""
63-
Return a Series with dtype='timedelta64[ns]', including a NaT.
64-
"""
65-
return pd.Series(['59 Days', '59 Days', 'NaT'], dtype='timedelta64[ns]')
66-
67-
6860
# ------------------------------------------------------------------
6961
# Scalar Fixtures
7062

0 commit comments

Comments
 (0)