8000 PERF: put BlockManager constructor in cython by jbrockmendel · Pull Request #40842 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

PERF: put BlockManager constructor in cython #40842

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 17 commits into from
Apr 19, 2021
Merged
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
Next Next commit
REF: move BM/SBM-only methods there
  • Loading branch information
jbrockmendel committed Apr 2, 2021
commit 4cdba6b83b3af1290a49a92539c7bd26ddec95d9
109 changes: 57 additions & 52 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,45 +275,6 @@ def arrays(self) -> List[ArrayLike]:
"""
return [blk.values for blk in self.blocks]

def __getstate__(self):
block_values = [b.values for b in self.blocks]
block_items = [self.items[b.mgr_locs.indexer] for b in self.blocks]
axes_array = list(self.axes)

extra_state = {
"0.14.1": {
"axes": axes_array,
"blocks": [
{"values": b.values, "mgr_locs": b.mgr_locs.indexer}
for b in self.blocks
],
}
}

# First three elements of the state are to maintain forward
# compatibility with 0.13.1.
return axes_array, block_values, block_items, extra_state

def __setstate__(self, state):
def unpickle_block(values, mgr_locs, ndim: int):
# TODO(EA2D): ndim would be unnecessary with 2D EAs
# older pickles may store e.g. DatetimeIndex instead of DatetimeArray
values = extract_array(values, extract_numpy=True)
return new_block(values, placement=mgr_locs, ndim=ndim)

if isinstance(state, tuple) and len(state) >= 4 and "0.14.1" in state[3]:
state = state[3]["0.14.1"]
self.axes = [ensure_index(ax) for ax in state["axes"]]
ndim = len(self.axes)
self.blocks = tuple(
unpickle_block(b["values"], b["mgr_locs"], ndim=ndim)
for b in state["blocks"]
)
else:
raise NotImplementedError("pre-0.14.1 pickles are no longer supported")

self._post_setstate()

def __repr__(self) -> str:
output = type(self).__name__
for i, ax in enumerate(self.axes):
Expand All @@ -326,19 +287,6 @@ def __repr__(self) -> str:
output += f"\n{block}"
return output

def _verify_integrity(self) -> None:
mgr_shape = self.shape
tot_items = sum(len(x.mgr_locs) for x in self.blocks)
for block in self.blocks:
if block.shape[1:] != mgr_shape[1:]:
raise construction_error(tot_items, block.shape[1:], self.axes)
if len(self.items) != tot_items:
raise AssertionError(
"Number of manager items must equal union of "
f"block items\n# manager items: {len(self.items)}, # "
f"tot_items: {tot_items}"
)

def reduce(
self: T, func: Callable, ignore_failures: bool = False
) -> Tuple[T, np.ndarray]:
Expand Down Expand Up @@ -1403,6 +1351,9 @@ class BlockManager(libinternals.BlockManager, _BlockManager):

ndim = 2

# -------------------------------------------------------------------
# Constructors

def __init__(
self,
blocks: Sequence[Block],
Expand All @@ -1428,6 +1379,21 @@ def from_blocks(cls, blocks: List[Block], axes: List[Index]) -> BlockManager:
"""
return cls(blocks, axes, verify_integrity=False)

def _verify_integrity(self) -> None:
mgr_shape = self.shape
tot_items = sum(len(x.mgr_locs) for x in self.blocks)
for block in self.blocks:
if block.shape[1:] != mgr_shape[1:]:
raise construction_error(tot_items, block.shape[1:], self.axes)
if len(self.items) != tot_items:
raise AssertionError(
"Number of manager items must equal union of "
f"block items\n# manager items: {len(self.items)}, # "
f"tot_items: {tot_items}"
)

# -------------------------------------------------------------------

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

Expand Down Expand Up @@ -1559,6 +1525,45 @@ def from_array(cls, array: ArrayLike, index: Index) -> SingleBlockManager:
block = new_block(array, placement=slice(0, len(index)), ndim=1)
return cls(block, index)

def __getstate__(self):
block_values = [b.values for b in self.blocks]
block_items = [self.items[b.mgr_locs.indexer] for b in self.blocks]
axes_array = list(self.axes)

extra_state = {
"0.14.1": {
"axes": axes_array,
"blocks": [
{"values": b.values, "mgr_locs": b.mgr_locs.indexer}
for b in self.blocks
],
}
}

# First three elements of the state are to maintain forward
# compatibility with 0.13.1.
return axes_array, block_values, block_items, extra_state

def __setstate__(self, state):
def unpickle_block(values, mgr_locs, ndim: int) -> Block:
# TODO(EA2D): ndim would be unnecessary with 2D EAs
# older pickles may store e.g. DatetimeIndex instead of DatetimeArray
values = extract_array(values, extract_numpy=True)
return new_block(values, placement=mgr_locs, ndim=ndim)

if isinstance(state, tuple) and len(state) >= 4 and "0.14.1" in state[3]:
state = state[3]["0.14.1"]
self.axes = [ensure_index(ax) for ax in state["axes"]]
ndim = len(self.axes)
self.blocks = tuple(
unpickle_block(b["values"], b["mgr_locs"], ndim=ndim)
for b in state["blocks"]
)
else:
raise NotImplementedError("pre-0.14.1 pickles are no longer supported")

self._post_setstate()

def _post_setstate(self):
pass

Expand Down
0