Skip to content

Commit 8e856d1

Browse files
committed
Fix Series[timedelta64]+DatetimeIndex[tz] bugs
ser + index lossed timezone closes pandas-dev#13905 index + ser retained timezone but returned a DatetimeIndex
1 parent 775099c commit 8e856d1

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,9 @@ def _maybe_update_attributes(self, attrs):
855855
return attrs
856856

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

pandas/core/ops.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,15 @@ def wrapper(left, right, name=name, na_op=na_op):
709709

710710
left, right = _align_method_SERIES(left, right)
711711

712+
if is_timedelta64_dtype(left) and isinstance(right, pd.DatetimeIndex):
713+
# GH#13905
714+
# Defer to DatetimeIndex/TimedeltaIndex operations where timezones
715+
# are handled carefully.
716+
result = op(pd.TimedeltaIndex(left), right)
717+
return construct_result(left, result,
718+
index=left.index, name=left.name,
719+
dtype=result.dtype)
720+
712721
converted = _Op.get_op(left, right, name, na_op)
713722

714723
left, right = converted.left, converted.right

pandas/tests/indexes/datetimes/test_arithmetic.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,21 @@ def test_datetimeindex_sub_timestamp_overflow(self):
363363
with pytest.raises(OverflowError):
364364
dtimin - variant
365365

366+
@pytest.mark.parametrize('tz', [None, 'America/Chicago'])
367+
def test_dti_add_series(self, tz):
368+
# GH#13905
369+
index = pd.DatetimeIndex(['2016-06-28 05:30', '2016-06-28 05:31'],
370+
tz=tz)
371+
ser = pd.Series([pd.Timedelta(seconds=5)] * 2,
372+
index=index)
373+
expected = pd.Series(index + pd.Timedelta(seconds=5),
374+
index=index)
375+
assert expected.dtype == index.dtype
376+
res = ser + index
377+
tm.assert_series_equal(res, expected)
378+
res2 = index + ser
379+
tm.assert_series_equal(res2, expected)
380+
366381

367382
# GH 10699
368383
@pytest.mark.parametrize('klass,assert_func', zip([Series, DatetimeIndex],

0 commit comments

Comments
 (0)