Skip to content

Commit 4467c2f

Browse files
topper-123DeaMariaLeon
andauthoredMay 22, 2023
BUG: dtype of DataFrame.idxmax/idxmin incorrect for empty frames (#53296)
* BUG: dtype of DataFrame.idxmax/idxmin incorrect for empty frames * simplify * fix groupby test failures * DOC: Fixing EX01 - Added examples (#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 1e61215 commit 4467c2f

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
@@ -432,6 +432,7 @@ Reshaping
432432
^^^^^^^^^
433433
- Bug in :func:`crosstab` when ``dropna=False`` would not keep ``np.nan`` in the result (:issue:`10772`)
434434
- 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`)
435+
- Bug in :meth:`DataFrame.idxmin` and :meth:`DataFrame.idxmax`, where the axis dtype would be lost for empty frames (:issue:`53265`)
435436
- Bug in :meth:`DataFrame.merge` not merging correctly when having ``MultiIndex`` with single level (:issue:`52331`)
436437
- Bug in :meth:`DataFrame.stack` losing extension dtypes when columns is a :class:`MultiIndex` and frame contains mixed dtypes (:issue:`45740`)
437438
- 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:
@@ -11181,6 +11186,11 @@ def idxmax(
1118111186
self, axis: Axis = 0, skipna: bool = True, numeric_only: bool = False
1118211187
) -> Series:
1118311188
axis = self._get_axis_number(axis)
11189+
11190+
if self.empty and len(self.axes[axis]):
11191+
axis_dtype = self.axes[axis].dtype
11192+
return self._constructor_sliced(dtype=axis_dtype)
11193+
1118411194
if numeric_only:
1118511195
data = self._get_numeric_data()
1118611196
else:

‎pandas/tests/frame/test_reductions.py

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

967+
@pytest.mark.parametrize("axis", [0, 1])
968+
def test_idxmin_empty(self, index, skipna, axis):
969+
# GH53265
970+
if axis == 0:
971+
frame = DataFrame(index=index)
972+
else:
973+
frame = DataFrame(columns=index)
974+
975+
result = frame.idxmin(axis=axis, skipna=skipna)
976+
expected = Series(dtype=index.dtype)
977+
tm.assert_series_equal(result, expected)
978+
967979
@pytest.mark.parametrize("numeric_only", [True, False])
968980
def test_idxmin_numeric_only(self, numeric_only):
969981
df = DataFrame({"a": [2, 3, 1], "b": [2, 1, 1], "c": list("xyx")})
@@ -992,6 +1004,18 @@ def test_idxmax(self, float_frame, int_frame, skipna, axis):
9921004
expected = df.apply(Series.idxmax, axis=axis, skipna=skipna)
9931005
tm.assert_series_equal(result, expected)
9941006

1007+
@pytest.mark.parametrize("axis", [0, 1])
1008+
def test_idxmax_empty(self, index, skipna, axis):
1009+
# GH53265
1010+
if axis == 0:
1011+
frame = DataFrame(index=index)
1012+
else:
1013+
frame = DataFrame(columns=index)
1014+
1015+
result = frame.idxmax(axis=axis, skipna=skipna)
1016+
expected = Series(dtype=index.dtype)
1017+
tm.assert_series_equal(result, expected)
1018+
9951019
@pytest.mark.parametrize("numeric_only", [True, False])
9961020
def test_idxmax_numeric_only(self, numeric_only):
9971021
df = DataFrame({"a": [2, 3, 1], "b": [2, 1, 1], "c": list("xyx")})

0 commit comments

Comments
 (0)
Please sign in to comment.