Skip to content

PERF: optimize Block.getitem_block #34978

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 1 commit into from
Jun 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pandas/_libs/internals.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ cnp.import_array()
from pandas._libs.algos import ensure_int64


@cython.final
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do these actually make a diff?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i havent measured this independently, so not sure how much it matters in this case (tried it for some Timestamp methods and got a decent boost). it allows cython to do some inlining, at the cost of disallowing subclassing.

cdef class BlockPlacement:
# __slots__ = '_as_slice', '_as_array', '_len'
cdef:
Expand Down
18 changes: 17 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from pandas._libs import NaT, algos as libalgos, lib, writers
import pandas._libs.internals as libinternals
from pandas._libs.internals import BlockPlacement
from pandas._libs.tslibs import conversion
from pandas._libs.tslibs.timezones import tz_compare
from pandas._typing import ArrayLike
Expand Down Expand Up @@ -112,6 +113,19 @@ class Block(PandasObject):
_verify_integrity = True
_validate_ndim = True

@classmethod
def _simple_new(
cls, values: ArrayLike, placement: BlockPlacement, ndim: int
) -> "Block":
"""
Fastpath constructor, does *no* validation
"""
obj = object.__new__(cls)
obj.ndim = ndim
obj.values = values
obj._mgr_locs = placement
return obj

def __init__(self, values, placement, ndim=None):
self.ndim = self._check_ndim(values, ndim)
self.mgr_locs = placement
Expand Down Expand Up @@ -289,13 +303,15 @@ def getitem_block(self, slicer, new_mgr_locs=None):
if new_mgr_locs is None:
axis0_slicer = slicer[0] if isinstance(slicer, tuple) else slicer
new_mgr_locs = self.mgr_locs[axis0_slicer]
elif not isinstance(new_mgr_locs, BlockPlacement):
new_mgr_locs = BlockPlacement(new_mgr_locs)

new_values = self._slice(slicer)

if self._validate_ndim and new_values.ndim != self.ndim:
raise ValueError("Only same dim slicing is allowed")

return self.make_block_same_class(new_values, new_mgr_locs)
return type(self)._simple_new(new_values, new_mgr_locs, self.ndim)

@property
def shape(self):
Expand Down