Skip to content

BUG: alignment changing index on input series #36503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 25, 2020
8 changes: 8 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to deep copy, just .copy() as that copies the data; here we just need to avoid mutating the index, no?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isnt that what deep=False means?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh woa, yes, i would remove the param then, its confusing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you can remove here and above the deep keyword

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i prefer to be explicit about this being non-deep

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would prefer to just use copy, this is not the pattern we use; your comment is explanatory.

right = right.copy(deep=False)
left.index = join_index
right.index = join_index

Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down