Skip to content

REGR: Index.union loses python string dtype #54778

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 2 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5194,7 +5194,7 @@ def _from_join_target(self, result: np.ndarray) -> ArrayLike:
"""
if isinstance(self.values, BaseMaskedArray):
return type(self.values)(result, np.zeros(result.shape, dtype=np.bool_))
elif isinstance(self.values, ArrowExtensionArray):
elif isinstance(self.values, (ArrowExtensionArray, StringArray)):
Copy link
Member

Choose a reason for hiding this comment

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

just out of interest, why can't this be done for all EAs?

Copy link
Member Author

Choose a reason for hiding this comment

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

We enhanced this when we added _get_join_target. Previously we returned result unchanged, so not sure if this would potentially break stuff

Copy link
Member

Choose a reason for hiding this comment

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

ok - this looks fine to be, but would prefer if someone more familiar with core/indexes could take a look too

return type(self.values)._from_sequence(result)
return result

Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/test_setops.py
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,10 @@ def test_union_ea_dtypes(self, any_numeric_ea_and_arrow_dtype):
result = idx.union(idx2)
expected = Index([1, 2, 3, 4, 5], dtype=any_numeric_ea_and_arrow_dtype)
tm.assert_index_equal(result, expected)

def test_union_string_array(self, any_string_dtype):
idx1 = Index(["a"], dtype=any_string_dtype)
idx2 = Index(["b"], dtype=any_string_dtype)
result = idx1.union(idx2)
expected = Index(["a", "b"], dtype=any_string_dtype)
tm.assert_index_equal(result, expected)