Repository: pandas-dev/pandas

Issue:
DatetimeIndex + TimeDelta gives wrong results when timezone is set
When I add a time delta to a DatetimeIndex which is tz-aware, the results appear as UTC, but sometimes claim to be in the original non-UTC timezone.
#### Code

```
import pandas as pd
index = pd.DatetimeIndex(['2016-06-28 05:30', '2016-06-28 05:31'], dtype='datetime64[ns, America/Chicago]')
offset = pd.Series(['00:00:05', '00:00:05'], dtype='timedelta64[ns]', index=index)
print(index + offset)
print(index + offset.values)
```
#### Actual Output

```
2016-06-28 05:30:00-05:00   2016-06-28 10:30:05
2016-06-28 05:31:00-05:00   2016-06-28 10:31:05
dtype: datetime64[ns]
DatetimeIndex(['2016-06-28 10:30:05-05:00', '2016-06-28 10:31:05-05:00'], dtype='datetime64[ns, America/Chicago]', freq=None)
```
#### Expected Output

```
2016-06-28 05:30:00-05:00   2016-06-28 05:30:05-05:00
2016-06-28 05:31:00-05:00   2016-06-28 05:31:05-05:00
dtype: datetime64[ns]
DatetimeIndex(['2016-06-28 05:30:05-05:00', '2016-06-28 05:31:05-05:00'], dtype='datetime64[ns, America/Chicago]', freq=None)
```

Note however that this does print the expected result:

```
print(index + pd.to_timedelta('00:00:05'))
```

I'm using Pandas 0.18.1.



Discussion:
yep, thought we had an issue about this, but can't seem to find it.


Resolving patch:
diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py
--- a/pandas/core/indexes/datetimelike.py
+++ b/pandas/core/indexes/datetimelike.py
@@ -671,7 +671,9 @@ def __add__(self, other):
             from pandas.tseries.offsets import DateOffset
 
             other = lib.item_from_zerodim(other)
-            if is_timedelta64_dtype(other):
+            if isinstance(other, ABCSeries):
+                return NotImplemented
+            elif is_timedelta64_dtype(other):
                 return self._add_delta(other)
             elif isinstance(self, TimedeltaIndex) and isinstance(other, Index):
                 if hasattr(other, '_add_delta'):
@@ -702,7 +704,9 @@ def __sub__(self, other):
             from pandas.tseries.offsets import DateOffset
 
             other = lib.item_from_zerodim(other)
-            if is_timedelta64_dtype(other):
+            if isinstance(other, ABCSeries):
+                return NotImplemented
+            elif is_timedelta64_dtype(other):
                 return self._add_delta(-other)
             elif isinstance(self, TimedeltaIndex) and isinstance(other, Index):
                 if not isinstance(other, TimedeltaIndex):
diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py
--- a/pandas/core/indexes/datetimes.py
+++ b/pandas/core/indexes/datetimes.py
@@ -854,6 +854,9 @@ def _maybe_update_attributes(self, attrs):
         return attrs
 
     def _add_delta(self, delta):
+        if isinstance(delta, ABCSeries):
+            return NotImplemented
+
         from pandas import TimedeltaIndex
         name = self.name
 
diff --git a/pandas/core/ops.py b/pandas/core/ops.py
--- a/pandas/core/ops.py
+++ b/pandas/core/ops.py
@@ -39,7 +39,7 @@
 from pandas.core.dtypes.generic import (
     ABCSeries,
     ABCDataFrame,
-    ABCIndex,
+    ABCIndex, ABCDatetimeIndex,
     ABCPeriodIndex)
 
 # -----------------------------------------------------------------------------
@@ -514,8 +514,9 @@ def _convert_to_array(self, values, name=None, other=None):
                 values[:] = iNaT
 
             # a datelike
-            elif isinstance(values, pd.DatetimeIndex):
-                values = values.to_series()
+            elif isinstance(values, ABCDatetimeIndex):
+                # TODO: why are we casting to_series in the first place?
+                values = values.to_series(keep_tz=True)
             # datetime with tz
             elif (isinstance(ovalues, datetime.datetime) and
                   hasattr(ovalues, 'tzinfo')):
@@ -535,6 +536,11 @@ def _convert_to_array(self, values, name=None, other=None):
         elif inferred_type in ('timedelta', 'timedelta64'):
             # have a timedelta, convert to to ns here
             values = to_timedelta(values, errors='coerce', box=False)
+            if isinstance(other, ABCDatetimeIndex):
+                # GH#13905
+                # Defer to DatetimeIndex/TimedeltaIndex operations where
+                # timezones are handled carefully.
+                values = pd.TimedeltaIndex(values)
         elif inferred_type == 'integer':
             # py3 compat where dtype is 'm' but is an integer
             if values.dtype.kind == 'm':
@@ -754,25 +760,26 @@ def wrapper(left, right, name=name, na_op=na_op):
         na_op = converted.na_op
 
         if isinstance(rvalues, ABCSeries):
-            name = _maybe_match_name(left, rvalues)
             lvalues = getattr(lvalues, 'values', lvalues)
             rvalues = getattr(rvalues, 'values', rvalues)
             # _Op aligns left and right
         else:
-            if isinstance(rvalues, pd.Index):
-                name = _maybe_match_name(left, rvalues)
-            else:
-                name = left.name
             if (hasattr(lvalues, 'values') and
-                    not isinstance(lvalues, pd.DatetimeIndex)):
+                    not isinstance(lvalues, ABCDatetimeIndex)):
                 lvalues = lvalues.values
 
+        if isinstance(right, (ABCSeries, pd.Index)):
+            # `left` is always a Series object
+            res_name = _maybe_match_name(left, right)
+        else:
+            res_name = left.name
+
         result = wrap_results(safe_na_op(lvalues, rvalues))
         return construct_result(
             left,
             result,
             index=left.index,
-            name=name,
+            name=res_name,
             dtype=dtype,
         )
 


Test patch:
diff --git a/pandas/tests/indexes/datetimes/test_arithmetic.py b/pandas/tests/indexes/datetimes/test_arithmetic.py
--- a/pandas/tests/indexes/datetimes/test_arithmetic.py
+++ b/pandas/tests/indexes/datetimes/test_arithmetic.py
@@ -364,6 +364,33 @@ def test_datetimeindex_sub_timestamp_overflow(self):
             with pytest.raises(OverflowError):
                 dtimin - variant
 
+    @pytest.mark.parametrize('names', [('foo', None, None),
+                                       ('baz', 'bar', None),
+                                       ('bar', 'bar', 'bar')])
+    @pytest.mark.parametrize('tz', [None, 'America/Chicago'])
+    def test_dti_add_series(self, tz, names):
+        # GH#13905
+        index = DatetimeIndex(['2016-06-28 05:30', '2016-06-28 05:31'],
+                              tz=tz, name=names[0])
+        ser = Series([Timedelta(seconds=5)] * 2,
+                     index=index, name=names[1])
+        expected = Series(index + Timedelta(seconds=5),
+                          index=index, name=names[2])
+
+        # passing name arg isn't enough when names[2] is None
+        expected.name = names[2]
+        assert expected.dtype == index.dtype
+        result = ser + index
+        tm.assert_series_equal(result, expected)
+        result2 = index + ser
+        tm.assert_series_equal(result2, expected)
+
+        expected = index + Timedelta(seconds=5)
+        result3 = ser.values + index
+        tm.assert_index_equal(result3, expected)
+        result4 = index + ser.values
+        tm.assert_index_equal(result4, expected)
+
     @pytest.mark.parametrize('box', [np.array, pd.Index])
     def test_dti_add_offset_array(self, tz, box):
         # GH#18849