Skip to content

REF: simplify _is_single_block/is_mixed_type #34935

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 6 commits into from
Jun 23, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 2 additions & 7 deletions pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ def _holder(self):
def _consolidate_key(self):
return (self._can_consolidate, self.dtype.name)

@property
def _is_single_block(self) -> bool:
return self.ndim == 1

@property
def is_view(self) -> bool:
""" return a boolean if I am possibly a view """
Expand Down Expand Up @@ -259,7 +255,7 @@ def make_block_same_class(self, values, placement=None, ndim=None):
def __repr__(self) -> str:
# don't want to print out all of the items here
name = type(self).__name__
if self._is_single_block:
if self.ndim == 1:
result = f"{name}: {len(self)} dtype: {self.dtype}"
else:

Expand Down Expand Up @@ -476,8 +472,7 @@ def downcast(self, dtypes=None):

values = self.values

# single block handling
if self._is_single_block:
if self.ndim == 1:

# try to cast all non-floats here
if dtypes is None:
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/internals/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _get_mgr_concatenation_plan(mgr, indexers):
blklocs = algos.take_1d(mgr.blklocs, ax0_indexer, fill_value=-1)
else:

if mgr._is_single_block:
if mgr._is_single_block and len(mgr.blocks):
blk = mgr.blocks[0]
return [(blk.mgr_locs, JoinUnit(blk, mgr_shape, indexers))]

Expand Down
17 changes: 5 additions & 12 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,16 +220,8 @@ def set_axis(self, axis: int, new_labels: Index) -> None:

@property
def _is_single_block(self) -> bool:
if self.ndim == 1:
return True

if len(self.blocks) != 1:
return False

blk = self.blocks[0]
return blk.mgr_locs.is_slice_like and blk.mgr_locs.as_slice == slice(
0, len(self), 1
)
# Assumes we are 2D; overriden by SingleBlockManager
return len(self.blocks) <= 1
Copy link
Member

Choose a reason for hiding this comment

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

Is there a particular reason for this to be lte instead of eq? From the name I think the latter is more apt (and what it did previously?)

Copy link
Member Author

Choose a reason for hiding this comment

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

good point, will update


def _rebuild_blknos_and_blklocs(self) -> None:
"""
Expand Down Expand Up @@ -663,7 +655,7 @@ def _consolidate_check(self) -> None:
def is_mixed_type(self) -> bool:
# Warning, consolidation needs to get checked upstairs
self._consolidate_inplace()
return len(self.blocks) > 1
return not self._is_single_block

@property
def is_numeric_mixed_type(self) -> bool:
Expand Down Expand Up @@ -1292,7 +1284,7 @@ def _slice_take_blocks_ax0(
slice_or_indexer, self.shape[0], allow_fill=allow_fill
)

if self._is_single_block:
if self._is_single_block and len(self.blocks):
blk = self.blocks[0]

if sl_type in ("slice", "mask"):
Expand Down Expand Up @@ -1486,6 +1478,7 @@ class SingleBlockManager(BlockManager):
_is_consolidated = True
_known_consolidated = True
__slots__ = ()
_is_single_block = True

def __init__(
self,
Expand Down