[NumPy-Tickets] [NumPy] #1562: potentially unexpected results with loadtxt()
NumPy Trac
numpy-tickets@scipy....
Sat Mar 26 09:27:06 CDT 2011
#1562: potentially unexpected results with loadtxt()
------------------------+---------------------------------------------------
Reporter: weathergod | Owner: somebody
Type: defect | Status: needs_review
Priority: normal | Milestone: 1.5.1
Component: Other | Version:
Keywords: |
------------------------+---------------------------------------------------
Changes (by dynetrekk):
* cc: paul.anton.letnes@… (added)
* status: new => needs_review
Comment:
A suggested implementation can be found here. If requested, the shape is
simply modified to contain enough ones to ensure the minimum number of
dimensions.
{{{
diff -r 2763b87dd7e8 -r 438a92342252 numpy/lib/npyio.py
--- a/numpy/lib/npyio.py Fri Mar 25 22:37:19 2011 -0600
+++ b/numpy/lib/npyio.py Sat Mar 26 12:23:25 2011 +0100
@@ -579,7 +579,8 @@
def loadtxt(fname, dtype=float, comments='#', delimiter=None,
- converters=None, skiprows=0, usecols=None, unpack=False):
+ converters=None, skiprows=0, usecols=None, unpack=False,
+ ndmin=None):
"""
Load data from a text file.
@@ -616,6 +617,9 @@
unpack : bool, optional
If True, the returned array is transposed, so that arguments may
be
unpacked using ``x, y, z = loadtxt(...)``. The default is False.
+ ndmin : int, optional
+ If not None, the returned array must have at least `ndmin`
dimensions.
+ Legal values: 1 or 2.
Returns
-------
@@ -790,8 +794,20 @@
fh.close()
X = np.array(X, dtype)
+ X = np.squeeze(X)
- X = np.squeeze(X)
+ # Verify that the array has at least dimensions `ndmin`.
+ if not (ndmin is None):
+ # Check correctness of the values of `ndmin`
+ if not (ndmin in (1, 2)):
+ msg = 'Illegal value of ndmin keyword: {0}'
+ raise ValueError(msg.format(ndmin))
+ # Tweak the size and shape of the arrays
+ if not (len(X.shape) >= ndmin):
+ if ndmin == 1:
+ X.shape = (X.size, )
+ elif ndmin == 2:
+ X.shape = (X.size, 1)
if unpack:
return X.T
else:
}}}
--
Ticket URL: <http://projects.scipy.org/numpy/ticket/1562#comment:1>
NumPy <http://projects.scipy.org/numpy>
My example project
More information about the NumPy-Tickets
mailing list