27
27
)
28
28
from pandas .core .dtypes .concat import concat_compat
29
29
from pandas .core .dtypes .dtypes import ExtensionDtype
30
- from pandas .core .dtypes .generic import ABCExtensionArray , ABCSeries
30
+ from pandas .core .dtypes .generic import ABCDataFrame , ABCSeries
31
31
from pandas .core .dtypes .missing import isna
32
32
33
33
import pandas .core .algorithms as algos
@@ -375,7 +375,7 @@ def reduce(self, func, *args, **kwargs):
375
375
376
376
return res
377
377
378
- def apply (self : T , f , filter = None , ** kwargs ) -> T :
378
+ def apply (self : T , f , filter = None , align_keys = None , ** kwargs ) -> T :
379
379
"""
380
380
Iterate over the blocks, collect and create a new BlockManager.
381
381
@@ -390,6 +390,7 @@ def apply(self: T, f, filter=None, **kwargs) -> T:
390
390
-------
391
391
BlockManager
392
392
"""
393
+ align_keys = align_keys or []
393
394
result_blocks = []
394
395
# fillna: Series/DataFrame is responsible for making sure value is aligned
395
396
@@ -404,28 +405,14 @@ def apply(self: T, f, filter=None, **kwargs) -> T:
404
405
405
406
self ._consolidate_inplace ()
406
407
408
+ align_copy = False
407
409
if f == "where" :
408
410
align_copy = True
409
- if kwargs .get ("align" , True ):
410
- align_keys = ["other" , "cond" ]
411
- else :
412
- align_keys = ["cond" ]
413
- elif f == "putmask" :
414
- align_copy = False
415
- if kwargs .get ("align" , True ):
416
- align_keys = ["new" , "mask" ]
417
- else :
418
- align_keys = ["mask" ]
419
- else :
420
- align_keys = []
421
411
422
- # TODO(EA): may interfere with ExtensionBlock.setitem for blocks
423
- # with a .values attribute.
424
412
aligned_args = {
425
413
k : kwargs [k ]
426
414
for k in align_keys
427
- if not isinstance (kwargs [k ], ABCExtensionArray )
428
- and hasattr (kwargs [k ], "values" )
415
+ if isinstance (kwargs [k ], (ABCSeries , ABCDataFrame ))
429
416
}
430
417
431
418
for b in self .blocks :
@@ -561,13 +548,24 @@ def isna(self, func) -> "BlockManager":
561
548
return self .apply ("apply" , func = func )
562
549
563
550
def where (self , ** kwargs ) -> "BlockManager" :
564
- return self .apply ("where" , ** kwargs )
551
+ if kwargs .pop ("align" , True ):
552
+ align_keys = ["other" , "cond" ]
553
+ else :
554
+ align_keys = ["cond" ]
555
+
556
+ return self .apply ("where" , align_keys = align_keys , ** kwargs )
565
557
566
558
def setitem (self , indexer , value ) -> "BlockManager" :
567
559
return self .apply ("setitem" , indexer = indexer , value = value )
568
560
569
561
def putmask (self , ** kwargs ):
570
- return self .apply ("putmask" , ** kwargs )
562
+
563
+ if kwargs .pop ("align" , True ):
564
+ align_keys = ["new" , "mask" ]
565
+ else :
566
+ align_keys = ["mask" ]
567
+
568
+ return self .apply ("putmask" , align_keys = align_keys , ** kwargs )
571
569
572
570
def diff (self , n : int , axis : int ) -> "BlockManager" :
573
571
return self .apply ("diff" , n = n , axis = axis )
0 commit comments