Skip to content

Commit 554d1aa

Browse files
BUG: dtype of DataFrame.idxmax/idxmin incorrect for empty frames (pandas-dev#53296)
* BUG: dtype of DataFrame.idxmax/idxmin incorrect for empty frames * simplify * fix groupby test failures * DOC: Fixing EX01 - Added examples (pandas-dev#53292) * Added examples for Interval and IntervalArray * changed code_checks.sh * forgot these * fix DataFrame.[idxmax|idxmin] --------- Co-authored-by: Dea María Léon <[email protected]>
1 parent 489d28f commit 554d1aa

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

doc/source/whatsnew/v2.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ Reshaping
465465
^^^^^^^^^
466466
- Bug in :func:`crosstab` when ``dropna=False`` would not keep ``np.nan`` in the result (:issue:`10772`)
467467
- Bug in :meth:`DataFrame.agg` and :meth:`Series.agg` on non-unique columns would return incorrect type when dist-like argument passed in (:issue:`51099`)
468+
- Bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax`, where the axis dtype would be lost for empty frames (:issue:`53265`)
468469
- Bug in :meth:`DataFrame.merge` not merging correctly when having ``MultiIndex`` with single level (:issue:`52331`)
469470
- Bug in :meth:`DataFrame.stack` losing extension dtypes when columns is a :class:`MultiIndex` and frame contains mixed dtypes (:issue:`45740`)
470471
- Bug in :meth:`DataFrame.transpose` inferring dtype for object column (:issue:`51546`)

pandas/core/frame.py

+10
Original file line numberDiff line numberDiff line change
@@ -11156,6 +11156,11 @@ def idxmin(
1115611156
self, axis: Axis = 0, skipna: bool = True, numeric_only: bool = False
1115711157
) -> Series:
1115811158
axis = self._get_axis_number(axis)
11159+
11160+
if self.empty and len(self.axes[axis]):
11161+
axis_dtype = self.axes[axis].dtype
11162+
return self._constructor_sliced(dtype=axis_dtype)
11163+
1115911164
if numeric_only:
1116011165
data = self._get_numeric_data()
1116111166
else:
@@ -11180,6 +11185,11 @@ def idxmax(
1118011185
self, axis: Axis = 0, skipna: bool = True, numeric_only: bool = False
1118111186
) -> Series:
1118211187
axis = self._get_axis_number(axis)
11188+
11189+
if self.empty and len(self.axes[axis]):
11190+
axis_dtype = self.axes[axis].dtype
11191+
return self._constructor_sliced(dtype=axis_dtype)
11192+
1118311193
if numeric_only:
1118411194
data = self._get_numeric_data()
1118511195
else:

pandas/tests/frame/test_reductions.py

+24
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,18 @@ def test_idxmin(self, float_frame, int_frame, skipna, axis):
969969
expected = df.apply(Series.idxmin, axis=axis, skipna=skipna)
970970
tm.assert_series_equal(result, expected)
971971

972+
@pytest.mark.parametrize("axis", [0, 1])
973+
def test_idxmin_empty(self, index, skipna, axis):
974+
# GH53265
975+
if axis == 0:
976+
frame = DataFrame(index=index)
977+
else:
978+
frame = DataFrame(columns=index)
979+
980+
result = frame.idxmin(axis=axis, skipna=skipna)
981+
expected = Series(dtype=index.dtype)
982+
tm.assert_series_equal(result, expected)
983+
972984
@pytest.mark.parametrize("numeric_only", [True, False])
973985
def test_idxmin_numeric_only(self, numeric_only):
974986
df = DataFrame({"a": [2, 3, 1], "b": [2, 1, 1], "c": list("xyx")})
@@ -997,6 +1009,18 @@ def test_idxmax(self, float_frame, int_frame, skipna, axis):
9971009
expected = df.apply(Series.idxmax, axis=axis, skipna=skipna)
9981010
tm.assert_series_equal(result, expected)
9991011

1012+
@pytest.mark.parametrize("axis", [0, 1])
1013+
def test_idxmax_empty(self, index, skipna, axis):
1014+
# GH53265
1015+
if axis == 0:
1016+
frame = DataFrame(index=index)
1017+
else:
1018+
frame = DataFrame(columns=index)
1019+
1020+
result = frame.idxmax(axis=axis, skipna=skipna)
1021+
expected = Series(dtype=index.dtype)
1022+
tm.assert_series_equal(result, expected)
1023+
10001024
@pytest.mark.parametrize("numeric_only", [True, False])
10011025
def test_idxmax_numeric_only(self, numeric_only):
10021026
df = DataFrame({"a": [2, 3, 1], "b": [2, 1, 1], "c": list("xyx")})

0 commit comments

Comments
 (0)