Skip to content

Commit 735bb11

Browse files
committed
BUG: fix tz-aware DatetimeIndex + TimedeltaIndex (pandas-dev#17558)
Fix minor bug causing DatetimeIndex + TimedeltaIndex to raise an error, and fix another bug causing the sum of a tz-aware DatetimeIndex and a numpy array of timedeltas to incorrectly have timezone applied twice.
1 parent 0e85ca7 commit 735bb11

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pandas/core/indexes/datetimelike.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
is_integer, is_float,
1414
is_bool_dtype, _ensure_int64,
1515
is_scalar, is_dtype_equal,
16+
is_timedelta64_dtype, is_integer_dtype,
1617
is_list_like)
1718
from pandas.core.dtypes.generic import (
1819
ABCIndex, ABCSeries,
@@ -651,6 +652,15 @@ def __add__(self, other):
651652
raise TypeError("cannot add {typ1} and {typ2}"
652653
.format(typ1=type(self).__name__,
653654
typ2=type(other).__name__))
655+
elif isinstance(other, np.ndarray):
656+
if is_timedelta64_dtype(other):
657+
return self._add_delta(TimedeltaIndex(other))
658+
elif is_integer_dtype(other):
659+
return NotImplemented
660+
else:
661+
raise TypeError("cannot add {typ1} and np.ndarray[{typ2}]"
662+
.format(typ1=type(self).__name__,
663+
typ2=other.dtype))
654664
elif isinstance(other, (DateOffset, timedelta, np.timedelta64,
655665
Timedelta)):
656666
return self._add_delta(other)
@@ -731,7 +741,7 @@ def _add_delta_tdi(self, other):
731741
if self.hasnans or other.hasnans:
732742
mask = (self._isnan) | (other._isnan)
733743
new_values[mask] = iNaT
734-
return new_values.view(self.dtype)
744+
return new_values.view('i8')
735745

736746
def isin(self, values):
737747
"""

pandas/tests/indexes/datetimes/test_ops.py

+12
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,18 @@ def test_add_dti_dti(self):
460460
with pytest.raises(TypeError):
461461
dti + dti_tz
462462

463+
def test_add_dti_ndarray(self):
464+
# GH 17558
465+
# Check that tz-aware DatetimeIndex + np.array(dtype="timedelta64") / TimedeltaIndex
466+
# works as expected
467+
dti = pd.DatetimeIndex([pd.Timestamp("2017/01/01")]).tz_localize('US/Eastern')
468+
expected = pd.DatetimeIndex([pd.Timestamp("2017/01/01 01:00")]).tz_localize('US/Eastern')
469+
470+
td_np = np.array([np.timedelta64(1, 'h')], dtype="timedelta64[ns]")
471+
tm.assert_index_equal(dti + td_np, expected)
472+
tm.assert_index_equal(dti + td_np[0], expected)
473+
tm.assert_index_equal(dti + TimedeltaIndex(td_np), expected)
474+
463475
def test_difference(self):
464476
for tz in self.tz:
465477
# diff

0 commit comments

Comments
 (0)