Skip to content

Commit 6ed8141

Browse files
committed
CLN: aggregation.transform
1 parent 6929e26 commit 6ed8141

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

pandas/core/aggregation.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
Union,
1919
)
2020

21-
from pandas._typing import AggFuncType, Axis, FrameOrSeries, Label
21+
from pandas._typing import AggFuncType, Axis, FrameOrSeries, FrameOrSeriesUnion, Label
2222

2323
from pandas.core.dtypes.common import is_dict_like, is_list_like
2424
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
@@ -387,8 +387,8 @@ def validate_func_kwargs(
387387

388388

389389
def transform(
390-
obj: FrameOrSeries, func: AggFuncType, axis: Axis, *args, **kwargs,
391-
) -> FrameOrSeries:
390+
obj: FrameOrSeriesUnion, func: AggFuncType, axis: Axis, *args, **kwargs,
391+
) -> FrameOrSeriesUnion:
392392
"""
393393
Transform a DataFrame or Series
394394
@@ -423,13 +423,13 @@ def transform(
423423
assert not is_series
424424
return transform(obj.T, func, 0, *args, **kwargs).T
425425

426-
if isinstance(func, list):
426+
if is_list_like(func) and not is_dict_like(func):
427427
if is_series:
428428
func = {com.get_callable_name(v) or v: v for v in func}
429429
else:
430430
func = {col: func for col in obj}
431431

432-
if isinstance(func, dict):
432+
if is_dict_like(func):
433433
if not is_series:
434434
cols = sorted(set(func.keys()) - set(obj.columns))
435435
if len(cols) > 0:
@@ -439,7 +439,7 @@ def transform(
439439
# GH 15931 - deprecation of renaming keys
440440
raise SpecificationError("nested renamer is not supported")
441441

442-
results = {}
442+
results: Dict[Label, FrameOrSeriesUnion] = {}
443443
for name, how in func.items():
444444
colg = obj._gotitem(name, ndim=1)
445445
try:

pandas/core/shared_docs.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,15 @@
267267
----------
268268
func : function, str, list or dict
269269
Function to use for transforming the data. If a function, must either
270-
work when passed a {klass} or when passed to {klass}.apply.
270+
work when passed a {klass} or when passed to {klass}.apply. If func
271+
is both list-like and dict-like, dict-like behavior takes precedence.
271272
272273
Accepted combinations are:
273274
274275
- function
275276
- string function name
276-
- list of functions and/or function names, e.g. ``[np.exp, 'sqrt']``
277-
- dict of axis labels -> functions, function names or list of such.
277+
- list-like of functions and/or function names, e.g. ``[np.exp, 'sqrt']``
278+
- dict-like of axis labels -> functions, function names or list-like of such.
278279
{axis}
279280
*args
280281
Positional arguments to pass to `func`.

0 commit comments

Comments
 (0)