Skip to content

BUG: DataFrame.idxmin/idxmax with mixed dtypes #38195

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Dec 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ Numeric
- Bug in :class:`DataFrame` allowing arithmetic operations with list of array-likes with undefined results. Behavior changed to raising ``ValueError`` (:issue:`36702`)
- Bug in :meth:`DataFrame.std` with ``timedelta64`` dtype and ``skipna=False`` (:issue:`37392`)
- Bug in :meth:`DataFrame.min` and :meth:`DataFrame.max` with ``datetime64`` dtype and ``skipna=False`` (:issue:`36907`)
- Bug in :meth:`DataFrame.idxmax` and :meth:`DataFrame.idxmin` with mixed dtypes incorrectly raising ``TypeError`` (:issue:`38195`)

Conversion
^^^^^^^^^^
Expand Down
12 changes: 10 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9001,7 +9001,11 @@ def idxmin(self, axis=0, skipna=True) -> Series:
dtype: object
"""
axis = self._get_axis_number(axis)
indices = nanops.nanargmin(self.values, axis=axis, skipna=skipna)

res = self._reduce(
nanops.nanargmin, "argmin", axis=axis, skipna=skipna, numeric_only=False
)
indices = res._values

# indices will always be np.ndarray since axis is not None and
# values is a 2d array for DataFrame
Expand Down Expand Up @@ -9074,7 +9078,11 @@ def idxmax(self, axis=0, skipna=True) -> Series:
dtype: object
"""
axis = self._get_axis_number(axis)
indices = nanops.nanargmax(self.values, axis=axis, skipna=skipna)

res = self._reduce(
nanops.nanargmax, "argmax", axis=axis, skipna=skipna, numeric_only=False
)
indices = res._values

# indices will always be np.ndarray since axis is not None and
# values is a 2d array for DataFrame
Expand Down
14 changes: 14 additions & 0 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,20 @@ def test_idxmax(self, float_frame, int_frame):
with pytest.raises(ValueError, match=msg):
frame.idxmax(axis=2)

def test_idxmax_mixed_dtype(self):
# don't cast to object, which would raise in nanops
dti = pd.date_range("2016-01-01", periods=3)

df = DataFrame({1: [0, 2, 1], 2: range(3)[::-1], 3: dti})

result = df.idxmax()
expected = Series([1, 0, 2], index=[1, 2, 3])
tm.assert_series_equal(result, expected)

result = df.idxmin()
expected = Series([0, 2, 0], index=[1, 2, 3])
tm.assert_series_equal(result, expected)

# ----------------------------------------------------------------------
# Logical reductions

Expand Down