From fed69e58c49f21cf9b217257a1f5a75488c42f97 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 22 Jun 2020 12:46:43 -0700 Subject: [PATCH 1/5] REF: clarify is_mixed_dtype/is_single_block --- pandas/core/internals/blocks.py | 9 ++------- pandas/core/internals/managers.py | 4 +++- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/pandas/core/internals/blocks.py b/pandas/core/internals/blocks.py index 38c495e1dd0f3..0c98a779424bd 100644 --- a/pandas/core/internals/blocks.py +++ b/pandas/core/internals/blocks.py @@ -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 """ @@ -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: @@ -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: diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index eaf59051205d6..602824af3ea1c 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -220,6 +220,7 @@ def set_axis(self, axis: int, new_labels: Index) -> None: @property def _is_single_block(self) -> bool: + # Assumes we are 2D; overriden by SingleBlockManager if self.ndim == 1: return True @@ -663,7 +664,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 self._is_single_block @property def is_numeric_mixed_type(self) -> bool: @@ -1486,6 +1487,7 @@ class SingleBlockManager(BlockManager): _is_consolidated = True _known_consolidated = True __slots__ = () + _is_single_block = True def __init__( self, From 9b58d5e84218c000cf43f35d4c82197f17bcfc42 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 22 Jun 2020 12:48:10 -0700 Subject: [PATCH 2/5] simplify _is_single_block --- pandas/core/internals/managers.py | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index 602824af3ea1c..fe2b92fb9e209 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -221,16 +221,7 @@ def set_axis(self, axis: int, new_labels: Index) -> None: @property def _is_single_block(self) -> bool: # Assumes we are 2D; overriden by SingleBlockManager - 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 - ) + return len(self.blocks) <= 1 def _rebuild_blknos_and_blklocs(self) -> None: """ From 1e28584b05b30fddb5888070d706f00346fd0f62 Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 22 Jun 2020 12:52:30 -0700 Subject: [PATCH 3/5] typo fixup --- pandas/core/internals/managers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index fe2b92fb9e209..aaf65ce874397 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -655,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 self._is_single_block + return not self._is_single_block @property def is_numeric_mixed_type(self) -> bool: From 012d55393ee833680e65f40c78b91184cfd0a54a Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 22 Jun 2020 13:19:26 -0700 Subject: [PATCH 4/5] fix for zero-blocks --- pandas/core/internals/concat.py | 2 +- pandas/core/internals/managers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index fd8c5f5e27c02..a0e91e67ad5a3 100644 --- a/pandas/core/internals/concat.py +++ b/pandas/core/internals/concat.py @@ -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))] diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index aaf65ce874397..c0902ea0f9ed0 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1284,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"): From f75920b06b5ce2db46e2586152ac46f35ecc83d1 Mon Sep 17 00:00:00 2001 From: Brock Date: Tue, 23 Jun 2020 10:48:17 -0700 Subject: [PATCH 5/5] update to require exactly one block --- pandas/core/internals/concat.py | 2 +- pandas/core/internals/managers.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pandas/core/internals/concat.py b/pandas/core/internals/concat.py index a0e91e67ad5a3..fd8c5f5e27c02 100644 --- a/pandas/core/internals/concat.py +++ b/pandas/core/internals/concat.py @@ -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 and len(mgr.blocks): + if mgr._is_single_block: blk = mgr.blocks[0] return [(blk.mgr_locs, JoinUnit(blk, mgr_shape, indexers))] diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index c0902ea0f9ed0..6055a6205d286 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -221,7 +221,7 @@ def set_axis(self, axis: int, new_labels: Index) -> None: @property def _is_single_block(self) -> bool: # Assumes we are 2D; overriden by SingleBlockManager - return len(self.blocks) <= 1 + return len(self.blocks) == 1 def _rebuild_blknos_and_blklocs(self) -> None: """ @@ -655,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 not self._is_single_block + return len(self.blocks) > 1 @property def is_numeric_mixed_type(self) -> bool: @@ -1284,7 +1284,7 @@ def _slice_take_blocks_ax0( slice_or_indexer, self.shape[0], allow_fill=allow_fill ) - if self._is_single_block and len(self.blocks): + if self._is_single_block: blk = self.blocks[0] if sl_type in ("slice", "mask"):