Skip to content

Commit 4749fd6

Browse files
authored
BUG: DataFrame.idxmin/idxmax with mixed dtypes (#38195)
1 parent c184fde commit 4749fd6

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

doc/source/whatsnew/v1.2.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,7 @@ Numeric
620620
- Bug in :class:`DataFrame` allowing arithmetic operations with list of array-likes with undefined results. Behavior changed to raising ``ValueError`` (:issue:`36702`)
621621
- Bug in :meth:`DataFrame.std` with ``timedelta64`` dtype and ``skipna=False`` (:issue:`37392`)
622622
- Bug in :meth:`DataFrame.min` and :meth:`DataFrame.max` with ``datetime64`` dtype and ``skipna=False`` (:issue:`36907`)
623+
- Bug in :meth:`DataFrame.idxmax` and :meth:`DataFrame.idxmin` with mixed dtypes incorrectly raising ``TypeError`` (:issue:`38195`)
623624

624625
Conversion
625626
^^^^^^^^^^

pandas/core/frame.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9001,7 +9001,11 @@ def idxmin(self, axis=0, skipna=True) -> Series:
90019001
dtype: object
90029002
"""
90039003
axis = self._get_axis_number(axis)
9004-
indices = nanops.nanargmin(self.values, axis=axis, skipna=skipna)
9004+
9005+
res = self._reduce(
9006+
nanops.nanargmin, "argmin", axis=axis, skipna=skipna, numeric_only=False
9007+
)
9008+
indices = res._values
90059009

90069010
# indices will always be np.ndarray since axis is not None and
90079011
# values is a 2d array for DataFrame
@@ -9074,7 +9078,11 @@ def idxmax(self, axis=0, skipna=True) -> Series:
90749078
dtype: object
90759079
"""
90769080
axis = self._get_axis_number(axis)
9077-
indices = nanops.nanargmax(self.values, axis=axis, skipna=skipna)
9081+
9082+
res = self._reduce(
9083+
nanops.nanargmax, "argmax", axis=axis, skipna=skipna, numeric_only=False
9084+
)
9085+
indices = res._values
90789086

90799087
# indices will always be np.ndarray since axis is not None and
90809088
# values is a 2d array for DataFrame

pandas/tests/frame/test_reductions.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -969,6 +969,20 @@ def test_idxmax(self, float_frame, int_frame):
969969
with pytest.raises(ValueError, match=msg):
970970
frame.idxmax(axis=2)
971971

972+
def test_idxmax_mixed_dtype(self):
973+
# don't cast to object, which would raise in nanops
974+
dti = pd.date_range("2016-01-01", periods=3)
975+
976+
df = DataFrame({1: [0, 2, 1], 2: range(3)[::-1], 3: dti})
977+
978+
result = df.idxmax()
979+
expected = Series([1, 0, 2], index=[1, 2, 3])
980+
tm.assert_series_equal(result, expected)
981+
982+
result = df.idxmin()
983+
expected = Series([0, 2, 0], index=[1, 2, 3])
984+
tm.assert_series_equal(result, expected)
985+
972986
# ----------------------------------------------------------------------
973987
# Logical reductions
974988

0 commit comments

Comments
 (0)