summaryrefslogtreecommitdiff
path: root/numpy/lib
diff options
context:
space:
mode:
Diffstat (limited to 'numpy/lib')
-rw-r--r--numpy/lib/npyio.py5
-rw-r--r--numpy/lib/tests/test_io.py10
2 files changed, 12 insertions, 3 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index f8c24b1a0..4d5e96b93 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -626,7 +626,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
unpacked using ``x, y, z = loadtxt(...)``. When used with a record
data-type, arrays are returned for each field. Default is False.
ndmin : int, optional
- The returned array must have at least `ndmin` dimensions.
+ The returned array will have at least `ndmin` dimensions.
+ Otherwise single-dimensional axes will be squeezed.
Legal values: 0 (default), 1 or 2.
.. versionadded:: 1.6.0
@@ -802,6 +803,8 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
fh.close()
X = np.array(X, dtype)
+ if X.ndim == 3 and X.shape[:2] == (1, 1):
+ X.shape = (1, -1)
# Verify that the array has at least dimensions `ndmin`.
# Check correctness of the values of `ndmin`
diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py
index aa27e7ecc..97633d525 100644
--- a/numpy/lib/tests/test_io.py
+++ b/numpy/lib/tests/test_io.py
@@ -486,11 +486,17 @@ class TestLoadTxt(TestCase):
d.write(asbytes('0,1,2'))
d.seek(0)
x = np.loadtxt(d, dtype=int, delimiter=',', ndmin=2)
- assert_(x.shape == (3, 1))
+ assert_(x.shape == (1, 3))
assert_raises(ValueError, np.loadtxt, d, ndmin=3)
assert_raises(ValueError, np.loadtxt, d, ndmin=1.5)
e = StringIO()
- assert_(np.loadtxt(e, ndmin=2).shape == (0, 1,))
+ e.write(asbytes('0\n1\n2'))
+ e.seek(0)
+ x = np.loadtxt(e, dtype=int, delimiter=',', ndmin=2)
+ assert_(x.shape == (3, 1))
+ f = StringIO()
+ assert_(np.loadtxt(f, ndmin=2).shape == (0, 1,))
+ assert_(np.loadtxt(f, ndmin=1).shape == (0,))
def test_generator_source(self):
def count():