From a4cb637712600cd2bc4cfd79a9f5fefbef113c00 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 20 Sep 2020 11:07:33 -0700 Subject: [PATCH 1/3] BUG: alignment changing index on input series --- pandas/core/generic.py | 8 ++++++++ pandas/tests/series/test_arithmetic.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 0b9021b094cd7..594ce87025d52 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8748,6 +8748,10 @@ def _align_frame( if is_datetime64tz_dtype(left.index.dtype): if left.index.tz != right.index.tz: if join_index is not None: + # GH#33671 ensure we don't change the index on + # our original Series + left = left.copy(deep=False) + right = right.copy(deep=False) left.index = join_index right.index = join_index @@ -8835,6 +8839,10 @@ def _align_series( if is_datetime64tz_dtype(left.index.dtype): if left.index.tz != right.index.tz: if join_index is not None: + # GH#33671 ensure we don't change the index on + # our original Series + left = left.copy(deep=False) + right = right.copy(deep=False) left.index = join_index right.index = join_index diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 8fad6ee1cca8b..f30246ff12fac 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -254,6 +254,19 @@ def test_sub_datetimelike_align(self): result = (dt2.to_frame() - dt.to_frame())[0] tm.assert_series_equal(result, expected) + def test_alignment_doesnt_change_tz(self): + # GH#33671 + dti = pd.date_range("2016-01-01", periods=10, tz="CET") + dti_utc = dti.tz_convert("UTC") + ser = pd.Series(10, index=dti) + ser_utc = pd.Series(10, index=dti_utc) + + # we don't care about the result, just that original indexes are unchanged + ser * ser_utc + + assert ser.index is dti + assert ser_utc.index is dti_utc + # ------------------------------------------------------------------ # Comparisons From 57d81f49487f92abcebd8d8c9a222b57e0094675 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 21 Sep 2020 16:42:36 -0700 Subject: [PATCH 2/3] whatsnew --- doc/source/whatsnew/v1.2.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 19a563be0a568..4ebd60b5d658c 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -272,6 +272,7 @@ Numeric - Bug in :func:`to_numeric` where float precision was incorrect (:issue:`31364`) - Bug in :meth:`DataFrame.any` with ``axis=1`` and ``bool_only=True`` ignoring the ``bool_only`` keyword (:issue:`32432`) - Bug in :meth:`Series.equals` where a ``ValueError`` was raised when numpy arrays were compared to scalars (:issue:`35267`) +- Bug in :class:`Series` where two :class:`Series` each have a :class:`DatetimeIndex` with different timezones having those indexes incorrectly changed when performing arithmetic operations (:issue:`33671`) - Conversion From edd8440e445c3e7c86777cbc84da619a7533342b Mon Sep 17 00:00:00 2001 From: Brock Date: Wed, 23 Sep 2020 18:40:25 -0700 Subject: [PATCH 3/3] remove deep=False --- pandas/core/generic.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 594ce87025d52..a8b48f875c825 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -8749,9 +8749,9 @@ def _align_frame( if left.index.tz != right.index.tz: if join_index is not None: # GH#33671 ensure we don't change the index on - # our original Series - left = left.copy(deep=False) - right = right.copy(deep=False) + # our original Series (NB: by default deep=False) + left = left.copy() + right = right.copy() left.index = join_index right.index = join_index @@ -8840,9 +8840,9 @@ def _align_series( if left.index.tz != right.index.tz: if join_index is not None: # GH#33671 ensure we don't change the index on - # our original Series - left = left.copy(deep=False) - right = right.copy(deep=False) + # our original Series (NB: by default deep=False) + left = left.copy() + right = right.copy() left.index = join_index right.index = join_index