MCPcopy
hub / github.com/pandas-dev/pandas / _prep_ndarraylike

Function _prep_ndarraylike

pandas/core/internals/construction.py:504–539  ·  view source on GitHub ↗
(values, copy: bool = True)

Source from the content-addressed store, hash-verified

502
503
504def _prep_ndarraylike(values, copy: bool = True) -> np.ndarray:
505 # values is specifically _not_ ndarray, EA, Index, or Series
506 # We only get here with `not treat_as_nested(values)`
507
508 if len(values) == 0:
509 # TODO: check for length-zero range, in which case return int64 dtype?
510 # TODO: reuse anything in try_cast?
511 return np.empty((0, 0), dtype=object)
512 elif isinstance(values, range):
513 arr = range_to_ndarray(values)
514 return arr[..., np.newaxis]
515
516 def convert(v):
517 if not is_list_like(v) or isinstance(v, ABCDataFrame):
518 return v
519
520 v = extract_array(v, extract_numpy=True)
521 res = maybe_convert_platform(v)
522 # We don't do maybe_infer_objects here bc we will end up doing
523 # it column-by-column in ndarray_to_mgr
524 return res
525
526 # we could have a 1-dim or 2-dim list here
527 # this is equiv of np.asarray, but does object conversion
528 # and platform dtype preservation
529 # does not convert e.g. [1, "a", True] to ["1", "a", "True"] like
530 # np.asarray would
531 if is_list_like(values[0]):
532 values = np.array([convert(v) for v in values])
533 elif isinstance(values[0], np.ndarray) and values[0].ndim == 0:
534 # GH#21861 see test_constructor_list_of_lists
535 values = np.array([convert(v) for v in values])
536 else:
537 values = convert(values)
538
539 return _ensure_2d(values)
540
541
542def _ensure_2d(values: np.ndarray) -> np.ndarray:

Callers 1

ndarray_to_mgrFunction · 0.85

Calls 5

range_to_ndarrayFunction · 0.90
convertFunction · 0.85
_ensure_2dFunction · 0.85
emptyMethod · 0.45
arrayMethod · 0.45

Tested by

no test coverage detected