Skip to content

Commit 04beec7

Browse files
jbrockmendeljreback
authored andcommitted
Fix Series[timedelta64]+DatetimeIndex[tz] bugs (#18884)
closes #13905
1 parent a697421 commit 04beec7

File tree

5 files changed

+54
-12
lines changed

5 files changed

+54
-12
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ Numeric
372372

373373
- Bug in :func:`Series.__sub__` subtracting a non-nanosecond ``np.datetime64`` object from a ``Series`` gave incorrect results (:issue:`7996`)
374374
- Bug in :class:`DatetimeIndex`, :class:`TimedeltaIndex` addition and subtraction of zero-dimensional integer arrays gave incorrect results (:issue:`19012`)
375+
- Bug in :func:`Series.__add__` adding Series with dtype ``timedelta64[ns]`` to a timezone-aware ``DatetimeIndex`` incorrectly dropped timezone information (:issue:`13905`)
375376
-
376377

377378
Categorical

pandas/core/indexes/datetimelike.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,9 @@ def __add__(self, other):
671671
from pandas.tseries.offsets import DateOffset
672672

673673
other = lib.item_from_zerodim(other)
674-
if is_timedelta64_dtype(other):
674+
if isinstance(other, ABCSeries):
675+
return NotImplemented
676+
elif is_timedelta64_dtype(other):
675677
return self._add_delta(other)
676678
elif isinstance(self, TimedeltaIndex) and isinstance(other, Index):
677679
if hasattr(other, '_add_delta'):
@@ -702,7 +704,9 @@ def __sub__(self, other):
702704
from pandas.tseries.offsets import DateOffset
703705

704706
other = lib.item_from_zerodim(other)
705-
if is_timedelta64_dtype(other):
707+
if isinstance(other, ABCSeries):
708+
return NotImplemented
709+
elif is_timedelta64_dtype(other):
706710
return self._add_delta(-other)
707711
elif isinstance(self, TimedeltaIndex) and isinstance(other, Index):
708712
if not isinstance(other, TimedeltaIndex):

pandas/core/indexes/datetimes.py

+3
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,9 @@ def _maybe_update_attributes(self, attrs):
854854
return attrs
855855

856856
def _add_delta(self, delta):
857+
if isinstance(delta, ABCSeries):
858+
return NotImplemented
859+
857860
from pandas import TimedeltaIndex
858861
name = self.name
859862

pandas/core/ops.py

+17-10
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from pandas.core.dtypes.generic import (
4040
ABCSeries,
4141
ABCDataFrame,
42-
ABCIndex,
42+
ABCIndex, ABCDatetimeIndex,
4343
ABCPeriodIndex)
4444

4545
# -----------------------------------------------------------------------------
@@ -514,8 +514,9 @@ def _convert_to_array(self, values, name=None, other=None):
514514
values[:] = iNaT
515515

516516
# a datelike
517-
elif isinstance(values, pd.DatetimeIndex):
518-
values = values.to_series()
517+
elif isinstance(values, ABCDatetimeIndex):
518+
# TODO: why are we casting to_series in the first place?
519+
values = values.to_series(keep_tz=True)
519520
# datetime with tz
520521
elif (isinstance(ovalues, datetime.datetime) and
521522
hasattr(ovalues, 'tzinfo')):
@@ -535,6 +536,11 @@ def _convert_to_array(self, values, name=None, other=None):
535536
elif inferred_type in ('timedelta', 'timedelta64'):
536537
# have a timedelta, convert to to ns here
537538
values = to_timedelta(values, errors='coerce', box=False)
539+
if isinstance(other, ABCDatetimeIndex):
540+
# GH#13905
541+
# Defer to DatetimeIndex/TimedeltaIndex operations where
542+
# timezones are handled carefully.
543+
values = pd.TimedeltaIndex(values)
538544
elif inferred_type == 'integer':
539545
# py3 compat where dtype is 'm' but is an integer
540546
if values.dtype.kind == 'm':
@@ -754,25 +760,26 @@ def wrapper(left, right, name=name, na_op=na_op):
754760
na_op = converted.na_op
755761

756762
if isinstance(rvalues, ABCSeries):
757-
name = _maybe_match_name(left, rvalues)
758763
lvalues = getattr(lvalues, 'values', lvalues)
759764
rvalues = getattr(rvalues, 'values', rvalues)
760765
# _Op aligns left and right
761766
else:
762-
if isinstance(rvalues, pd.Index):
763-
name = _maybe_match_name(left, rvalues)
764-
else:
765-
name = left.name
766767
if (hasattr(lvalues, 'values') and
767-
not isinstance(lvalues, pd.DatetimeIndex)):
768+
not isinstance(lvalues, ABCDatetimeIndex)):
768769
lvalues = lvalues.values
769770

771+
if isinstance(right, (ABCSeries, pd.Index)):
772+
# `left` is always a Series object
773+
res_name = _maybe_match_name(left, right)
774+
else:
775+
res_name = left.name
776+
770777
result = wrap_results(safe_na_op(lvalues, rvalues))
771778
return construct_result(
772779
left,
773780
result,
774781
index=left.index,
775-
name=name,
782+
name=res_name,
776783
dtype=dtype,
777784
)
778785

pandas/tests/indexes/datetimes/test_arithmetic.py

+27
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,33 @@ def test_datetimeindex_sub_timestamp_overflow(self):
364364
with pytest.raises(OverflowError):
365365
dtimin - variant
366366

367+
@pytest.mark.parametrize('names', [('foo', None, None),
368+
('baz', 'bar', None),
369+
('bar', 'bar', 'bar')])
370+
@pytest.mark.parametrize('tz', [None, 'America/Chicago'])
371+
def test_dti_add_series(self, tz, names):
372+
# GH#13905
373+
index = DatetimeIndex(['2016-06-28 05:30', '2016-06-28 05:31'],
374+
tz=tz, name=names[0])
375+
ser = Series([Timedelta(seconds=5)] * 2,
376+
index=index, name=names[1])
377+
expected = Series(index + Timedelta(seconds=5),
378+
index=index, name=names[2])
379+
380+
# passing name arg isn't enough when names[2] is None
381+
expected.name = names[2]
382+
assert expected.dtype == index.dtype
383+
result = ser + index
384+
tm.assert_series_equal(result, expected)
385+
result2 = index + ser
386+
tm.assert_series_equal(result2, expected)
387+
388+
expected = index + Timedelta(seconds=5)
389+
result3 = ser.values + index
390+
tm.assert_index_equal(result3, expected)
391+
result4 = index + ser.values
392+
tm.assert_index_equal(result4, expected)
393+
367394
@pytest.mark.parametrize('box', [np.array, pd.Index])
368395
def test_dti_add_offset_array(self, tz, box):
369396
# GH#18849

0 commit comments

Comments
 (0)