Skip to content

Commit bb2d2e0

Browse files
authored
BUG: mode not sorting values for arrow backed strings (#55621)
* BUG: mode not sorting values for arrow backed strings * Fix tests * Change to pa_installed variable * Update pyarrow.py * Fix * Fix
1 parent e48df1c commit bb2d2e0

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

doc/source/whatsnew/v2.1.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Bug fixes
3131
- Fixed bug in :meth:`Index.insert` raising when inserting ``None`` into :class:`Index` with ``dtype="string[pyarrow_numpy]"`` (:issue:`55365`)
3232
- Fixed bug in :meth:`Series.all` and :meth:`Series.any` not treating missing values correctly for ``dtype="string[pyarrow_numpy]"`` (:issue:`55367`)
3333
- Fixed bug in :meth:`Series.floordiv` for :class:`ArrowDtype` (:issue:`55561`)
34+
- Fixed bug in :meth:`Series.mode` not sorting values for arrow backed string dtype (:issue:`55621`)
3435
- Fixed bug in :meth:`Series.rank` for ``string[pyarrow_numpy]`` dtype (:issue:`55362`)
3536
- Fixed bug in :meth:`Series.str.extractall` for :class:`ArrowDtype` dtype being converted to object (:issue:`53846`)
3637
- Fixed bug where PDEP-6 warning about setting an item of an incompatible dtype was being shown when creating a new conditional column (:issue:`55025`)

pandas/core/arrays/arrow/array.py

+1
Original file line numberDiff line numberDiff line change
@@ -1919,6 +1919,7 @@ def _mode(self, dropna: bool = True) -> Self:
19191919
if pa.types.is_temporal(pa_type):
19201920
most_common = most_common.cast(pa_type)
19211921

1922+
most_common = most_common.take(pc.array_sort_indices(most_common))
19221923
return type(self)(most_common)
19231924

19241925
def _maybe_convert_setitem_value(self, value):

pandas/tests/extension/test_arrow.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,7 @@ def test_quantile(data, interpolation, quantile, request):
13331333

13341334
@pytest.mark.parametrize(
13351335
"take_idx, exp_idx",
1336-
[[[0, 0, 2, 2, 4, 4], [0, 4]], [[0, 0, 0, 2, 4, 4], [0]]],
1336+
[[[0, 0, 2, 2, 4, 4], [4, 0]], [[0, 0, 0, 2, 4, 4], [0]]],
13371337
ids=["multi_mode", "single_mode"],
13381338
)
13391339
def test_mode_dropna_true(data_for_grouping, take_idx, exp_idx):
@@ -1351,7 +1351,7 @@ def test_mode_dropna_false_mode_na(data):
13511351
expected = pd.Series([None], dtype=data.dtype)
13521352
tm.assert_series_equal(result, expected)
13531353

1354-
expected = pd.Series([None, data[0]], dtype=data.dtype)
1354+
expected = pd.Series([data[0], None], dtype=data.dtype)
13551355
result = expected.mode(dropna=False)
13561356
tm.assert_series_equal(result, expected)
13571357

pandas/tests/groupby/test_groupby.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
PerformanceWarning,
1010
SpecificationError,
1111
)
12+
import pandas.util._test_decorators as td
1213

1314
import pandas as pd
1415
from pandas import (
@@ -2541,7 +2542,7 @@ def test_groupby_column_index_name_lost(func):
25412542
"infer_string",
25422543
[
25432544
False,
2544-
True,
2545+
pytest.param(True, marks=td.skip_if_no("pyarrow")),
25452546
],
25462547
)
25472548
def test_groupby_duplicate_columns(infer_string):
@@ -2773,13 +2774,20 @@ def test_rolling_wrong_param_min_period():
27732774
test_df.groupby("name")["val"].rolling(window=2, min_period=1).sum()
27742775

27752776

2776-
def test_by_column_values_with_same_starting_value():
2777+
@pytest.mark.parametrize(
2778+
"dtype",
2779+
[
2780+
object,
2781+
pytest.param("string[pyarrow_numpy]", marks=td.skip_if_no("pyarrow")),
2782+
],
2783+
)
2784+
def test_by_column_values_with_same_starting_value(dtype):
27772785
# GH29635
27782786
df = DataFrame(
27792787
{
27802788
"Name": ["Thomas", "Thomas", "Thomas John"],
27812789
"Credit": [1200, 1300, 900],
2782-
"Mood": ["sad", "happy", "happy"],
2790+
"Mood": Series(["sad", "happy", "happy"], dtype=dtype),
27832791
}
27842792
)
27852793
aggregate_details = {"Mood": Series.mode, "Credit": "sum"}

0 commit comments

Comments
 (0)