20
20
import numpy as np
21
21
22
22
from pandas ._libs import internals as libinternals , lib
23
- from pandas ._typing import ArrayLike , Dtype , DtypeObj , Shape
23
+ from pandas ._typing import ArrayLike , Dtype , DtypeObj , Scalar , Shape
24
24
from pandas .errors import PerformanceWarning
25
25
from pandas .util ._validators import validate_bool_kwarg
26
26
@@ -367,24 +367,51 @@ def reduce(
367
367
new_mgr = type (self ).from_blocks (res_blocks , [self .items , index ])
368
368
return new_mgr , indexer
369
369
370
- def operate_scalar (self , other , op ) -> BlockManager :
370
+ def operate_scalar (self , other : Scalar , op ) -> BlockManager :
371
371
"""
372
- TODO fill in
372
+ Element-wise (arithmetic/comparison/logical) operation with other scalar.
373
+
374
+ Parameters
375
+ ----------
376
+ other : scalar
377
+ op : operator function (eg ``operator.add``)
378
+
379
+ Returns
380
+ -------
381
+ BlockManager
373
382
"""
374
383
# Get the appropriate array-op to apply to each column/block's values.
375
384
array_op = ops .get_array_op (op )
376
385
return self .apply (array_op , right = other )
377
386
378
387
def operate_array (self , other : ArrayLike , op , axis : int ) -> BlockManager :
379
388
"""
380
- TODO fill in
389
+ Element-wise (arithmetic/comparison/logical) operation with other array.
390
+
391
+ The array is already checked to be of the correct length.
392
+
393
+ Parameters
394
+ ----------
395
+ other : np.ndarray or ExtensionArray
396
+ op : operator function (eg ``operator.add``)
397
+ axis : int
398
+ Whether to match the array on the index and broadcast along the
399
+ columns (axis=0) or match the array on the columns and broadcast
400
+ along the rows (axis=1).
401
+
402
+ Returns
403
+ -------
404
+ BlockManager
381
405
"""
382
406
array_op = ops .get_array_op (op )
383
407
if axis == 1 :
408
+ # match on the columns -> operate on each column array with single
409
+ # element from other array
384
410
arrays = [
385
411
array_op (self .iget_values (i ), _right ) for i , _right in enumerate (other )
386
412
]
387
413
else :
414
+ # match on the rows -> operate for each column array with full other array
388
415
arrays = [
389
416
array_op (self .iget_values (i ), other ) for i in range (len (self .items ))
390
417
]
@@ -393,15 +420,20 @@ def operate_array(self, other: ArrayLike, op, axis: int) -> BlockManager:
393
420
394
421
def operate_manager (self , other : BlockManager , op ) -> BlockManager :
395
422
"""
396
- TODO fill in
397
- """
398
- array_op = ops .get_array_op (op )
399
- return self .operate_blockwise (other , array_op )
423
+ Element-wise (arithmetic/comparison/logical) operation with other BlockManager.
400
424
401
- def operate_blockwise (self , other : BlockManager , array_op ) -> BlockManager :
402
- """
403
- Apply array_op blockwise with another (aligned) BlockManager.
425
+ The other BlockManager is already aligned with `self`.
426
+
427
+ Parameters
428
+ ----------
429
+ other : BlockManager
430
+ op : operator function (eg ``operator.add``)
431
+
432
+ Returns
433
+ -------
434
+ BlockManager
404
435
"""
436
+ array_op = ops .get_array_op (op )
405
437
return operate_blockwise (self , other , array_op )
406
438
407
439
def apply (
0 commit comments