Skip to content

Commit 40d3b5f

Browse files
authored
BUG: alignment changing index on input series (pandas-dev#36503)
* BUG: alignment changing index on input series * whatsnew * remove deep=False
1 parent 5895978 commit 40d3b5f

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

doc/source/whatsnew/v1.2.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ Numeric
272272
- Bug in :func:`to_numeric` where float precision was incorrect (:issue:`31364`)
273273
- Bug in :meth:`DataFrame.any` with ``axis=1`` and ``bool_only=True`` ignoring the ``bool_only`` keyword (:issue:`32432`)
274274
- Bug in :meth:`Series.equals` where a ``ValueError`` was raised when numpy arrays were compared to scalars (:issue:`35267`)
275+
- 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`)
275276
-
276277

277278
Conversion

pandas/core/generic.py

+8
Original file line numberDiff line numberDiff line change
@@ -8748,6 +8748,10 @@ def _align_frame(
87488748
if is_datetime64tz_dtype(left.index.dtype):
87498749
if left.index.tz != right.index.tz:
87508750
if join_index is not None:
8751+
# GH#33671 ensure we don't change the index on
8752+
# our original Series (NB: by default deep=False)
8753+
left = left.copy()
8754+
right = right.copy()
87518755
left.index = join_index
87528756
right.index = join_index
87538757

@@ -8835,6 +8839,10 @@ def _align_series(
88358839
if is_datetime64tz_dtype(left.index.dtype):
88368840
if left.index.tz != right.index.tz:
88378841
if join_index is not None:
8842+
# GH#33671 ensure we don't change the index on
8843+
# our original Series (NB: by default deep=False)
8844+
left = left.copy()
8845+
right = right.copy()
88388846
left.index = join_index
88398847
right.index = join_index
88408848

pandas/tests/series/test_arithmetic.py

+13
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,19 @@ def test_sub_datetimelike_align(self):
254254
result = (dt2.to_frame() - dt.to_frame())[0]
255255
tm.assert_series_equal(result, expected)
256256

257+
def test_alignment_doesnt_change_tz(self):
258+
# GH#33671
259+
dti = pd.date_range("2016-01-01", periods=10, tz="CET")
260+
dti_utc = dti.tz_convert("UTC")
261+
ser = pd.Series(10, index=dti)
262+
ser_utc = pd.Series(10, index=dti_utc)
263+
264+
# we don't care about the result, just that original indexes are unchanged
265+
ser * ser_utc
266+
267+
assert ser.index is dti
268+
assert ser_utc.index is dti_utc
269+
257270

258271
# ------------------------------------------------------------------
259272
# Comparisons

0 commit comments

Comments
 (0)