summaryrefslogtreecommitdiff
path: root/numpy/lib/npyio.py
diff options
context:
space:
mode:
authorDerek Homeier <derek@astro.physik.uni-goettingen.de>2011-05-06 12:07:32 +0200
committerRalf Gommers <ralf.gommers@googlemail.com>2011-05-07 22:12:29 +0200
commitb233716a031cb523f9bc65dda2c22f69f6f0736a (patch)
tree13b7a7fa4d38e75d4c36c5b51c465cdddba7f98a /numpy/lib/npyio.py
parent607d2b3bbe984892fbf345788a54eafebdf967ed (diff)
downloadnumpy-b233716a031cb523f9bc65dda2c22f69f6f0736a.tar.gz
use np.atleast_Nd() to boost dimensions to ndmin
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r--numpy/lib/npyio.py13
1 files changed, 8 insertions, 5 deletions
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: