Skip to content

Commit 1f8622e

Browse files
add ArrayManager versions
1 parent c6ed357 commit 1f8622e

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

pandas/core/internals/array_manager.py

+66-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import numpy as np
99

1010
from pandas._libs import algos as libalgos, lib
11-
from pandas._typing import ArrayLike, DtypeObj, Hashable
11+
from pandas._typing import ArrayLike, DtypeObj, Hashable, Scalar
1212
from pandas.util._validators import validate_bool_kwarg
1313

1414
from pandas.core.dtypes.cast import find_common_type, infer_dtype_from_scalar
@@ -22,6 +22,7 @@
2222
from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
2323
from pandas.core.dtypes.missing import array_equals, isna
2424

25+
from pandas.core import ops
2526
import pandas.core.algorithms as algos
2627
from pandas.core.arrays import ExtensionArray
2728
from pandas.core.arrays.sparse import SparseDtype
@@ -187,11 +188,73 @@ def reduce(
187188
indexer = np.arange(self.shape[0])
188189
return new_mgr, indexer
189190

190-
def operate_blockwise(self, other: ArrayManager, array_op) -> ArrayManager:
191+
def operate_scalar(self, other: Scalar, op) -> ArrayManager:
191192
"""
192-
Apply array_op blockwise with another (aligned) BlockManager.
193+
Element-wise (arithmetic/comparison/logical) operation with other scalar.
194+
195+
Parameters
196+
----------
197+
other : scalar
198+
op : operator function (eg ``operator.add``)
199+
200+
Returns
201+
-------
202+
ArrayManager
203+
"""
204+
# Get the appropriate array-op to apply to each column/block's values.
205+
array_op = ops.get_array_op(op)
206+
result_arrays = [array_op(left, other) for left in self.arrays]
207+
return type(self)(result_arrays, self._axes)
208+
209+
def operate_array(self, other: ArrayLike, op, axis: int) -> ArrayManager:
210+
"""
211+
Element-wise (arithmetic/comparison/logical) operation with other array.
212+
213+
The array is already checked to be of the correct length.
214+
215+
Parameters
216+
----------
217+
other : np.ndarray or ExtensionArray
218+
op : operator function (eg ``operator.add``)
219+
axis : int
220+
Whether to match the array on the index and broadcast along the
221+
columns (axis=0) or match the array on the columns and broadcast
222+
along the rows (axis=1).
223+
224+
Returns
225+
-------
226+
ArrayManager
227+
"""
228+
array_op = ops.get_array_op(op)
229+
if axis == 1:
230+
# match on the columns -> operate on each column array with single
231+
# element from other array
232+
result_arrays = [
233+
array_op(left, right_scalar)
234+
for left, right_scalar in zip(self.arrays, other)
235+
]
236+
else:
237+
# match on the rows -> operate for each column array with full other array
238+
result_arrays = [array_op(left, other) for left in self.arrays]
239+
return type(self)(result_arrays, self._axes)
240+
241+
def operate_manager(self, other: ArrayManager, op) -> ArrayManager:
242+
"""
243+
Element-wise (arithmetic/comparison/logical) operation with other ArrayManager.
244+
245+
The other ArrayManager is already aligned with `self`.
246+
247+
Parameters
248+
----------
249+
other : ArrayManager
250+
op : operator function (eg ``operator.add``)
251+
252+
Returns
253+
-------
254+
ArrayManager
193255
"""
194256
# TODO what if `other` is BlockManager ?
257+
array_op = ops.get_array_op(op)
195258
left_arrays = self.arrays
196259
right_arrays = other.arrays
197260
result_arrays = [

0 commit comments

Comments
 (0)