@@ -526,35 +526,9 @@ def _aggregate_named(self, func, *args, **kwargs):
526
526
@Substitution (klass = "Series" )
527
527
@Appender (_transform_template )
528
528
def transform (self , func , * args , engine = None , engine_kwargs = None , ** kwargs ):
529
-
530
- if maybe_use_numba (engine ):
531
- with group_selection_context (self ):
532
- data = self ._selected_obj
533
- result = self ._transform_with_numba (
534
- data .to_frame (), func , * args , engine_kwargs = engine_kwargs , ** kwargs
535
- )
536
- return self .obj ._constructor (
537
- result .ravel (), index = data .index , name = data .name
538
- )
539
-
540
- func = com .get_cython_func (func ) or func
541
-
542
- if not isinstance (func , str ):
543
- return self ._transform_general (func , * args , ** kwargs )
544
-
545
- elif func not in base .transform_kernel_allowlist :
546
- msg = f"'{ func } ' is not a valid function name for transform(name)"
547
- raise ValueError (msg )
548
- elif func in base .cythonized_kernels or func in base .transformation_kernels :
549
- # cythonized transform or canned "agg+broadcast"
550
- return getattr (self , func )(* args , ** kwargs )
551
- # If func is a reduction, we need to broadcast the
552
- # result to the whole group. Compute func result
553
- # and deal with possible broadcasting below.
554
- # Temporarily set observed for dealing with categoricals.
555
- with com .temp_setattr (self , "observed" , True ):
556
- result = getattr (self , func )(* args , ** kwargs )
557
- return self ._wrap_transform_fast_result (result )
529
+ return self ._transform (
530
+ func , * args , engine = engine , engine_kwargs = engine_kwargs , ** kwargs
531
+ )
558
532
559
533
def _transform_general (self , func : Callable , * args , ** kwargs ) -> Series :
560
534
"""
@@ -586,6 +560,9 @@ def _transform_general(self, func: Callable, *args, **kwargs) -> Series:
586
560
result .name = self ._selected_obj .name
587
561
return result
588
562
563
+ def _can_use_transform_fast (self , result ) -> bool :
564
+ return True
565
+
589
566
def _wrap_transform_fast_result (self , result : Series ) -> Series :
590
567
"""
591
568
fast version of transform, only applicable to
@@ -1334,43 +1311,14 @@ def _transform_general(self, func, *args, **kwargs):
1334
1311
@Substitution (klass = "DataFrame" )
1335
1312
@Appender (_transform_template )
1336
1313
def transform (self , func , * args , engine = None , engine_kwargs = None , ** kwargs ):
1314
+ return self ._transform (
1315
+ func , * args , engine = engine , engine_kwargs = engine_kwargs , ** kwargs
1316
+ )
1337
1317
1338
- if maybe_use_numba (engine ):
1339
- with group_selection_context (self ):
1340
- data = self ._selected_obj
1341
- result = self ._transform_with_numba (
1342
- data , func , * args , engine_kwargs = engine_kwargs , ** kwargs
1343
- )
1344
- return self .obj ._constructor (result , index = data .index , columns = data .columns )
1345
-
1346
- # optimized transforms
1347
- func = com .get_cython_func (func ) or func
1348
-
1349
- if not isinstance (func , str ):
1350
- return self ._transform_general (func , * args , ** kwargs )
1351
-
1352
- elif func not in base .transform_kernel_allowlist :
1353
- msg = f"'{ func } ' is not a valid function name for transform(name)"
1354
- raise ValueError (msg )
1355
- elif func in base .cythonized_kernels or func in base .transformation_kernels :
1356
- # cythonized transformation or canned "reduction+broadcast"
1357
- return getattr (self , func )(* args , ** kwargs )
1358
- # GH 30918
1359
- # Use _transform_fast only when we know func is an aggregation
1360
- if func in base .reduction_kernels :
1361
- # If func is a reduction, we need to broadcast the
1362
- # result to the whole group. Compute func result
1363
- # and deal with possible broadcasting below.
1364
- # Temporarily set observed for dealing with categoricals.
1365
- with com .temp_setattr (self , "observed" , True ):
1366
- result = getattr (self , func )(* args , ** kwargs )
1367
-
1368
- if isinstance (result , DataFrame ) and result .columns .equals (
1369
- self ._obj_with_exclusions .columns
1370
- ):
1371
- return self ._wrap_transform_fast_result (result )
1372
-
1373
- return self ._transform_general (func , * args , ** kwargs )
1318
+ def _can_use_transform_fast (self , result ) -> bool :
1319
+ return isinstance (result , DataFrame ) and result .columns .equals (
1320
+ self ._obj_with_exclusions .columns
1321
+ )
1374
1322
1375
1323
def _wrap_transform_fast_result (self , result : DataFrame ) -> DataFrame :
1376
1324
"""
0 commit comments