-
-
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
61555f6
PERF: blockwise equals
jbrockmendel 28731b7
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 8b44750
docstring
jbrockmendel 6f01de7
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 528fd85
Use namedtuple
jbrockmendel a08b108
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel ec80188
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel d0b116b
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 5e9cd06
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 11aeb30
annotate, make array_equals a module-level func
jbrockmendel 8b21d3e
generator->iterator
jbrockmendel 385ec46
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel 5e95f66
remove unnecessary check
jbrockmendel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,31 @@ | ||
from typing import TYPE_CHECKING, List, Tuple | ||
from collections import namedtuple | ||
from typing import TYPE_CHECKING, Generator, List, Tuple | ||
|
||
import numpy as np | ||
|
||
from pandas._typing import ArrayLike | ||
|
||
from pandas.core.dtypes.common import is_dtype_equal | ||
from pandas.core.dtypes.missing import array_equivalent | ||
|
||
from pandas.core.arrays import ExtensionArray | ||
|
||
if TYPE_CHECKING: | ||
from pandas.core.internals.blocks import Block # noqa:F401 | ||
from pandas.core.internals.managers import BlockManager # noqa:F401 | ||
|
||
|
||
def operate_blockwise( | ||
left: "BlockManager", right: "BlockManager", array_op | ||
) -> "BlockManager": | ||
BlockPairInfo = namedtuple( | ||
"BlockPairInfo", ["lvals", "rvals", "locs", "left_ea", "right_ea", "rblk"], | ||
) | ||
|
||
|
||
def _iter_block_pairs( | ||
left: "BlockManager", right: "BlockManager" | ||
) -> Generator[BlockPairInfo, None, None]: | ||
# 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 +44,32 @@ def operate_blockwise( | |
right_ea = not isinstance(rblk.values, np.ndarray) | ||
|
||
lvals, rvals = _get_same_shape_values(blk, rblk, left_ea, right_ea) | ||
info = BlockPairInfo(lvals, rvals, locs, left_ea, right_ea, rblk) | ||
yield info | ||
|
||
|
||
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_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) | ||
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) | ||
|
||
# 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) | ||
# 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) | ||
|
||
_reset_block_mgr_locs(nbs, locs) | ||
_reset_block_mgr_locs(nbs, locs) | ||
|
||
res_blks.extend(nbs) | ||
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 +106,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 +123,28 @@ def _get_same_shape_values( | |
rvals = rvals[0, :] | ||
|
||
return lvals, rvals | ||
|
||
|
||
def blockwise_all(left: "BlockManager", right: "BlockManager", op) -> bool: | ||
""" | ||
Blockwise `all` reduction. | ||
""" | ||
for info in _iter_block_pairs(left, right): | ||
res = op(info.lvals, info.rvals) | ||
if not res: | ||
return False | ||
return True | ||
|
||
|
||
def array_equals(left: ArrayLike, right: ArrayLike) -> bool: | ||
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. since the inputs are 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. good idea |
||
""" | ||
ExtensionArray-compatible implementation of array_equivalent. | ||
""" | ||
if not is_dtype_equal(left.dtype, right.dtype): | ||
return False | ||
elif isinstance(left, ExtensionArray): | ||
return left.equals(right) | ||
elif isinstance(right, ExtensionArray): | ||
return False | ||
else: | ||
return array_equivalent(left, right, dtype_equal=True) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Iterable (or Iterator) is adequate.
from https://mypy.readthedocs.io/en/stable/kinds_of_types.html#generators