From b233716a031cb523f9bc65dda2c22f69f6f0736a Mon Sep 17 00:00:00 2001 From: Derek Homeier Date: Fri, 6 May 2011 12:07:32 +0200 Subject: use np.atleast_Nd() to boost dimensions to ndmin --- numpy/lib/npyio.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'numpy/lib/npyio.py') diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 4d5e96b93..13f659d70 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -627,7 +627,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, data-type, arrays are returned for each field. Default is False. ndmin : int, optional The returned array will have at least `ndmin` dimensions. - Otherwise single-dimensional axes will be squeezed. + Otherwise mono-dimensional axes will be squeezed. Legal values: 0 (default), 1 or 2. .. versionadded:: 1.6.0 @@ -803,6 +803,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, fh.close() X = np.array(X, dtype) + # Multicolumn data are returned with shape (1, N, M), i.e. + # (1, 1, M) for a single row - remove the singleton dimension there if X.ndim == 3 and X.shape[:2] == (1, 1): X.shape = (1, -1) @@ -810,15 +812,16 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, # Check correctness of the values of `ndmin` if not ndmin in [0, 1, 2]: raise ValueError('Illegal value of ndmin keyword: %s' % ndmin) - # Tweak the size and shape of the arrays + # Tweak the size and shape of the arrays - remove extraneous dimensions if X.ndim > ndmin: X = np.squeeze(X) - # Has to be in this order for the odd case ndmin=1, X.squeeze().ndim=0 + # and ensure we have the minimum number of dimensions asked for + # - has to be in this order for the odd case ndmin=1, X.squeeze().ndim=0 if X.ndim < ndmin: if ndmin == 1: - X.shape = (X.size, ) + X = np.atleast_1d(X) elif ndmin == 2: - X.shape = (X.size, 1) + X = np.atleast_2d(X).T if unpack: if len(dtype_types) > 1: -- cgit v1.2.1