Skip to content

Commit a0c39c1

Browse files
committed
BUG: 2D ndarray of dtype 'object' is always copied upon construction (pandas-dev#39263)
1 parent c322b24 commit a0c39c1

File tree

1 file changed

+11
-16
lines changed

1 file changed

+11
-16
lines changed

pandas/core/internals/construction.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -236,23 +236,18 @@ def init_ndarray(values, index, columns, dtype: Optional[DtypeObj], copy: bool):
236236
# on the entire block; this is to convert if we have datetimelike's
237237
# embedded in an object type
238238
if dtype is None and is_object_dtype(values.dtype):
239-
240239
if values.ndim == 2 and values.shape[0] != 1:
241-
# transpose and separate blocks
242-
243-
dvals_list = [maybe_infer_to_datetimelike(row) for row in values]
244-
for n in range(len(dvals_list)):
245-
if isinstance(dvals_list[n], np.ndarray):
246-
dvals_list[n] = dvals_list[n].reshape(1, -1)
247-
248-
from pandas.core.internals.blocks import make_block
249-
250-
# TODO: What about re-joining object columns?
251-
block_values = [
252-
make_block(dvals_list[n], placement=[n], ndim=2)
253-
for n in range(len(dvals_list))
254-
]
255-
240+
maybe_datetime = [maybe_infer_to_datetimelike(instance) for instance in values]
241+
# don't convert (and copy) the objects if no type inference occurs
242+
if any(
243+
not is_dtype_equal(instance.dtype, values.dtype)
244+
for instance in maybe_datetime
245+
):
246+
return create_block_manager_from_arrays(
247+
maybe_datetime, columns, [columns, index]
248+
)
249+
else:
250+
block_values = [values]
256251
else:
257252
datelike_vals = maybe_infer_to_datetimelike(values)
258253
block_values = [datelike_vals]

0 commit comments

Comments
 (0)