8000 PERF: implement get_slice in cython by jbrockmendel · Pull Request #41045 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

PERF: implement get_slice in cython #41045

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 19 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
PERF: implement get_slice in cython
  • Loading branch information
jbrockmendel committed Apr 19, 2021
commit 771d798dd43506ea529ca576f1303c95df419fe3
2 changes: 2 additions & 0 deletions pandas/_libs/internals.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ class BlockManager:
_blklocs: np.ndarray

def __init__(self, blocks: tuple[B, ...], axes: list[Index], verify_integrity=True): ...

def get_slice(self: T, slobj: slice, axis: int=...) -> T: ...
29 changes: 28 additions & 1 deletion pandas/_libs/internals.pyx
< 8000 td id="diff-b90f7e53741b9a3602af71af161fc90d6f70721607460245fa6385a6a2de9ffbR610" data-line-number="610" class="blob-num blob-num-context js-linkable-line-number js-blob-rnum">
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ cdef class NumpyBlock(SharedBlock):
self.values = values

# @final # not useful in cython, but we _would_ annotate with @final
def getitem_block_index(self, slicer: slice) -> NumpyBlock:
cpdef NumpyBlock getitem_block_index(self, slice slicer):
"""
Perform __getitem__-like specialized to slicing along index.

Expand Down Expand Up @@ -610,3 +610,30 @@ cdef class BlockManager:
self._rebuild_blknos_and_blklocs()

# -------------------------------------------------------------------
# Indexing

cdef BlockManager _get_index_slice(self, slobj):
cdef:
SharedBlock blk, nb

nbs = []
for blk in self.blocks:
nb = blk.getitem_block_index(slobj)
nbs.append(nb)

new_axes = [self.axes[0], self.axes[1]._getitem_slice(slobj)]
return type(self)(tuple(nbs), new_axes, verify_integrity=False)

def get_slice(self, slobj: slice, axis: int = 0) -> BlockManager:

if axis == 0:
new_blocks = self._slice_take_blocks_ax0(slobj)
elif axis == 1:
return self._get_index_slice(slobj)
else:
raise IndexError("Requested axis not found in manager")

new_axes = list(self.axes)
new_axes[axis] = new_axes[axis]._getitem_slice(slobj)

return type(self)(tuple(new_blocks), new_axes, verify_integrity=False)
15 changes: 0 additions & 15 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1116,21 +1116,6 @@ def fast_xs(self, loc: int) -> ArrayLike:

return result

def get_slice(self, slobj: slice, axis: int = 0) -> BlockManager:
assert isinstance(slobj, slice), type(slobj)

if axis == 0:
new_blocks = self._slice_take_blocks_ax0(slobj)
elif axis == 1:
new_blocks = [blk.getitem_block_index(slobj) for blk in self.blocks]
else:
raise IndexError("Requested axis not found in manager")

new_axes = list(self.axes)
new_axes[axis] = new_axes[axis]._getitem_slice(slobj)

return type(self)(tuple(new_blocks), new_axes, verify_integrity=False)

def iget(self, i: int) -> SingleBlockManager:
"""
Return the data as a SingleBlockManager.
Expand Down
0