Skip to content

Commit 9e00908

Browse files
committed
Revert "try not replacing _from_blocks with _from_array"
This reverts commit 672f9c0.
1 parent ac71b7d commit 9e00908

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

pandas/core/internals/construction.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,11 @@
7676
ArrayManager,
7777
SingleArrayManager,
7878
)
79-
from pandas.core.internals.blocks import (
80-
ensure_block_shape,
81-
new_block,
82-
)
8379
from pandas.core.internals.managers import (
8480
BlockManager,
8581
SingleBlockManager,
82+
create_block_manager_from_array,
8683
create_block_manager_from_arrays,
87-
create_block_manager_from_blocks,
8884
)
8985

9086
if TYPE_CHECKING:
@@ -340,42 +336,29 @@ def ndarray_to_mgr(
340336

341337
return ArrayManager(arrays, [index, columns], verify_integrity=False)
342338

343-
values = values.T
339+
array = values.T
344340

345341
# if we don't have a dtype specified, then try to convert objects
346342
# on the entire block; this is to convert if we have datetimelike's
347343
# embedded in an object type
348-
if dtype is None and is_object_dtype(values.dtype):
349-
350-
if values.ndim == 2 and values.shape[0] != 1:
344+
if dtype is None and is_object_dtype(array.dtype):
345+
if array.ndim == 2 and array.shape[0] != 1:
351346
# transpose and separate blocks
352-
353-
dtlike_vals = [maybe_infer_to_datetimelike(row) for row in values]
347+
maybe_datetime = [
348+
maybe_infer_to_datetimelike(instance) for instance in array
349+
]
354350
# don't convert (and copy) the objects if no type inference occurs
355351
if any(
356-
not is_dtype_equal(instance.dtype, values.dtype)
357-
for instance in dtlike_vals
352+
not is_dtype_equal(instance.dtype, array.dtype)
353+
for instance in maybe_datetime
358354
):
359-
dvals_list = [ensure_block_shape(dval, 2) for dval in dtlike_vals]
360-
block_values = [
361-
new_block(dvals_list[n], placement=n, ndim=2)
362-
for n in range(len(dvals_list))
363-
]
364-
else:
365-
nb = new_block(values, placement=slice(len(columns)), ndim=2)
366-
block_values = [nb]
355+
return create_block_manager_from_arrays(
356+
maybe_datetime, columns, [columns, index]
357+
)
367358
else:
368-
datelike_vals = maybe_infer_to_datetimelike(values)
369-
nb = new_block(datelike_vals, placement=slice(len(columns)), ndim=2)
370-
block_values = [nb]
371-
else:
372-
nb = new_block(values, placement=slice(len(columns)), ndim=2)
373-
block_values = [nb]
374-
375-
if len(columns) == 0:
376-
block_values = []
359+
array = maybe_infer_to_datetimelike(array)
377360

378-
return create_block_manager_from_blocks(block_values, [columns, index])
361+
return create_block_manager_from_array(array, [columns, index])
379362

380363

381364
def _check_values_indices_shape_match(

pandas/core/internals/managers.py

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

17931793

1794+
def create_block_manager_from_array(
1795+
array,
1796+
axes: list[Index],
1797+
consolidate: bool = True,
1798+
) -> BlockManager:
1799+
assert isinstance(axes, list)
1800+
assert all(isinstance(x, Index) for x in axes)
1801+
1802+
array = _extract_array(array)
1803+
1804+
try:
1805+
block = new_block(values=array, placement=slice(0, len(axes[0])), ndim=2)
1806+
mgr = BlockManager([block], axes)
1807+
except ValueError as e:
1808+
raise construction_error(array.shape[0], array.shape[1:], axes, e)
1809+
if consolidate:
1810+
mgr._consolidate_inplace()
1811+
return mgr
1812+
1813+
17941814
def construction_error(
17951815
tot_items: int,
17961816
block_shape: Shape,

0 commit comments

Comments
 (0)