Skip to content

Commit

Permalink
TST: Extend datetime64 arith tests to array classes, fix several brok…
Browse files Browse the repository at this point in the history
…en cases (#23771)
  • Loading branch information
jbrockmendel authored and jreback committed Nov 19, 2018
1 parent e538182 commit 0ab8eb2
Show file tree
Hide file tree
Showing 9 changed files with 420 additions and 327 deletions.
8 changes: 8 additions & 0 deletions pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,10 @@ def __add__(self, other):
else: # pragma: no cover
return NotImplemented

if is_timedelta64_dtype(result) and isinstance(result, np.ndarray):
from pandas.core.arrays import TimedeltaArrayMixin
# TODO: infer freq?
return TimedeltaArrayMixin(result)
return result

cls.__add__ = __add__
Expand Down Expand Up @@ -791,6 +795,10 @@ def __sub__(self, other):
else: # pragma: no cover
return NotImplemented

if is_timedelta64_dtype(result) and isinstance(result, np.ndarray):
from pandas.core.arrays import TimedeltaArrayMixin
# TODO: infer freq?
return TimedeltaArrayMixin(result)
return result

cls.__sub__ = __sub__
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ def __new__(cls, values, freq=None, tz=None, dtype=None):
# if dtype has an embedded tz, capture it
tz = dtl.validate_tz_from_dtype(dtype, tz)

if is_object_dtype(values):
# kludge; dispatch until the DatetimeArray constructor is complete
from pandas import DatetimeIndex
values = DatetimeIndex(values, freq=freq, tz=tz)

if isinstance(values, ABCSeries):
# extract to ndarray or DatetimeIndex
values = values._values
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/arrays/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,11 @@ def _evaluate_with_timedelta_like(self, other, op):

return NotImplemented

def __neg__(self):
if self.freq is not None:
return type(self)(-self._data, freq=-self.freq)
return type(self)(-self._data)

# ----------------------------------------------------------------
# Conversion Methods - Vectorized analogues of Timedelta methods

Expand Down
4 changes: 4 additions & 0 deletions pandas/core/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,10 @@ def should_series_dispatch(left, right, op):
# numpy integer dtypes as timedelta64 dtypes in this scenario
return True

if is_datetime64_dtype(ldtype) and is_object_dtype(rdtype):
# in particular case where right is an array of DateOffsets
return True

return False


Expand Down
8 changes: 0 additions & 8 deletions pandas/tests/arithmetic/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ def numeric_idx(request):
return request.param


@pytest.fixture
def tdser():
"""
Return a Series with dtype='timedelta64[ns]', including a NaT.
"""
return pd.Series(['59 Days', '59 Days', 'NaT'], dtype='timedelta64[ns]')


# ------------------------------------------------------------------
# Scalar Fixtures

Expand Down
Loading

0 comments on commit 0ab8eb2

Please sign in to comment.