|
60 | 60 | union_indexes,
|
61 | 61 | )
|
62 | 62 | from pandas.core.internals.managers import (
|
| 63 | + create_block_manager_from_array, |
63 | 64 | create_block_manager_from_arrays,
|
64 |
| - create_block_manager_from_blocks, |
65 | 65 | )
|
66 | 66 |
|
67 | 67 | if TYPE_CHECKING:
|
@@ -230,36 +230,29 @@ def init_ndarray(values, index, columns, dtype: Optional[DtypeObj], copy: bool):
|
230 | 230 | index, columns = _get_axes(
|
231 | 231 | values.shape[0], values.shape[1], index=index, columns=columns
|
232 | 232 | )
|
233 |
| - values = values.T |
| 233 | + |
| 234 | + array = values.T |
234 | 235 |
|
235 | 236 | # if we don't have a dtype specified, then try to convert objects
|
236 | 237 | # on the entire block; this is to convert if we have datetimelike's
|
237 | 238 | # embedded in an object type
|
238 |
| - if dtype is None and is_object_dtype(values.dtype): |
239 |
| - |
240 |
| - 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)) |
| 239 | + if dtype is None and is_object_dtype(array.dtype): |
| 240 | + if array.ndim == 2 and array.shape[0] != 1: |
| 241 | + maybe_datetime = [ |
| 242 | + maybe_infer_to_datetimelike(instance) for instance in array |
254 | 243 | ]
|
255 |
| - |
| 244 | + # don't convert (and copy) the objects if no type inference occurs |
| 245 | + if any( |
| 246 | + not is_dtype_equal(instance.dtype, array.dtype) |
| 247 | + for instance in maybe_datetime |
| 248 | + ): |
| 249 | + return create_block_manager_from_arrays( |
| 250 | + maybe_datetime, columns, [columns, index] |
| 251 | + ) |
256 | 252 | else:
|
257 |
| - datelike_vals = maybe_infer_to_datetimelike(values) |
258 |
| - block_values = [datelike_vals] |
259 |
| - else: |
260 |
| - block_values = [values] |
| 253 | + array = maybe_infer_to_datetimelike(array) |
261 | 254 |
|
262 |
| - return create_block_manager_from_blocks(block_values, [columns, index]) |
| 255 | + return create_block_manager_from_array(array, [columns, index]) |
263 | 256 |
|
264 | 257 |
|
265 | 258 | def init_dict(data: Dict, index, columns, dtype: Optional[DtypeObj] = None):
|
|
0 commit comments