|
53 | 53 | )
|
54 | 54 | from pandas.core.arrays import (
|
55 | 55 | Categorical,
|
56 |
| - DatetimeArray, |
57 | 56 | ExtensionArray,
|
58 |
| - TimedeltaArray, |
59 | 57 | )
|
60 | 58 | from pandas.core.construction import (
|
61 | 59 | ensure_wrapped_if_datetimelike,
|
@@ -277,14 +275,20 @@ def ndarray_to_mgr(
|
277 | 275 |
|
278 | 276 | return arrays_to_mgr(values, columns, index, dtype=dtype, typ=typ)
|
279 | 277 |
|
280 |
| - elif is_extension_array_dtype(vdtype) and not is_1d_only_ea_dtype(vdtype): |
281 |
| - # i.e. Datetime64TZ, PeriodDtype |
| 278 | + elif is_extension_array_dtype(vdtype): |
| 279 | + # i.e. Datetime64TZ, PeriodDtype; cases with is_1d_only_ea_dtype(vdtype) |
| 280 | + # are already caught above |
282 | 281 | values = extract_array(values, extract_numpy=True)
|
283 | 282 | if copy:
|
284 | 283 | values = values.copy()
|
285 | 284 | if values.ndim == 1:
|
286 | 285 | values = values.reshape(-1, 1)
|
287 | 286 |
|
| 287 | + elif isinstance(values, (np.ndarray, ExtensionArray, ABCSeries, Index)): |
| 288 | + # drop subclass info |
| 289 | + values = np.array(values, copy=copy_on_sanitize) |
| 290 | + values = _ensure_2d(values) |
| 291 | + |
288 | 292 | else:
|
289 | 293 | # by definition an array here
|
290 | 294 | # the dtypes will be coerced to a single dtype
|
@@ -496,51 +500,50 @@ def treat_as_nested(data) -> bool:
|
496 | 500 | # ---------------------------------------------------------------------
|
497 | 501 |
|
498 | 502 |
|
499 |
| -def _prep_ndarraylike( |
500 |
| - values, copy: bool = True |
501 |
| -) -> np.ndarray | DatetimeArray | TimedeltaArray: |
502 |
| - if isinstance(values, TimedeltaArray) or ( |
503 |
| - isinstance(values, DatetimeArray) and values.tz is None |
504 |
| - ): |
505 |
| - # By retaining DTA/TDA instead of unpacking, we end up retaining non-nano |
506 |
| - pass |
507 |
| - |
508 |
| - elif not isinstance(values, (np.ndarray, ABCSeries, Index)): |
509 |
| - if len(values) == 0: |
510 |
| - return np.empty((0, 0), dtype=object) |
511 |
| - elif isinstance(values, range): |
512 |
| - arr = range_to_ndarray(values) |
513 |
| - return arr[..., np.newaxis] |
514 |
| - |
515 |
| - def convert(v): |
516 |
| - if not is_list_like(v) or isinstance(v, ABCDataFrame): |
517 |
| - return v |
518 |
| - |
519 |
| - v = extract_array(v, extract_numpy=True) |
520 |
| - res = maybe_convert_platform(v) |
521 |
| - return res |
522 |
| - |
523 |
| - # we could have a 1-dim or 2-dim list here |
524 |
| - # this is equiv of np.asarray, but does object conversion |
525 |
| - # and platform dtype preservation |
526 |
| - if is_list_like(values[0]): |
527 |
| - values = np.array([convert(v) for v in values]) |
528 |
| - elif isinstance(values[0], np.ndarray) and values[0].ndim == 0: |
529 |
| - # GH#21861 see test_constructor_list_of_lists |
530 |
| - values = np.array([convert(v) for v in values]) |
531 |
| - else: |
532 |
| - values = convert(values) |
533 |
| - |
| 503 | +def _prep_ndarraylike(values, copy: bool = True) -> np.ndarray: |
| 504 | + # values is specifically _not_ ndarray, EA, Index, or Series |
| 505 | + # We only get here with `not treat_as_nested(values)` |
| 506 | + |
| 507 | + if len(values) == 0: |
| 508 | + return np.empty((0, 0), dtype=object) |
| 509 | + elif isinstance(values, range): |
| 510 | + arr = range_to_ndarray(values) |
| 511 | + return arr[..., np.newaxis] |
| 512 | + |
| 513 | + def convert(v): |
| 514 | + if not is_list_like(v) or isinstance(v, ABCDataFrame): |
| 515 | + return v |
| 516 | + |
| 517 | + v = extract_array(v, extract_numpy=True) |
| 518 | + res = maybe_convert_platform(v) |
| 519 | + # We don't do maybe_infer_to_datetimelike here bc we will end up doing |
| 520 | + # it column-by-column in ndarray_to_mgr |
| 521 | + return res |
| 522 | + |
| 523 | + # we could have a 1-dim or 2-dim list here |
| 524 | + # this is equiv of np.asarray, but does object conversion |
| 525 | + # and platform dtype preservation |
| 526 | + # does not convert e.g. [1, "a", True] to ["1", "a", "True"] like |
| 527 | + # np.asarray would |
| 528 | + if is_list_like(values[0]): |
| 529 | + values = np.array([convert(v) for v in values]) |
| 530 | + elif isinstance(values[0], np.ndarray) and values[0].ndim == 0: |
| 531 | + # GH#21861 see test_constructor_list_of_lists |
| 532 | + values = np.array([convert(v) for v in values]) |
534 | 533 | else:
|
| 534 | + values = convert(values) |
535 | 535 |
|
536 |
| - # drop subclass info |
537 |
| - values = np.array(values, copy=copy) |
| 536 | + return _ensure_2d(values) |
538 | 537 |
|
| 538 | + |
| 539 | +def _ensure_2d(values: np.ndarray) -> np.ndarray: |
| 540 | + """ |
| 541 | + Reshape 1D values, raise on anything else other than 2D. |
| 542 | + """ |
539 | 543 | if values.ndim == 1:
|
540 | 544 | values = values.reshape((values.shape[0], 1))
|
541 | 545 | elif values.ndim != 2:
|
542 | 546 | raise ValueError(f"Must pass 2-d input. shape={values.shape}")
|
543 |
| - |
544 | 547 | return values
|
545 | 548 |
|
546 | 549 |
|
|
0 commit comments