MCPcopy Create free account
hub / github.com/numpy/numpy / _ensure_ndmin_ndarray

Function _ensure_ndmin_ndarray

numpy/lib/npyio.py:760–780  ·  view source on GitHub ↗

This is a helper function of loadtxt and genfromtxt to ensure proper minimum dimension as requested ndim : int. Supported values 1, 2, 3 ^^ whenever this changes, keep in sync with _ensure_ndmin_ndarray_check_param

(a, *, ndmin: int)

Source from the content-addressed store, hash-verified

758 raise ValueError(f"Illegal value of ndmin keyword: {ndmin}")
759
760def _ensure_ndmin_ndarray(a, *, ndmin: int):
761 """This is a helper function of loadtxt and genfromtxt to ensure
762 proper minimum dimension as requested
763
764 ndim : int. Supported values 1, 2, 3
765 ^^ whenever this changes, keep in sync with
766 _ensure_ndmin_ndarray_check_param
767 """
768 # Verify that the array has at least dimensions `ndmin`.
769 # Tweak the size and shape of the arrays - remove extraneous dimensions
770 if a.ndim > ndmin:
771 a = np.squeeze(a)
772 # and ensure we have the minimum number of dimensions asked for
773 # - has to be in this order for the odd case ndmin=1, a.squeeze().ndim=0
774 if a.ndim < ndmin:
775 if ndmin == 1:
776 a = np.atleast_1d(a)
777 elif ndmin == 2:
778 a = np.atleast_2d(a).T
779
780 return a
781
782
783# amount of lines loadtxt reads in one chunk, can be overridden for testing

Callers 2

_readFunction · 0.85
genfromtxtFunction · 0.85

Calls 1

squeezeMethod · 0.45

Tested by

no test coverage detected