Skip to content

Commit 498917e

Browse files
author
luke
committed
Less code way
1 parent ee4b751 commit 498917e

File tree

1 file changed

+12
-43
lines changed

1 file changed

+12
-43
lines changed

pandas/core/groupby/generic.py

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,34 +1080,13 @@ def nsmallest(
10801080

10811081
@doc(Series.idxmin.__doc__)
10821082
def idxmin(self, axis: Axis = 0, skipna: bool = True) -> Series | DataFrame:
1083-
def func(df):
1084-
return df.idxmin(axis=axis, skipna=skipna)
1085-
1086-
func.__name__ = "idxmin"
1087-
return self._idxmin_idxmax(func)
1083+
result = self._op_via_apply("idxmax", axis=axis, skipna=skipna)
1084+
return result.astype(self._obj_with_exclusions.index.dtype)
10881085

10891086
@doc(Series.idxmax.__doc__)
10901087
def idxmax(self, axis: Axis = 0, skipna: bool = True) -> Series | DataFrame:
1091-
def func(df):
1092-
return df.idxmax(axis=axis, skipna=skipna)
1093-
1094-
func.__name__ = "idxmax"
1095-
return self._idxmin_idxmax(func)
1096-
1097-
def _idxmin_idxmax(self, func: Callable) -> DataFrame | Series:
1098-
result, _ = self.grouper.apply(func, self._obj_with_exclusions, self.axis)
1099-
1100-
if len(result) == 0:
1101-
return self.obj._constructor(
1102-
index=self.grouper.result_index,
1103-
name=self._obj_with_exclusions.name,
1104-
dtype=self._obj_with_exclusions.index.dtype,
1105-
)
1106-
1107-
else:
1108-
return self._wrap_applied_output(
1109-
self._obj_with_exclusions, result, not_indexed_same=True
1110-
)
1088+
result = self._op_via_apply("idxmin", axis=axis, skipna=skipna)
1089+
return result.astype(self._obj_with_exclusions.index.dtype)
11111090

11121091
@doc(Series.corr.__doc__)
11131092
def corr(
@@ -2037,8 +2016,10 @@ def idxmax(
20372016
def func(df):
20382017
return df.idxmax(axis=axis, skipna=skipna, numeric_only=numeric_only)
20392018

2040-
func.__name__ = "idxmax"
2041-
return self._idxmin_idxmax(func)
2019+
result = self._python_apply_general(
2020+
func, self._obj_with_exclusions, not_indexed_same=True
2021+
)
2022+
return result.astype(self._obj_with_exclusions.index.dtype, copy=False)
20422023

20432024
def idxmin(
20442025
self,
@@ -2120,25 +2101,13 @@ def idxmin(
21202101
def func(df):
21212102
return df.idxmin(axis=axis, skipna=skipna, numeric_only=numeric_only)
21222103

2123-
func.__name__ = "idxmin"
2124-
return self._idxmin_idxmax(func)
2104+
result = self._python_apply_general(
2105+
func, self._obj_with_exclusions, not_indexed_same=True
2106+
)
2107+
return result.astype(self._obj_with_exclusions.index.dtype, copy=False)
21252108

21262109
boxplot = boxplot_frame_groupby
21272110

2128-
def _idxmin_idxmax(self, func: Callable) -> DataFrame:
2129-
result, _ = self.grouper.apply(func, self._obj_with_exclusions, self.axis)
2130-
2131-
if len(result) == 0:
2132-
return self.obj._constructor(
2133-
index=self.grouper.result_index,
2134-
columns=self._obj_with_exclusions.columns,
2135-
dtype=self._obj_with_exclusions.index.dtype,
2136-
)
2137-
else:
2138-
return self._wrap_applied_output(
2139-
self._obj_with_exclusions, result, not_indexed_same=True
2140-
)
2141-
21422111
def value_counts(
21432112
self,
21442113
subset: Sequence[Hashable] | None = None,

0 commit comments

Comments
 (0)