Skip to content

Commit 9a81226

Browse files
authored
PERF: DataFrame(ndarray) (#43307)
1 parent 375267f commit 9a81226

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

pandas/core/internals/construction.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,9 @@ def ndarray_to_mgr(
386386
if len(columns) == 0:
387387
block_values = []
388388

389-
return create_block_manager_from_blocks(block_values, [columns, index])
389+
return create_block_manager_from_blocks(
390+
block_values, [columns, index], verify_integrity=False
391+
)
390392

391393

392394
def _check_values_indices_shape_match(

pandas/core/internals/managers.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,11 @@ def is_consolidated(self) -> bool:
15531553
return self._is_consolidated
15541554

15551555
def _consolidate_check(self) -> None:
1556+
if len(self.blocks) == 1:
1557+
# fastpath
1558+
self._is_consolidated = True
1559+
self._known_consolidated = True
1560+
return
15561561
dtypes = [blk.dtype for blk in self.blocks if blk._can_consolidate]
15571562
self._is_consolidated = len(dtypes) == len(set(dtypes))
15581563
self._known_consolidated = True
@@ -1775,10 +1780,20 @@ def _equal_values(self: T, other: T) -> bool:
17751780

17761781

17771782
def create_block_manager_from_blocks(
1778-
blocks: list[Block], axes: list[Index], consolidate: bool = True
1783+
blocks: list[Block],
1784+
axes: list[Index],
1785+
consolidate: bool = True,
1786+
verify_integrity: bool = True,
17791787
) -> BlockManager:
1788+
# If verify_integrity=False, then caller is responsible for checking
1789+
# all(x.shape[-1] == len(axes[1]) for x in blocks)
1790+
# sum(x.shape[0] for x in blocks) == len(axes[0])
1791+
# set(x for for blk in blocks for x in blk.mgr_locs) == set(range(len(axes[0])))
1792+
# all(blk.ndim == 2 for blk in blocks)
1793+
# This allows us to safely pass verify_integrity=False
1794+
17801795
try:
1781-
mgr = BlockManager(blocks, axes)
1796+
mgr = BlockManager(blocks, axes, verify_integrity=verify_integrity)
17821797

17831798
except ValueError as err:
17841799
arrays = [blk.values for blk in blocks]

0 commit comments

Comments
 (0)