From 15632c750d5ab3362b719d674d3750963e46e3fa Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Mon, 29 Nov 2021 22:49:26 +0100 Subject: [PATCH 1/2] CLN: helper attribute to check for ArrayManager --- pandas/core/frame.py | 4 ++-- pandas/core/generic.py | 10 +++++++++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 8c85c4e961d99..ede9d836506cb 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -858,7 +858,7 @@ def _is_homogeneous_type(self) -> bool: ... "B": np.array([1, 2], dtype=np.int64)})._is_homogeneous_type False """ - if isinstance(self._mgr, ArrayManager): + if self._using_array_manager: return len({arr.dtype for arr in self._mgr.arrays}) == 1 if self._mgr.any_extension_types: return len({block.dtype for block in self._mgr.blocks}) == 1 @@ -870,7 +870,7 @@ def _can_fast_transpose(self) -> bool: """ Can we transpose this DataFrame without creating any new array objects. """ - if isinstance(self._mgr, ArrayManager): + if self._using_array_manager: return False blocks = self._mgr.blocks if len(blocks) != 1: diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 4aff7acc4c6fb..bdc8fe7e4e5f5 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -321,6 +321,14 @@ def _as_manager(self: NDFrameT, typ: str, copy: bool_t = True) -> NDFrameT: # fastpath of passing a manager doesn't check the option/manager class return self._constructor(new_mgr).__finalize__(self) + @property + def _using_array_manager(self) -> bool_t: + """ + Private attribute to quickly check if the DataFrame/Series is + backed by an ArrayManager or not. + """ + return isinstance(self._mgr, (ArrayManager, SingleArrayManager)) + # ---------------------------------------------------------------------- # attrs and flags @@ -5606,7 +5614,7 @@ def _protect_consolidate(self, f): Consolidate _mgr -- if the blocks have changed, then clear the cache """ - if isinstance(self._mgr, (ArrayManager, SingleArrayManager)): + if self._using_array_manager: return f() blocks_before = len(self._mgr.blocks) result = f() From dd7df72806c24ad7c5920b5d55d64cd18a83fd23 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Tue, 30 Nov 2021 08:43:41 +0100 Subject: [PATCH 2/2] replace getattr in indexing --- pandas/core/indexing.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index fc2204724aceb..369a90af22c34 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -1552,11 +1552,7 @@ def _setitem_with_indexer(self, indexer, value, name="iloc"): # if there is only one block/type, still have to take split path # unless the block is one-dimensional or it can hold the value - if ( - not take_split_path - and getattr(self.obj._mgr, "blocks", False) - and self.ndim > 1 - ): + if not take_split_path and not self.obj._using_array_manager and self.ndim > 1: # in case of dict, keys are indices val = list(value.values()) if isinstance(value, dict) else value blk = self.obj._mgr.blocks[0]