Skip to content

Commit e5d508e

Browse files
Backport PR #54778 on branch 2.1.x (REGR: Index.union loses python string dtype) (#54813)
Backport PR #54778: REGR: Index.union loses python string dtype Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 4daed68 commit e5d508e

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pandas/core/indexes/base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5204,7 +5204,7 @@ def _from_join_target(self, result: np.ndarray) -> ArrayLike:
52045204
"""
52055205
if isinstance(self.values, BaseMaskedArray):
52065206
return type(self.values)(result, np.zeros(result.shape, dtype=np.bool_))
5207-
elif isinstance(self.values, ArrowExtensionArray):
5207+
elif isinstance(self.values, (ArrowExtensionArray, StringArray)):
52085208
return type(self.values)._from_sequence(result)
52095209
return result
52105210

pandas/tests/indexes/test_setops.py

+7
Original file line numberDiff line numberDiff line change
@@ -899,3 +899,10 @@ def test_union_ea_dtypes(self, any_numeric_ea_and_arrow_dtype):
899899
result = idx.union(idx2)
900900
expected = Index([1, 2, 3, 4, 5], dtype=any_numeric_ea_and_arrow_dtype)
901901
tm.assert_index_equal(result, expected)
902+
903+
def test_union_string_array(self, any_string_dtype):
904+
idx1 = Index(["a"], dtype=any_string_dtype)
905+
idx2 = Index(["b"], dtype=any_string_dtype)
906+
result = idx1.union(idx2)
907+
expected = Index(["a", "b"], dtype=any_string_dtype)
908+
tm.assert_index_equal(result, expected)

0 commit comments

Comments
 (0)