Skip to content

String dtype: fix alignment sorting in case of python storage #59448

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
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
5 changes: 4 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4884,7 +4884,10 @@ def _can_use_libjoin(self) -> bool:
return (
isinstance(self.dtype, np.dtype)
or isinstance(self._values, (ArrowExtensionArray, BaseMaskedArray))
or self.dtype == "string[python]"
or (
isinstance(self.dtype, StringDtype)
and self.dtype.storage == "python"
)
)
# Exclude index types where the conversion to numpy converts to object dtype,
# which negates the performance benefit of libjoin
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/series/methods/test_align.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,19 @@ def test_align_periodindex(join_type):
ts.align(ts[::2], join=join_type)


def test_align_stringindex(any_string_dtype):
left = Series(range(3), index=pd.Index(["a", "b", "d"], dtype=any_string_dtype))
right = Series(range(3), index=pd.Index(["a", "b", "c"], dtype=any_string_dtype))
result_left, result_right = left.align(right)

expected_idx = pd.Index(["a", "b", "c", "d"], dtype=any_string_dtype)
expected_left = Series([0, 1, np.nan, 2], index=expected_idx)
expected_right = Series([0, 1, 2, np.nan], index=expected_idx)

tm.assert_series_equal(result_left, expected_left)
tm.assert_series_equal(result_right, expected_right)


def test_align_left_fewer_levels():
# GH#45224
left = Series([2], index=pd.MultiIndex.from_tuples([(1, 3)], names=["a", "c"]))
Expand Down