Skip to content

REF: consistent arguments for create_block_manager_from_blocks #40403

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ def ndarray_to_mgr(
)
values = values.T

_check_values_indices_shape_match(values, index, columns)

# if we don't have a dtype specified, then try to convert objects
# on the entire block; this is to convert if we have datetimelike's
# embedded in an object type
Expand All @@ -317,15 +319,37 @@ def ndarray_to_mgr(
else:
datelike_vals = maybe_infer_to_datetimelike(values)
datelike_vals = maybe_squeeze_dt64tz(datelike_vals)
block_values = [datelike_vals]
nb = new_block(datelike_vals, placement=slice(len(columns)), ndim=2)
block_values = [nb]
else:
# error: List item 0 has incompatible type "Union[ExtensionArray, ndarray]";
# expected "Block"
block_values = [maybe_squeeze_dt64tz(values)] # type: ignore[list-item]
new_values = maybe_squeeze_dt64tz(values)
nb = new_block(new_values, placement=slice(len(columns)), ndim=2)
block_values = [nb]

if len(columns) == 0:
block_values = []

return create_block_manager_from_blocks(block_values, [columns, index])


def _check_values_indices_shape_match(
values: np.ndarray, index: Index, columns: Index
) -> None:
"""
Check that the shape implied by our axes matches the actual shape of the
data.
"""
if values.shape[0] != len(columns):
# Could let this raise in Block constructor, but we get a more
# helpful exception message this way.
if values.shape[1] == 0:
raise ValueError("Empty data passed with indices specified.")

passed = values.T.shape
implied = (len(index), len(columns))
raise ValueError(f"Shape of passed values is {passed}, indices imply {implied}")


def maybe_squeeze_dt64tz(dta: ArrayLike) -> ArrayLike:
"""
If we have a tzaware DatetimeArray with shape (1, N), squeeze to (N,)
Expand Down
31 changes: 10 additions & 21 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1726,30 +1726,19 @@ def set_values(self, values: ArrayLike):
# Constructor Helpers


def create_block_manager_from_blocks(blocks, axes: List[Index]) -> BlockManager:
def create_block_manager_from_blocks(
blocks: List[Block], axes: List[Index]
) -> BlockManager:
try:
if len(blocks) == 1 and not isinstance(blocks[0], Block):
# if blocks[0] is of length 0, return empty blocks
if not len(blocks[0]):
blocks = []
else:
# It's OK if a single block is passed as values, its placement
# is basically "all items", but if there're many, don't bother
# converting, it's an error anyway.
blocks = [
new_block(
values=blocks[0], placement=slice(0, len(axes[0])), ndim=2
)
]

mgr = BlockManager(blocks, axes)
mgr._consolidate_inplace()
return mgr

except ValueError as e:
blocks = [getattr(b, "values", b) for b in blocks]
tot_items = sum(b.shape[0] for b in blocks)
raise construction_error(tot_items, blocks[0].shape[1:], axes, e)
except ValueError as err:
arrays = [blk.values for blk in blocks]
tot_items = sum(arr.shape[0] for arr in arrays)
raise construction_error(tot_items, arrays[0].shape[1:], axes, err)

mgr._consolidate_inplace()
return mgr


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