Skip to content

BUG: GroupBy.idxmax/idxmin with EA dtypes #38733

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 11 commits into from
Jan 19, 2021
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ Groupby/resample/rolling
- Fixed bug in :meth:`DataFrameGroupBy.sum` and :meth:`SeriesGroupBy.sum` causing loss of precision through using Kahan summation (:issue:`38778`)
- Fixed bug in :meth:`DataFrameGroupBy.cumsum`, :meth:`SeriesGroupBy.cumsum`, :meth:`DataFrameGroupBy.mean` and :meth:`SeriesGroupBy.mean` causing loss of precision through using Kahan summation (:issue:`38934`)
- Bug in :meth:`.Resampler.aggregate` and :meth:`DataFrame.transform` raising ``TypeError`` instead of ``SpecificationError`` when missing keys had mixed dtypes (:issue:`39025`)
- Bug in :meth:`GroupBy.idxmin` and :meth:`GroupBy.idxmax` with ``ExtendionDtype`` columns (:issue:`38733`)

Reshaping
^^^^^^^^^
Expand Down
12 changes: 9 additions & 3 deletions pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from pandas.compat.numpy import function as nv
from pandas.errors import AbstractMethodError
from pandas.util._decorators import Appender, Substitution
from pandas.util._validators import validate_fillna_kwargs
from pandas.util._validators import validate_bool_kwarg, validate_fillna_kwargs

from pandas.core.dtypes.cast import maybe_cast_to_extension_array
from pandas.core.dtypes.common import (
Expand Down Expand Up @@ -596,7 +596,7 @@ def argsort(
mask=np.asarray(self.isna()),
)

def argmin(self):
def argmin(self, skipna: bool = True) -> int:
"""
Return the index of minimum value.

Expand All @@ -611,9 +611,12 @@ def argmin(self):
--------
ExtensionArray.argmax
"""
validate_bool_kwarg(skipna, "skipna")
if not skipna and self.isna().any():
raise NotImplementedError
return nargminmax(self, "argmin")

def argmax(self):
def argmax(self, skipna: bool = True) -> int:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you update the Parameters section here (and for argmin)

"""
Return the index of maximum value.

Expand All @@ -628,6 +631,9 @@ def argmax(self):
--------
ExtensionArray.argmin
"""
validate_bool_kwarg(skipna, "skipna")
if not skipna and self.isna().any():
raise NotImplementedError
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a test that hits this (and argmin)

return nargminmax(self, "argmax")

def fillna(self, value=None, method=None, limit=None):
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,10 +531,16 @@ def test_idxmin_idxmax_returns_int_types(func, values):
}
)
df["c_date"] = pd.to_datetime(df["c_date"])
df["c_date_tz"] = df["c_date"].dt.tz_localize("US/Pacific")
df["c_timedelta"] = df["c_date"] - df["c_date"].iloc[0]
df["c_period"] = df["c_date"].dt.to_period("W")

result = getattr(df.groupby("name"), func)()

expected = DataFrame(values, index=Index(["A", "B"], name="name"))
expected["c_date_tz"] = expected["c_date"]
expected["c_timedelta"] = expected["c_date"]
expected["c_period"] = expected["c_date"]

tm.assert_frame_equal(result, expected)

Expand Down