diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 87569eb0f48cd..18b9f32eb5b19 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -866,7 +866,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 @@ -878,7 +878,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() 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]