68
68
PeriodIndex ,
69
69
TimedeltaIndex ,
70
70
)
71
- from pandas .core .internals import ArrayManager
72
71
from pandas .core .reshape .concat import concat
73
72
from pandas .core .util .numba_ import (
74
73
NUMBA_FUNC_CACHE ,
@@ -421,26 +420,39 @@ def _apply_blockwise(
421
420
# GH 12541: Special case for count where we support date-like types
422
421
obj = notna (obj ).astype (int )
423
422
obj ._mgr = obj ._mgr .consolidate ()
424
- mgr = obj ._mgr
425
423
426
- def hfunc (bvalues : ArrayLike ) -> ArrayLike :
427
- # TODO(EA2D): getattr unnecessary with 2D EAs
428
- values = self ._prep_values (getattr (bvalues , "T" , bvalues ))
429
- res_values = homogeneous_func (values )
430
- return getattr (res_values , "T" , res_values )
431
-
432
- def hfunc2d (values : ArrayLike ) -> ArrayLike :
424
+ def hfunc (values : ArrayLike ) -> ArrayLike :
433
425
values = self ._prep_values (values )
434
426
return homogeneous_func (values )
435
427
436
- if isinstance (mgr , ArrayManager ) and self .axis == 1 :
437
- new_mgr = mgr .apply_2d (hfunc2d , ignore_failures = True )
438
- else :
439
- new_mgr = mgr .apply (hfunc , ignore_failures = True )
428
+ if self .axis == 1 :
429
+ obj = obj .T
440
430
441
- if 0 != len (new_mgr .items ) != len (mgr .items ):
431
+ taker = []
432
+ res_values = []
433
+ for i , arr in enumerate (obj ._iter_column_arrays ()):
434
+ # GH#42736 operate column-wise instead of block-wise
435
+ try :
436
+ res = hfunc (arr )
437
+ except (TypeError , NotImplementedError ):
438
+ pass
439
+ else :
440
+ res_values .append (res )
441
+ taker .append (i )
442
+
443
+ df = type (obj )._from_arrays (
444
+ res_values ,
445
+ index = obj .index ,
446
+ columns = obj .columns .take (taker ),
447
+ verify_integrity = False ,
448
+ )
449
+
450
+ if self .axis == 1 :
451
+ df = df .T
452
+
453
+ if 0 != len (res_values ) != len (obj .columns ):
442
454
# GH#42738 ignore_failures dropped nuisance columns
443
- dropped = mgr . items .difference (new_mgr . items )
455
+ dropped = obj . columns .difference (obj . columns . take ( taker ) )
444
456
warnings .warn (
445
457
"Dropping of nuisance columns in rolling operations "
446
458
"is deprecated; in a future version this will raise TypeError. "
@@ -449,9 +461,8 @@ def hfunc2d(values: ArrayLike) -> ArrayLike:
449
461
FutureWarning ,
450
462
stacklevel = find_stack_level (),
451
463
)
452
- out = obj ._constructor (new_mgr )
453
464
454
- return self ._resolve_output (out , obj )
465
+ return self ._resolve_output (df , obj )
455
466
456
467
def _apply_tablewise (
457
468
self , homogeneous_func : Callable [..., ArrayLike ], name : str | None = None
@@ -540,10 +551,7 @@ def calc(x):
540
551
return func (x , start , end , min_periods , * numba_args )
541
552
542
553
with np .errstate (all = "ignore" ):
543
- if values .ndim > 1 and self .method == "single" :
544
- result = np .apply_along_axis (calc , self .axis , values )
545
- else :
546
- result = calc (values )
554
+ result = calc (values )
547
555
548
556
if numba_cache_key is not None :
549
557
NUMBA_FUNC_CACHE [numba_cache_key ] = func
@@ -1024,11 +1032,8 @@ def calc(x):
1024
1032
return func (x , window , self .min_periods or len (window ))
1025
1033
1026
1034
with np .errstate (all = "ignore" ):
1027
- if values .ndim > 1 :
1028
- result = np .apply_along_axis (calc , self .axis , values )
1029
- else :
1030
- # Our weighted aggregations return memoryviews
1031
- result = np .asarray (calc (values ))
1035
+ # Our weighted aggregations return memoryviews
1036
+ result = np .asarray (calc (values ))
1032
1037
1033
1038
if self .center :
1034
1039
result = self ._center_window (result , offset )
0 commit comments