@@ -418,8 +418,6 @@ def transform(
418
418
ValueError
419
419
If the transform function fails or does not transform.
420
420
"""
421
- from pandas .core .reshape .concat import concat
422
-
423
421
is_series = obj .ndim == 1
424
422
425
423
if obj ._get_axis_number (axis ) == 1 :
@@ -433,42 +431,11 @@ def transform(
433
431
func = {col : func for col in obj }
434
432
435
433
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 )
458
435
459
436
# func is either str or callable
460
437
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 )
472
439
except Exception :
473
440
raise ValueError ("Transform function failed" )
474
441
@@ -482,3 +449,52 @@ def transform(
482
449
raise ValueError ("Function did not transform" )
483
450
484
451
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