|
80 | 80 | ArrayManager,
|
81 | 81 | SingleArrayManager,
|
82 | 82 | )
|
| 83 | +from pandas.core.internals.blocks import ( |
| 84 | + ensure_block_shape, |
| 85 | + new_block, |
| 86 | +) |
83 | 87 | from pandas.core.internals.managers import (
|
84 | 88 | BlockManager,
|
85 | 89 | SingleBlockManager,
|
86 |
| - create_block_manager_from_array, |
87 | 90 | create_block_manager_from_arrays,
|
| 91 | + create_block_manager_from_blocks, |
88 | 92 | )
|
89 | 93 |
|
90 | 94 | if TYPE_CHECKING:
|
@@ -347,29 +351,42 @@ def ndarray_to_mgr(
|
347 | 351 |
|
348 | 352 | return ArrayManager(arrays, [index, columns], verify_integrity=False)
|
349 | 353 |
|
350 |
| - array = values.T |
| 354 | + values = values.T |
351 | 355 |
|
352 | 356 | # if we don't have a dtype specified, then try to convert objects
|
353 | 357 | # on the entire block; this is to convert if we have datetimelike's
|
354 | 358 | # embedded in an object type
|
355 |
| - if dtype is None and is_object_dtype(array.dtype): |
356 |
| - if array.ndim == 2 and array.shape[0] != 1: |
| 359 | + if dtype is None and is_object_dtype(values.dtype): |
| 360 | + |
| 361 | + if values.ndim == 2 and values.shape[0] != 1: |
357 | 362 | # transpose and separate blocks
|
358 |
| - maybe_datetime = [ |
359 |
| - maybe_infer_to_datetimelike(instance) for instance in array |
360 |
| - ] |
| 363 | + |
| 364 | + dtlike_vals = [maybe_infer_to_datetimelike(row) for row in values] |
361 | 365 | # don't convert (and copy) the objects if no type inference occurs
|
362 | 366 | if any(
|
363 |
| - not is_dtype_equal(instance.dtype, array.dtype) |
364 |
| - for instance in maybe_datetime |
| 367 | + not is_dtype_equal(instance.dtype, values.dtype) |
| 368 | + for instance in dtlike_vals |
365 | 369 | ):
|
366 |
| - return create_block_manager_from_arrays( |
367 |
| - maybe_datetime, columns, [columns, index] |
368 |
| - ) |
| 370 | + dvals_list = [ensure_block_shape(dval, 2) for dval in dtlike_vals] |
| 371 | + block_values = [ |
| 372 | + new_block(dvals_list[n], placement=n, ndim=2) |
| 373 | + for n in range(len(dvals_list)) |
| 374 | + ] |
| 375 | + else: |
| 376 | + nb = new_block(values, placement=slice(len(columns)), ndim=2) |
| 377 | + block_values = [nb] |
369 | 378 | else:
|
370 |
| - array = maybe_infer_to_datetimelike(array) |
| 379 | + datelike_vals = maybe_infer_to_datetimelike(values) |
| 380 | + nb = new_block(datelike_vals, placement=slice(len(columns)), ndim=2) |
| 381 | + block_values = [nb] |
| 382 | + else: |
| 383 | + nb = new_block(values, placement=slice(len(columns)), ndim=2) |
| 384 | + block_values = [nb] |
| 385 | + |
| 386 | + if len(columns) == 0: |
| 387 | + block_values = [] |
371 | 388 |
|
372 |
| - return create_block_manager_from_array(array, [columns, index]) |
| 389 | + return create_block_manager_from_blocks(block_values, [columns, index]) |
373 | 390 |
|
374 | 391 |
|
375 | 392 | def _check_values_indices_shape_match(
|
|
0 commit comments