Skip to content

Commit

Permalink
String dtype: fix alignment sorting in case of python storage (#59448)
Browse files Browse the repository at this point in the history
* String dtype: fix alignment sorting in case of python storage

* add test
  • Loading branch information
jorisvandenbossche authored Aug 8, 2024
1 parent 85821bf commit 3182e9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
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

0 comments on commit 3182e9b

Please sign in to comment.