-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
PERF: BlockManager.equals blockwise #35357
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 1 commit
61555f6
28731b7
8b44750
6f01de7
528fd85
a08b108
ec80188
d0b116b
5e9cd06
11aeb30
8b21d3e
385ec46
5e95f66
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 |
---|---|---|
|
@@ -9,13 +9,10 @@ | |
from pandas.core.internals.blocks import Block # noqa:F401 | ||
|
||
|
||
def operate_blockwise( | ||
left: "BlockManager", right: "BlockManager", array_op | ||
) -> "BlockManager": | ||
def _iter_block_pairs(left: "BlockManager", right: "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. can you type the output |
||
# At this point we have already checked the parent DataFrames for | ||
# assert rframe._indexed_same(lframe) | ||
|
||
res_blks: List["Block"] = [] | ||
for n, blk in enumerate(left.blocks): | ||
locs = blk.mgr_locs | ||
blk_vals = blk.values | ||
|
@@ -34,21 +31,31 @@ def operate_blockwise( | |
right_ea = not isinstance(rblk.values, np.ndarray) | ||
|
||
lvals, rvals = _get_same_shape_values(blk, rblk, left_ea, right_ea) | ||
yield lvals, rvals, locs, left_ea, right_ea, rblk | ||
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. maybe yield a NamedTuple? 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. IIRC there are issues with NamedTuple construction performance 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. maybe wait a couple of days and use a dataclass. 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.
really? can you point to something that shows this 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. May be out of date but I think this is where i got the impression that NamedTuple has perf issues: https://lwn.net/Articles/731423/ 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. i prefer readability here |
||
|
||
res_values = array_op(lvals, rvals) | ||
if left_ea and not right_ea and hasattr(res_values, "reshape"): | ||
res_values = res_values.reshape(1, -1) | ||
nbs = rblk._split_op_result(res_values) | ||
|
||
# Assertions are disabled for performance, but should hold: | ||
# if right_ea or left_ea: | ||
# assert len(nbs) == 1 | ||
# else: | ||
# assert res_values.shape == lvals.shape, (res_values.shape, lvals.shape) | ||
def operate_blockwise( | ||
left: "BlockManager", right: "BlockManager", array_op | ||
) -> "BlockManager": | ||
# At this point we have already checked the parent DataFrames for | ||
# assert rframe._indexed_same(lframe) | ||
|
||
res_blks: List["Block"] = [] | ||
for lvals, rvals, locs, left_ea, right_ea, rblk in _iter_block_pairs(left, right): | ||
res_values = array_op(lvals, rvals) | ||
if left_ea and not right_ea and hasattr(res_values, "reshape"): | ||
res_values = res_values.reshape(1, -1) | ||
nbs = rblk._split_op_result(res_values) | ||
|
||
_reset_block_mgr_locs(nbs, locs) | ||
# Assertions are disabled for performance, but should hold: | ||
# if right_ea or left_ea: | ||
# assert len(nbs) == 1 | ||
# else: | ||
# assert res_values.shape == lvals.shape, (res_values.shape, lvals.shape) | ||
|
||
res_blks.extend(nbs) | ||
_reset_block_mgr_locs(nbs, locs) | ||
|
||
res_blks.extend(nbs) | ||
|
||
# Assertions are disabled for performance, but should hold: | ||
# slocs = {y for nb in res_blks for y in nb.mgr_locs.as_array} | ||
|
@@ -85,7 +92,7 @@ def _get_same_shape_values( | |
# Require that the indexing into lvals be slice-like | ||
assert rblk.mgr_locs.is_slice_like, rblk.mgr_locs | ||
|
||
# TODO(EA2D): with 2D EAs pnly this first clause would be needed | ||
# TODO(EA2D): with 2D EAs only this first clause would be needed | ||
if not (left_ea or right_ea): | ||
lvals = lvals[rblk.mgr_locs.indexer, :] | ||
assert lvals.shape == rvals.shape, (lvals.shape, rvals.shape) | ||
|
@@ -102,3 +109,11 @@ def _get_same_shape_values( | |
rvals = rvals[0, :] | ||
|
||
return lvals, rvals | ||
|
||
|
||
def blockwise_all(left: "BlockManager", right: "BlockManager", op) -> bool: | ||
for lvals, rvals, _, _, _, _ in _iter_block_pairs(left, right): | ||
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. can you add a doc-string (also using a NamedTuple makes this easier to read here) |
||
res = op(lvals, rvals) | ||
if not res: | ||
return False | ||
return True |
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.
can you make this module left
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.
can you do this; make this a module level function