-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
REF: move actual lookup and dispatch to array_op from frame into internals #39772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
c454025
81b1016
c6ed357
1f8622e
a487f0f
9fe7f44
3395743
a1495f4
b5d7ab2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
import numpy as np | ||
|
||
from pandas._libs import internals as libinternals, lib | ||
from pandas._typing import ArrayLike, Dtype, DtypeObj, Shape | ||
from pandas._typing import ArrayLike, Dtype, DtypeObj, Scalar, Shape | ||
from pandas.errors import PerformanceWarning | ||
from pandas.util._validators import validate_bool_kwarg | ||
|
||
|
@@ -35,6 +35,7 @@ | |
from pandas.core.dtypes.generic import ABCDataFrame, ABCPandasArray, ABCSeries | ||
from pandas.core.dtypes.missing import array_equals, isna | ||
|
||
from pandas.core import ops | ||
import pandas.core.algorithms as algos | ||
from pandas.core.arrays.sparse import SparseDtype | ||
from pandas.core.construction import extract_array | ||
|
@@ -366,10 +367,73 @@ def reduce( | |
new_mgr = type(self).from_blocks(res_blocks, [self.items, index]) | ||
return new_mgr, indexer | ||
|
||
def operate_blockwise(self, other: BlockManager, array_op) -> BlockManager: | ||
def operate_scalar(self, other: Scalar, op) -> BlockManager: | ||
""" | ||
Apply array_op blockwise with another (aligned) BlockManager. | ||
Element-wise (arithmetic/comparison/logical) operation with other scalar. | ||
|
||
Parameters | ||
---------- | ||
other : scalar | ||
op : operator function (eg ``operator.add``) | ||
|
||
Returns | ||
------- | ||
BlockManager | ||
""" | ||
# Get the appropriate array-op to apply to each column/block's values. | ||
array_op = ops.get_array_op(op) | ||
return self.apply(array_op, right=other) | ||
|
||
def operate_array(self, other: ArrayLike, op, axis: int) -> BlockManager: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note I currently add 3 specialized methods ( |
||
""" | ||
Element-wise (arithmetic/comparison/logical) operation with other array. | ||
|
||
The array is already checked to be of the correct length. | ||
|
||
Parameters | ||
---------- | ||
other : np.ndarray or ExtensionArray | ||
op : operator function (eg ``operator.add``) | ||
axis : int | ||
Whether to match the array on the index and broadcast along the | ||
columns (axis=0) or match the array on the columns and broadcast | ||
along the rows (axis=1). | ||
|
||
Returns | ||
------- | ||
BlockManager | ||
""" | ||
array_op = ops.get_array_op(op) | ||
if axis == 1: | ||
# match on the columns -> operate on each column array with single | ||
# element from other array | ||
arrays = [ | ||
array_op(self.iget_values(i), _right) for i, _right in enumerate(other) | ||
] | ||
else: | ||
# match on the rows -> operate for each column array with full other array | ||
arrays = [ | ||
array_op(self.iget_values(i), other) for i in range(len(self.items)) | ||
] | ||
|
||
return create_block_manager_from_arrays(arrays, self.axes[0], self.axes) | ||
|
||
def operate_manager(self, other: BlockManager, op) -> BlockManager: | ||
jreback marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Element-wise (arithmetic/comparison/logical) operation with other BlockManager. | ||
|
||
The other BlockManager is already aligned with `self`. | ||
|
||
Parameters | ||
---------- | ||
other : BlockManager | ||
op : operator function (eg ``operator.add``) | ||
|
||
Returns | ||
------- | ||
BlockManager | ||
""" | ||
array_op = ops.get_array_op(op) | ||
return operate_blockwise(self, other, array_op) | ||
|
||
def apply( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.