Skip to content

Commit 5b07d7c

Browse files
phoflmroeschke
andauthored
REGR: Index.union loses python string dtype (#54778)
* REGR: Index.union loses python string dtype * Update pandas/core/indexes/base.py Co-authored-by: Matthew Roeschke <[email protected]> --------- Co-authored-by: Matthew Roeschke <[email protected]>
1 parent 634096f commit 5b07d7c

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
@@ -5194,7 +5194,7 @@ def _from_join_target(self, result: np.ndarray) -> ArrayLike:
51945194
"""
51955195
if isinstance(self.values, BaseMaskedArray):
51965196
return type(self.values)(result, np.zeros(result.shape, dtype=np.bool_))
5197-
elif isinstance(self.values, ArrowExtensionArray):
5197+
elif isinstance(self.values, (ArrowExtensionArray, StringArray)):
51985198
return type(self.values)._from_sequence(result)
51995199
return result
52005200

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)