Skip to content

Commit 672f9c0

Browse files
committed
try not replacing _from_blocks with _from_array
1 parent cb59f2a commit 672f9c0

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

pandas/core/internals/construction.py

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,15 @@
8080
ArrayManager,
8181
SingleArrayManager,
8282
)
83+
from pandas.core.internals.blocks import (
84+
ensure_block_shape,
85+
new_block,
86+
)
8387
from pandas.core.internals.managers import (
8488
BlockManager,
8589
SingleBlockManager,
86-
create_block_manager_from_array,
8790
create_block_manager_from_arrays,
91+
create_block_manager_from_blocks,
8892
)
8993

9094
if TYPE_CHECKING:
@@ -347,29 +351,42 @@ def ndarray_to_mgr(
347351

348352
return ArrayManager(arrays, [index, columns], verify_integrity=False)
349353

350-
array = values.T
354+
values = values.T
351355

352356
# if we don't have a dtype specified, then try to convert objects
353357
# on the entire block; this is to convert if we have datetimelike's
354358
# embedded in an object type
355-
if dtype is None and is_object_dtype(array.dtype):
356-
if array.ndim == 2 and array.shape[0] != 1:
359+
if dtype is None and is_object_dtype(values.dtype):
360+
361+
if values.ndim == 2 and values.shape[0] != 1:
357362
# transpose and separate blocks
358-
maybe_datetime = [
359-
maybe_infer_to_datetimelike(instance) for instance in array
360-
]
363+
364+
dtlike_vals = [maybe_infer_to_datetimelike(row) for row in values]
361365
# don't convert (and copy) the objects if no type inference occurs
362366
if any(
363-
not is_dtype_equal(instance.dtype, array.dtype)
364-
for instance in maybe_datetime
367+
not is_dtype_equal(instance.dtype, values.dtype)
368+
for instance in dtlike_vals
365369
):
366-
return create_block_manager_from_arrays(
367-
maybe_datetime, columns, [columns, index]
368-
)
370+
dvals_list = [ensure_block_shape(dval, 2) for dval in dtlike_vals]
371+
block_values = [
372+
new_block(dvals_list[n], placement=n, ndim=2)
373+
for n in range(len(dvals_list))
374+
]
375+
else:
376+
nb = new_block(values, placement=slice(len(columns)), ndim=2)
377+
block_values = [nb]
369378
else:
370-
array = maybe_infer_to_datetimelike(array)
379+
datelike_vals = maybe_infer_to_datetimelike(values)
380+
nb = new_block(datelike_vals, placement=slice(len(columns)), ndim=2)
381+
block_values = [nb]
382+
else:
383+
nb = new_block(values, placement=slice(len(columns)), ndim=2)
384+
block_values = [nb]
385+
386+
if len(columns) == 0:
387+
block_values = []
371388

372-
return create_block_manager_from_array(array, [columns, index])
389+
return create_block_manager_from_blocks(block_values, [columns, index])
373390

374391

375392
def _check_values_indices_shape_match(

pandas/core/internals/managers.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,26 +1753,6 @@ def create_block_manager_from_arrays(
17531753
return mgr
17541754

17551755

1756-
def create_block_manager_from_array(
1757-
array,
1758-
axes: list[Index],
1759-
consolidate: bool = True,
1760-
) -> BlockManager:
1761-
assert isinstance(axes, list)
1762-
assert all(isinstance(x, Index) for x in axes)
1763-
1764-
array = _extract_array(array)
1765-
1766-
try:
1767-
block = new_block(values=array, placement=slice(0, len(axes[0])), ndim=2)
1768-
mgr = BlockManager([block], axes)
1769-
except ValueError as e:
1770-
raise construction_error(array.shape[0], array.shape[1:], axes, e)
1771-
if consolidate:
1772-
mgr._consolidate_inplace()
1773-
return mgr
1774-
1775-
17761756
def construction_error(
17771757
tot_items: int,
17781758
block_shape: Shape,

0 commit comments

Comments
 (0)