Skip to content

Commit e06e381

Browse files
authored
REF: make operate_blockwise into a BlockManager method (#34281)
1 parent 8d0e1fe commit e06e381

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

pandas/core/internals/managers.py

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
get_block_type,
4848
make_block,
4949
)
50+
from pandas.core.internals.ops import operate_blockwise
5051

5152
# TODO: flexible with index=None and/or items=None
5253

@@ -352,6 +353,12 @@ def reduce(self, func, *args, **kwargs):
352353

353354
return res
354355

356+
def operate_blockwise(self, other: "BlockManager", array_op) -> "BlockManager":
357+
"""
358+
Apply array_op blockwise with another (aligned) BlockManager.
359+
"""
360+
return operate_blockwise(self, other, array_op)
361+
355362
def apply(self: T, f, align_keys=None, **kwargs) -> T:
356363
"""
357364
Iterate over the blocks, collect and create a new BlockManager.

pandas/core/ops/blockwise.py renamed to pandas/core/internals/ops.py

+10-8
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,24 @@
55
from pandas._typing import ArrayLike
66

77
if TYPE_CHECKING:
8+
from pandas.core.internals.managers import BlockManager # noqa:F401
89
from pandas.core.internals.blocks import Block # noqa:F401
910

1011

11-
def operate_blockwise(left, right, array_op):
12-
# At this point we have already checked
13-
# assert right._indexed_same(left)
12+
def operate_blockwise(
13+
left: "BlockManager", right: "BlockManager", array_op
14+
) -> "BlockManager":
15+
# At this point we have already checked the parent DataFrames for
16+
# assert rframe._indexed_same(lframe)
1417

1518
res_blks: List["Block"] = []
16-
rmgr = right._mgr
17-
for n, blk in enumerate(left._mgr.blocks):
19+
for n, blk in enumerate(left.blocks):
1820
locs = blk.mgr_locs
1921
blk_vals = blk.values
2022

2123
left_ea = not isinstance(blk_vals, np.ndarray)
2224

23-
rblks = rmgr._slice_take_blocks_ax0(locs.indexer, only_slice=True)
25+
rblks = right._slice_take_blocks_ax0(locs.indexer, only_slice=True)
2426

2527
# Assertions are disabled for performance, but should hold:
2628
# if left_ea:
@@ -51,11 +53,11 @@ def operate_blockwise(left, right, array_op):
5153
# Assertions are disabled for performance, but should hold:
5254
# slocs = {y for nb in res_blks for y in nb.mgr_locs.as_array}
5355
# nlocs = sum(len(nb.mgr_locs.as_array) for nb in res_blks)
54-
# assert nlocs == len(left.columns), (nlocs, len(left.columns))
56+
# assert nlocs == len(left.items), (nlocs, len(left.items))
5557
# assert len(slocs) == nlocs, (len(slocs), nlocs)
5658
# assert slocs == set(range(nlocs)), slocs
5759

58-
new_mgr = type(rmgr)(res_blks, axes=rmgr.axes, do_integrity_check=False)
60+
new_mgr = type(right)(res_blks, axes=right.axes, do_integrity_check=False)
5961
return new_mgr
6062

6163

pandas/core/ops/__init__.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
logical_op,
2727
)
2828
from pandas.core.ops.array_ops import comp_method_OBJECT_ARRAY # noqa:F401
29-
from pandas.core.ops.blockwise import operate_blockwise
3029
from pandas.core.ops.common import unpack_zerodim_and_defer
3130
from pandas.core.ops.dispatch import should_series_dispatch
3231
from pandas.core.ops.docstrings import (
@@ -327,7 +326,7 @@ def dispatch_to_series(left, right, func, str_rep=None, axis=None):
327326
assert right._indexed_same(left)
328327

329328
array_op = get_array_op(func, str_rep=str_rep)
330-
bm = operate_blockwise(left, right, array_op)
329+
bm = left._mgr.operate_blockwise(right._mgr, array_op)
331330
return type(left)(bm)
332331

333332
elif isinstance(right, ABCSeries) and axis == "columns":

0 commit comments

Comments
 (0)