|
8 | 8 | import numpy as np
|
9 | 9 |
|
10 | 10 | 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 |
12 | 12 | from pandas.util._validators import validate_bool_kwarg
|
13 | 13 |
|
14 | 14 | from pandas.core.dtypes.cast import find_common_type, infer_dtype_from_scalar
|
|
22 | 22 | from pandas.core.dtypes.generic import ABCDataFrame, ABCSeries
|
23 | 23 | from pandas.core.dtypes.missing import array_equals, isna
|
24 | 24 |
|
| 25 | +from pandas.core import ops |
25 | 26 | import pandas.core.algorithms as algos
|
26 | 27 | from pandas.core.arrays import ExtensionArray
|
27 | 28 | from pandas.core.arrays.sparse import SparseDtype
|
@@ -187,11 +188,73 @@ def reduce(
|
187 | 188 | indexer = np.arange(self.shape[0])
|
188 | 189 | return new_mgr, indexer
|
189 | 190 |
|
190 |
| - def operate_blockwise(self, other: ArrayManager, array_op) -> ArrayManager: |
| 191 | + def operate_scalar(self, other: Scalar, op) -> ArrayManager: |
191 | 192 | """
|
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 |
193 | 255 | """
|
194 | 256 | # TODO what if `other` is BlockManager ?
|
| 257 | + array_op = ops.get_array_op(op) |
195 | 258 | left_arrays = self.arrays
|
196 | 259 | right_arrays = other.arrays
|
197 | 260 | result_arrays = [
|
|
0 commit comments