Skip to content

Commit f7d1f28

Browse files
committed
my suggestion
`create_manager_from_block_values` can be deprecated after this change
1 parent 483202e commit f7d1f28

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

pandas/core/internals/construction.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
union_indexes,
6161
)
6262
from pandas.core.internals.managers import (
63+
create_block_manager_from_array,
6364
create_block_manager_from_arrays,
64-
create_block_manager_from_blocks,
6565
)
6666

6767
if TYPE_CHECKING:
@@ -249,14 +249,13 @@ def init_ndarray(values, index, columns, dtype: Optional[DtypeObj], copy: bool):
249249
maybe_datetime, columns, [columns, index]
250250
)
251251
else:
252-
block_values = [values]
252+
array = values
253253
else:
254-
datelike_vals = maybe_infer_to_datetimelike(values)
255-
block_values = [datelike_vals]
254+
array = maybe_infer_to_datetimelike(values)
256255
else:
257-
block_values = [values]
256+
array = values
258257

259-
return create_block_manager_from_blocks(block_values, [columns, index])
258+
return create_block_manager_from_array(array, [columns, index])
260259

261260

262261
def init_dict(data: Dict, index, columns, dtype: Optional[DtypeObj] = None):

pandas/core/internals/managers.py

+23-14
Original file line numberDiff line numberDiff line change
@@ -1634,21 +1634,17 @@ def fast_xs(self, loc):
16341634

16351635

16361636
def create_block_manager_from_blocks(blocks, axes: List[Index]) -> BlockManager:
1637-
try:
1638-
if len(blocks) == 1 and not isinstance(blocks[0], Block):
1639-
# if blocks[0] is of length 0, return empty blocks
1640-
if not len(blocks[0]):
1641-
blocks = []
1642-
else:
1643-
# It's OK if a single block is passed as values, its placement
1644-
# is basically "all items", but if there're many, don't bother
1645-
# converting, it's an error anyway.
1646-
blocks = [
1647-
make_block(
1648-
values=blocks[0], placement=slice(0, len(axes[0])), ndim=2
1649-
)
1650-
]
1637+
if len(blocks) == 1 and not isinstance(blocks[0], Block):
1638+
# if blocks[0] is of length 0, return empty blocks
1639+
if not len(blocks[0]):
1640+
blocks = []
1641+
else:
1642+
# It's OK if a single block is passed as values, its placement
1643+
# is basically "all items", but if there're many, don't bother
1644+
# converting, it's an error anyway.
1645+
return create_block_manager_from_array(blocks[0], axes)
16511646

1647+
try:
16521648
mgr = BlockManager(blocks, axes)
16531649
mgr._consolidate_inplace()
16541650
return mgr
@@ -1678,6 +1674,19 @@ def create_block_manager_from_arrays(
16781674
raise construction_error(len(arrays), arrays[0].shape, axes, e)
16791675

16801676

1677+
def create_block_manager_from_array(
1678+
array,
1679+
axes: List[Index],
1680+
) -> BlockManager:
1681+
try:
1682+
block = make_block(values=array, placement=slice(0, len(axes[0])), ndim=2)
1683+
mgr = BlockManager([block], axes)
1684+
mgr._consolidate_inplace()
1685+
except ValueError as e:
1686+
raise construction_error(array.shape[0], array.shape[1:], axes, e)
1687+
return mgr
1688+
1689+
16811690
def construction_error(tot_items, block_shape, axes, e=None):
16821691
""" raise a helpful message about our construction """
16831692
passed = tuple(map(int, [tot_items] + list(block_shape)))

0 commit comments

Comments
 (0)