Skip to content

Conversation

@deniskrds
Copy link

@deniskrds deniskrds commented Nov 22, 2025

…n) (#63169)

Previously, Index.intersection and Index.union (including MultiIndex implementations) would return a direct reference to self (or other) if the result was identical to the input. This allowed side effects where mutating the metadata (e.g. .name) of the result would corrupt the original index.

This change forces a shallow copy (deep=False) in these cases. This preserves the performance benefit of sharing the underlying data array while ensuring the Index container itself is a distinct object with independent metadata.

@deniskrds deniskrds force-pushed the fix-index-setops-mutation branch from 88fecc0 to 3e9186d Compare November 23, 2025 10:05
@rhshadrach rhshadrach added Bug Index Related to the Index class or subclasses setops union, intersection, difference, symmetric_difference labels Dec 7, 2025
Copy link
Member

@rhshadrach rhshadrach left a comment

Choose a reason for hiding this comment

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

Looking good, would like a 2nd eye on this. cc @jbrockmendel

tm.assert_index_equal(res, expected)


class TestSetOpsMutation:
Copy link
Member

Choose a reason for hiding this comment

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

We'd like to move away from having test classes in cases where the class itself isn't useful. Can you just make these functions.

Copy link
Author

Choose a reason for hiding this comment

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

Got it. I'll convert them to functions shortly.

@deniskrds deniskrds requested a review from rhshadrach December 8, 2025 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Index Related to the Index class or subclasses setops union, intersection, difference, symmetric_difference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Index.intersection returns reference instead of new instance

2 participants