Skip to content

Commit 170b439

Browse files
authored
CLN: trim unreachable groupby paths (#41361)
1 parent e927af2 commit 170b439

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

pandas/core/groupby/generic.py

+25-30
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,11 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
266266
func = maybe_mangle_lambdas(func)
267267
ret = self._aggregate_multiple_funcs(func)
268268
if relabeling:
269-
ret.columns = columns
269+
# error: Incompatible types in assignment (expression has type
270+
# "Optional[List[str]]", variable has type "Index")
271+
ret.columns = columns # type: ignore[assignment]
272+
return ret
273+
270274
else:
271275
cyfunc = com.get_cython_func(func)
272276
if cyfunc and not args and not kwargs:
@@ -282,33 +286,21 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
282286
# see test_groupby.test_basic
283287
result = self._aggregate_named(func, *args, **kwargs)
284288

285-
index = Index(sorted(result), name=self.grouper.names[0])
286-
ret = create_series_with_explicit_dtype(
287-
result, index=index, dtype_if_empty=object
288-
)
289-
290-
if not self.as_index: # pragma: no cover
291-
print("Warning, ignoring as_index=True")
292-
293-
if isinstance(ret, dict):
294-
from pandas import concat
295-
296-
ret = concat(ret.values(), axis=1, keys=[key.label for key in ret.keys()])
297-
return ret
289+
index = Index(sorted(result), name=self.grouper.names[0])
290+
return create_series_with_explicit_dtype(
291+
result, index=index, dtype_if_empty=object
292+
)
298293

299294
agg = aggregate
300295

301-
def _aggregate_multiple_funcs(self, arg):
296+
def _aggregate_multiple_funcs(self, arg) -> DataFrame:
302297
if isinstance(arg, dict):
303298

304299
# show the deprecation, but only if we
305300
# have not shown a higher level one
306301
# GH 15931
307-
if isinstance(self._selected_obj, Series):
308-
raise SpecificationError("nested renamer is not supported")
302+
raise SpecificationError("nested renamer is not supported")
309303

310-
columns = list(arg.keys())
311-
arg = arg.items()
312304
elif any(isinstance(x, (tuple, list)) for x in arg):
313305
arg = [(x, x) if not isinstance(x, (tuple, list)) else x for x in arg]
314306

@@ -335,8 +327,14 @@ def _aggregate_multiple_funcs(self, arg):
335327
results[base.OutputKey(label=name, position=idx)] = obj.aggregate(func)
336328

337329
if any(isinstance(x, DataFrame) for x in results.values()):
338-
# let higher level handle
339-
return results
330+
from pandas import concat
331+
332+
res_df = concat(
333+
results.values(), axis=1, keys=[key.label for key in results.keys()]
334+
)
335+
# error: Incompatible return value type (got "Union[DataFrame, Series]",
336+
# expected "DataFrame")
337+
return res_df # type: ignore[return-value]
340338

341339
indexed_output = {key.position: val for key, val in results.items()}
342340
output = self.obj._constructor_expanddim(indexed_output, index=None)
@@ -1000,6 +998,11 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
1000998
result = op.agg()
1001999
if not is_dict_like(func) and result is not None:
10021000
return result
1001+
elif relabeling and result is not None:
1002+
# this should be the only (non-raising) case with relabeling
1003+
# used reordered index of columns
1004+
result = result.iloc[:, order]
1005+
result.columns = columns
10031006

10041007
if result is None:
10051008

@@ -1039,12 +1042,6 @@ def aggregate(self, func=None, *args, engine=None, engine_kwargs=None, **kwargs)
10391042
[sobj.columns.name] * result.columns.nlevels
10401043
).droplevel(-1)
10411044

1042-
if relabeling:
1043-
1044-
# used reordered index of columns
1045-
result = result.iloc[:, order]
1046-
result.columns = columns
1047-
10481045
if not self.as_index:
10491046
self._insert_inaxis_grouper_inplace(result)
10501047
result.index = np.arange(len(result))
@@ -1389,9 +1386,7 @@ def _transform_item_by_item(self, obj: DataFrame, wrapper) -> DataFrame:
13891386
if not output:
13901387
raise TypeError("Transform function invalid for data types")
13911388

1392-
columns = obj.columns
1393-
if len(output) < len(obj.columns):
1394-
columns = columns.take(inds)
1389+
columns = obj.columns.take(inds)
13951390

13961391
return self.obj._constructor(output, index=obj.index, columns=columns)
13971392

pandas/core/groupby/groupby.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -2163,7 +2163,9 @@ def backfill(self, limit=None):
21632163
@final
21642164
@Substitution(name="groupby")
21652165
@Substitution(see_also=_common_see_also)
2166-
def nth(self, n: int | list[int], dropna: str | None = None) -> DataFrame:
2166+
def nth(
2167+
self, n: int | list[int], dropna: Literal["any", "all", None] = None
2168+
) -> DataFrame:
21672169
"""
21682170
Take the nth row from each group if n is an int, or a subset of rows
21692171
if n is a list of ints.
@@ -2176,9 +2178,9 @@ def nth(self, n: int | list[int], dropna: str | None = None) -> DataFrame:
21762178
----------
21772179
n : int or list of ints
21782180
A single nth value for the row or a list of nth values.
2179-
dropna : None or str, optional
2181+
dropna : {'any', 'all', None}, default None
21802182
Apply the specified dropna operation before counting which row is
2181-
the nth row. Needs to be None, 'any' or 'all'.
2183+
the nth row.
21822184
21832185
Returns
21842186
-------

0 commit comments

Comments
 (0)