Skip to content

Commit cb31e5a

Browse files
authored
REF: consistent arguments for create_block_manager_from_blocks (#40403)
1 parent edbc6ea commit cb31e5a

File tree

2 files changed

+38
-25
lines changed

2 files changed

+38
-25
lines changed

pandas/core/internals/construction.py

+28-4
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ def ndarray_to_mgr(
297297
)
298298
values = values.T
299299

300+
_check_values_indices_shape_match(values, index, columns)
301+
300302
# if we don't have a dtype specified, then try to convert objects
301303
# on the entire block; this is to convert if we have datetimelike's
302304
# embedded in an object type
@@ -318,15 +320,37 @@ def ndarray_to_mgr(
318320
else:
319321
datelike_vals = maybe_infer_to_datetimelike(values)
320322
datelike_vals = maybe_squeeze_dt64tz(datelike_vals)
321-
block_values = [datelike_vals]
323+
nb = new_block(datelike_vals, placement=slice(len(columns)), ndim=2)
324+
block_values = [nb]
322325
else:
323-
# error: List item 0 has incompatible type "Union[ExtensionArray, ndarray]";
324-
# expected "Block"
325-
block_values = [maybe_squeeze_dt64tz(values)] # type: ignore[list-item]
326+
new_values = maybe_squeeze_dt64tz(values)
327+
nb = new_block(new_values, placement=slice(len(columns)), ndim=2)
328+
block_values = [nb]
329+
330+
if len(columns) == 0:
331+
block_values = []
326332

327333
return create_block_manager_from_blocks(block_values, [columns, index])
328334

329335

336+
def _check_values_indices_shape_match(
337+
values: np.ndarray, index: Index, columns: Index
338+
) -> None:
339+
"""
340+
Check that the shape implied by our axes matches the actual shape of the
341+
data.
342+
"""
343+
if values.shape[0] != len(columns):
344+
# Could let this raise in Block constructor, but we get a more
345+
# helpful exception message this way.
346+
if values.shape[1] == 0:
347+
raise ValueError("Empty data passed with indices specified.")
348+
349+
passed = values.T.shape
350+
implied = (len(index), len(columns))
351+
raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")
352+
353+
330354
def maybe_squeeze_dt64tz(dta: ArrayLike) -> ArrayLike:
331355
"""
332356
If we have a tzaware DatetimeArray with shape (1, N), squeeze to (N,)

pandas/core/internals/managers.py

+10-21
Original file line numberDiff line numberDiff line change
@@ -1735,30 +1735,19 @@ def set_values(self, values: ArrayLike):
17351735
# Constructor Helpers
17361736

17371737

1738-
def create_block_manager_from_blocks(blocks, axes: List[Index]) -> BlockManager:
1738+
def create_block_manager_from_blocks(
1739+
blocks: List[Block], axes: List[Index]
1740+
) -> BlockManager:
17391741
try:
1740-
if len(blocks) == 1 and not isinstance(blocks[0], Block):
1741-
# if blocks[0] is of length 0, return empty blocks
1742-
if not len(blocks[0]):
1743-
blocks = []
1744-
else:
1745-
# It's OK if a single block is passed as values, its placement
1746-
# is basically "all items", but if there're many, don't bother
1747-
# converting, it's an error anyway.
1748-
blocks = [
1749-
new_block(
1750-
values=blocks[0], placement=slice(0, len(axes[0])), ndim=2
1751-
)
1752-
]
1753-
17541742
mgr = BlockManager(blocks, axes)
1755-
mgr._consolidate_inplace()
1756-
return mgr
17571743

1758-
except ValueError as e:
1759-
blocks = [getattr(b, "values", b) for b in blocks]
1760-
tot_items = sum(b.shape[0] for b in blocks)
1761-
raise construction_error(tot_items, blocks[0].shape[1:], axes, e)
1744+
except ValueError as err:
1745+
arrays = [blk.values for blk in blocks]
1746+
tot_items = sum(arr.shape[0] for arr in arrays)
1747+
raise construction_error(tot_items, arrays[0].shape[1:], axes, err)
1748+
1749+
mgr._consolidate_inplace()
1750+
return mgr
17621751

17631752

17641753
# We define this here so we can override it in tests.extension.test_numpy

0 commit comments

Comments
 (0)