Skip to content

Commit 7e8398c

Browse files
authored
Backport PR #55368 on branch 2.1.x (BUG: idxmin/max raising for arrow dtypes) (#55377)
BUG: idxmin/max raising for arrow dtypes (#55368) (cherry picked from commit 59616c5)
1 parent 78a5500 commit 7e8398c

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

doc/source/whatsnew/v2.1.2.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Fixed regressions
2121

2222
Bug fixes
2323
~~~~~~~~~
24-
-
24+
- Fixed bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax` raising for arrow dtypes (:issue:`55368`)
2525
-
2626

2727
.. ---------------------------------------------------------------------------

pandas/core/arrays/arrow/array.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from pandas.util._decorators import doc
3131
from pandas.util._validators import validate_fillna_kwargs
3232

33+
from pandas.core.dtypes.cast import infer_dtype_from_scalar
3334
from pandas.core.dtypes.common import (
3435
is_array_like,
3536
is_bool_dtype,
@@ -1595,13 +1596,21 @@ def _reduce(
15951596
pa_result = self._reduce_pyarrow(name, skipna=skipna, **kwargs)
15961597

15971598
if keepdims:
1598-
result = pa.array([pa_result.as_py()], type=pa_result.type)
1599+
if isinstance(pa_result, pa.Scalar):
1600+
result = pa.array([pa_result.as_py()], type=pa_result.type)
1601+
else:
1602+
result = pa.array(
1603+
[pa_result],
1604+
type=to_pyarrow_type(infer_dtype_from_scalar(pa_result)[0]),
1605+
)
15991606
return type(self)(result)
16001607

16011608
if pc.is_null(pa_result).as_py():
16021609
return self.dtype.na_value
1603-
else:
1610+
elif isinstance(pa_result, pa.Scalar):
16041611
return pa_result.as_py()
1612+
else:
1613+
return pa_result
16051614

16061615
def _explode(self):
16071616
"""

pandas/tests/frame/test_reductions.py

+13
Original file line numberDiff line numberDiff line change
@@ -1056,6 +1056,19 @@ def test_idxmax_numeric_only(self, numeric_only):
10561056
expected = Series([1, 0, 1], index=["a", "b", "c"])
10571057
tm.assert_series_equal(result, expected)
10581058

1059+
def test_idxmax_arrow_types(self):
1060+
# GH#55368
1061+
pytest.importorskip("pyarrow")
1062+
1063+
df = DataFrame({"a": [2, 3, 1], "b": [2, 1, 1]}, dtype="int64[pyarrow]")
1064+
result = df.idxmax()
1065+
expected = Series([1, 0], index=["a", "b"])
1066+
tm.assert_series_equal(result, expected)
1067+
1068+
result = df.idxmin()
1069+
expected = Series([2, 1], index=["a", "b"])
1070+
tm.assert_series_equal(result, expected)
1071+
10591072
def test_idxmax_axis_2(self, float_frame):
10601073
frame = float_frame
10611074
msg = "No axis named 2 for object type DataFrame"

0 commit comments

Comments
 (0)