Skip to content

Commit

Permalink
BUG: 2D ndarray of dtype 'object' is always copied upon construction (p…
Browse files Browse the repository at this point in the history
  • Loading branch information
irgolic committed Jan 22, 2021
1 parent c322b24 commit 8ea96ea
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions pandas/core/internals/construction.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,23 +236,20 @@ def init_ndarray(values, index, columns, dtype: Optional[DtypeObj], copy: bool):
# 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(values.dtype):

if values.ndim == 2 and values.shape[0] != 1:
# transpose and separate blocks

dvals_list = [maybe_infer_to_datetimelike(row) for row in values]
for n in range(len(dvals_list)):
if isinstance(dvals_list[n], np.ndarray):
dvals_list[n] = dvals_list[n].reshape(1, -1)

from pandas.core.internals.blocks import make_block

# TODO: What about re-joining object columns?
block_values = [
make_block(dvals_list[n], placement=[n], ndim=2)
for n in range(len(dvals_list))
maybe_datetime = [
maybe_infer_to_datetimelike(instance) for instance in values
]

# don't convert (and copy) the objects if no type inference occurs
if any(
not is_dtype_equal(instance.dtype, values.dtype)
for instance in maybe_datetime
):
return create_block_manager_from_arrays(
maybe_datetime, columns, [columns, index]
)
else:
block_values = [values]
else:
datelike_vals = maybe_infer_to_datetimelike(values)
block_values = [datelike_vals]
Expand Down

0 comments on commit 8ea96ea

Please sign in to comment.