diff --git a/pandas/core/internals/construction.py b/pandas/core/internals/construction.py index 86834a8dccf40..63d84ab39fa96 100644 --- a/pandas/core/internals/construction.py +++ b/pandas/core/internals/construction.py @@ -386,7 +386,9 @@ def ndarray_to_mgr( if len(columns) == 0: block_values = [] - return create_block_manager_from_blocks(block_values, [columns, index]) + return create_block_manager_from_blocks( + block_values, [columns, index], verify_integrity=False + ) def _check_values_indices_shape_match( diff --git a/pandas/core/internals/managers.py b/pandas/core/internals/managers.py index ab5c4aa503a9d..d1dc2aa7bd43f 100644 --- a/pandas/core/internals/managers.py +++ b/pandas/core/internals/managers.py @@ -1553,6 +1553,11 @@ def is_consolidated(self) -> bool: return self._is_consolidated def _consolidate_check(self) -> None: + if len(self.blocks) == 1: + # fastpath + self._is_consolidated = True + self._known_consolidated = True + return dtypes = [blk.dtype for blk in self.blocks if blk._can_consolidate] self._is_consolidated = len(dtypes) == len(set(dtypes)) self._known_consolidated = True @@ -1775,10 +1780,20 @@ def _equal_values(self: T, other: T) -> bool: def create_block_manager_from_blocks( - blocks: list[Block], axes: list[Index], consolidate: bool = True + blocks: list[Block], + axes: list[Index], + consolidate: bool = True, + verify_integrity: bool = True, ) -> BlockManager: + # If verify_integrity=False, then caller is responsible for checking + # all(x.shape[-1] == len(axes[1]) for x in blocks) + # sum(x.shape[0] for x in blocks) == len(axes[0]) + # set(x for for blk in blocks for x in blk.mgr_locs) == set(range(len(axes[0]))) + # all(blk.ndim == 2 for blk in blocks) + # This allows us to safely pass verify_integrity=False + try: - mgr = BlockManager(blocks, axes) + mgr = BlockManager(blocks, axes, verify_integrity=verify_integrity) except ValueError as err: arrays = [blk.values for blk in blocks]