Skip to content

Commit 3182e9b

Browse files
String dtype: fix alignment sorting in case of python storage (#59448)
* String dtype: fix alignment sorting in case of python storage * add test
1 parent 85821bf commit 3182e9b

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

pandas/core/indexes/base.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -4884,7 +4884,10 @@ def _can_use_libjoin(self) -> bool:
48844884
return (
48854885
isinstance(self.dtype, np.dtype)
48864886
or isinstance(self._values, (ArrowExtensionArray, BaseMaskedArray))
4887-
or self.dtype == "string[python]"
4887+
or (
4888+
isinstance(self.dtype, StringDtype)
4889+
and self.dtype.storage == "python"
4890+
)
48884891
)
48894892
# Exclude index types where the conversion to numpy converts to object dtype,
48904893
# which negates the performance benefit of libjoin

pandas/tests/series/methods/test_align.py

+13
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ def test_align_periodindex(join_type):
148148
ts.align(ts[::2], join=join_type)
149149

150150

151+
def test_align_stringindex(any_string_dtype):
152+
left = Series(range(3), index=pd.Index(["a", "b", "d"], dtype=any_string_dtype))
153+
right = Series(range(3), index=pd.Index(["a", "b", "c"], dtype=any_string_dtype))
154+
result_left, result_right = left.align(right)
155+
156+
expected_idx = pd.Index(["a", "b", "c", "d"], dtype=any_string_dtype)
157+
expected_left = Series([0, 1, np.nan, 2], index=expected_idx)
158+
expected_right = Series([0, 1, 2, np.nan], index=expected_idx)
159+
160+
tm.assert_series_equal(result_left, expected_left)
161+
tm.assert_series_equal(result_right, expected_right)
162+
163+
151164
def test_align_left_fewer_levels():
152165
# GH#45224
153166
left = Series([2], index=pd.MultiIndex.from_tuples([(1, 3)], names=["a", "c"]))

0 commit comments

Comments
 (0)