Skip to content

Commit

Permalink
try not replacing _from_blocks with _from_array
Browse files Browse the repository at this point in the history
  • Loading branch information
irgolic committed May 26, 2021
1 parent cb59f2a commit 672f9c0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
45 changes: 31 additions & 14 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@
ArrayManager,
SingleArrayManager,
)
from pandas.core.internals.blocks import (
ensure_block_shape,
new_block,
)
from pandas.core.internals.managers import (
BlockManager,
SingleBlockManager,
create_block_manager_from_array,
create_block_manager_from_arrays,
create_block_manager_from_blocks,
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -347,29 +351,42 @@ def ndarray_to_mgr(

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

array = values.T
values = values.T

# 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
if dtype is None and is_object_dtype(array.dtype):
if array.ndim == 2 and array.shape[0] != 1:
if dtype is None and is_object_dtype(values.dtype):

if values.ndim == 2 and values.shape[0] != 1:
# transpose and separate blocks
maybe_datetime = [
maybe_infer_to_datetimelike(instance) for instance in array
]

dtlike_vals = [maybe_infer_to_datetimelike(row) for row in values]
# don't convert (and copy) the objects if no type inference occurs
if any(
not is_dtype_equal(instance.dtype, array.dtype)
for instance in maybe_datetime
not is_dtype_equal(instance.dtype, values.dtype)
for instance in dtlike_vals
):
return create_block_manager_from_arrays(
maybe_datetime, columns, [columns, index]
)
dvals_list = [ensure_block_shape(dval, 2) for dval in dtlike_vals]
block_values = [
new_block(dvals_list[n], placement=n, ndim=2)
for n in range(len(dvals_list))
]
else:
nb = new_block(values, placement=slice(len(columns)), ndim=2)
block_values = [nb]
else:
array = maybe_infer_to_datetimelike(array)
datelike_vals = maybe_infer_to_datetimelike(values)
nb = new_block(datelike_vals, placement=slice(len(columns)), ndim=2)
block_values = [nb]
else:
nb = new_block(values, placement=slice(len(columns)), ndim=2)
block_values = [nb]

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

return create_block_manager_from_array(array, [columns, index])
return create_block_manager_from_blocks(block_values, [columns, index])


def _check_values_indices_shape_match(
Expand Down
20 changes: 0 additions & 20 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1753,26 +1753,6 @@ def create_block_manager_from_arrays(
return mgr


def create_block_manager_from_array(
array,
axes: list[Index],
consolidate: bool = True,
) -> BlockManager:
assert isinstance(axes, list)
assert all(isinstance(x, Index) for x in axes)

array = _extract_array(array)

try:
block = new_block(values=array, placement=slice(0, len(axes[0])), ndim=2)
mgr = BlockManager([block], axes)
except ValueError as e:
raise construction_error(array.shape[0], array.shape[1:], axes, e)
if consolidate:
mgr._consolidate_inplace()
return mgr


def construction_error(
tot_items: int,
block_shape: Shape,
Expand Down

0 comments on commit 672f9c0

Please sign in to comment.