Skip to content

BUG: idxmin & idxmax axis = 1 str reducer for transform #50329

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 10 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
74 changes: 70 additions & 4 deletions pandas/core/groupby/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1879,16 +1879,82 @@ def func(df):
)
return result

@doc(
_shared_docs["idxmin"],
numeric_only_default="False",
)
def idxmin(
self,
axis: Axis = 0,
skipna: bool = True,
numeric_only: bool = False,
) -> DataFrame:
"""
Return index of first occurrence of minimum over requested axis.

NA/null values are excluded.

Parameters
----------
axis : {{0 or 'index', 1 or 'columns'}}, default 0
The axis to use. 0 or 'index' for row-wise, 1 or 'columns' for column-wise.
The axis argument is ignored, instead we use the grouper's axis.

.. versionchanged:: 1.5.3
Copy link
Member

Choose a reason for hiding this comment

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

2.0.0 here; in generally only regression fixes make it to patch versions.


skipna : bool, default True
Exclude NA/null values. If an entire row/column is NA, the result
will be NA.
numeric_only : bool, default False
Include only `float`, `int` or `boolean` data.

.. versionadded:: 1.5.0

Returns
-------
Series
Indexes of minima along the specified axis.

Raises
------
ValueError
* If the row/column is empty

See Also
--------
Series.idxmin : Return index of the minimum element.

Notes
-----
This method is the DataFrame version of ``ndarray.argmin``.

Examples
--------
Consider a dataset containing food consumption in Argentina.

>>> df = pd.DataFrame({'consumption': [10.51, 103.11, 55.48],
... 'co2_emissions': [37.2, 19.66, 1712]},
... index=['Pork', 'Wheat Products', 'Beef'])

>>> df
consumption co2_emissions
Pork 10.51 37.20
Wheat Products 103.11 19.66
Beef 55.48 1712.00

By default, it returns the index for the minimum value in each column.

>>> df.idxmin()
consumption Pork
co2_emissions Wheat Products
dtype: object

To return the index for the minimum value in each row, use ``axis="columns"``.

>>> df.idxmin(axis="columns")
Pork consumption
Wheat Products co2_emissions
Beef consumption
dtype: object
"""

axis = self.axis
axis = DataFrame._get_axis_number(axis)
Copy link
Member

@rhshadrach rhshadrach Dec 23, 2022

Choose a reason for hiding this comment

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

L1958 here is now unnecessary, as self.axis is always 0 or 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, makes sense


def func(df):
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def test_transform_axis_1_reducer(request, reduction_func):
if reduction_func in (
"corrwith",
"idxmax",
"idxmin",
"ngroup",
"nth",
):
Expand Down