Skip to content

Commit be443d4

Browse files
rhshadrachKevin D Smith
authored and
Kevin D Smith
committed
CLN: Break up aggregate.transform (pandas-dev#36618)
1 parent e430ffc commit be443d4

File tree

1 file changed

+51
-35
lines changed

1 file changed

+51
-35
lines changed

pandas/core/aggregation.py

+51-35
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,6 @@ def transform(
418418
ValueError
419419
If the transform function fails or does not transform.
420420
"""
421-
from pandas.core.reshape.concat import concat
422-
423421
is_series = obj.ndim == 1
424422

425423
if obj._get_axis_number(axis) == 1:
@@ -433,42 +431,11 @@ def transform(
433431
func = {col: func for col in obj}
434432

435433
if isinstance(func, dict):
436-
if not is_series:
437-
cols = sorted(set(func.keys()) - set(obj.columns))
438-
if len(cols) > 0:
439-
raise SpecificationError(f"Column(s) {cols} do not exist")
440-
441-
if any(isinstance(v, dict) for v in func.values()):
442-
# GH 15931 - deprecation of renaming keys
443-
raise SpecificationError("nested renamer is not supported")
444-
445-
results = {}
446-
for name, how in func.items():
447-
colg = obj._gotitem(name, ndim=1)
448-
try:
449-
results[name] = transform(colg, how, 0, *args, **kwargs)
450-
except Exception as e:
451-
if str(e) == "Function did not transform":
452-
raise e
453-
454-
# combine results
455-
if len(results) == 0:
456-
raise ValueError("Transform function failed")
457-
return concat(results, axis=1)
434+
return transform_dict_like(obj, func, *args, **kwargs)
458435

459436
# func is either str or callable
460437
try:
461-
if isinstance(func, str):
462-
result = obj._try_aggregate_string_function(func, *args, **kwargs)
463-
else:
464-
f = obj._get_cython_func(func)
465-
if f and not args and not kwargs:
466-
result = getattr(obj, f)()
467-
else:
468-
try:
469-
result = obj.apply(func, args=args, **kwargs)
470-
except Exception:
471-
result = func(obj, *args, **kwargs)
438+
result = transform_str_or_callable(obj, func, *args, **kwargs)
472439
except Exception:
473440
raise ValueError("Transform function failed")
474441

@@ -482,3 +449,52 @@ def transform(
482449
raise ValueError("Function did not transform")
483450

484451
return result
452+
453+
454+
def transform_dict_like(obj, func, *args, **kwargs):
455+
"""
456+
Compute transform in the case of a dict-like func
457+
"""
458+
from pandas.core.reshape.concat import concat
459+
460+
if obj.ndim != 1:
461+
cols = sorted(set(func.keys()) - set(obj.columns))
462+
if len(cols) > 0:
463+
raise SpecificationError(f"Column(s) {cols} do not exist")
464+
465+
if any(isinstance(v, dict) for v in func.values()):
466+
# GH 15931 - deprecation of renaming keys
467+
raise SpecificationError("nested renamer is not supported")
468+
469+
results = {}
470+
for name, how in func.items():
471+
colg = obj._gotitem(name, ndim=1)
472+
try:
473+
results[name] = transform(colg, how, 0, *args, **kwargs)
474+
except Exception as e:
475+
if str(e) == "Function did not transform":
476+
raise e
477+
478+
# combine results
479+
if len(results) == 0:
480+
raise ValueError("Transform function failed")
481+
return concat(results, axis=1)
482+
483+
484+
def transform_str_or_callable(obj, func, *args, **kwargs):
485+
"""
486+
Compute transform in the case of a string or callable func
487+
"""
488+
if isinstance(func, str):
489+
return obj._try_aggregate_string_function(func, *args, **kwargs)
490+
491+
if not args and not kwargs:
492+
f = obj._get_cython_func(func)
493+
if f:
494+
return getattr(obj, f)()
495+
496+
# Two possible ways to use a UDF - apply or call directly
497+
try:
498+
return obj.apply(func, args=args, **kwargs)
499+
except Exception:
500+
return func(obj, *args, **kwargs)

0 commit comments

Comments
 (0)