summaryrefslogtreecommitdiff
path: root/numpy/lib/npyio.py
diff options
context:
space:
mode:
authorPranab Das <31024886+pranabdas@users.noreply.github.com>2022-07-02 12:04:14 +0800
committerGitHub <noreply@github.com>2022-07-02 00:04:14 -0400
commit3d9cacc100e1b9382a543f000d0df44180cff122 (patch)
tree0af40c6b43d8bf6f2f5baa0a5196f3afd5cc998a /numpy/lib/npyio.py
parent860a12ede7a4521debc16ef8c3a8b3d182b65c7a (diff)
downloadnumpy-3d9cacc100e1b9382a543f000d0df44180cff122.tar.gz
DOC: Clarify loadtxt input cols requirement (#21861)
Also add an example to illustrate how usecols can be used to read a file with varying number of fields.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r--numpy/lib/npyio.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py
index 52bcf25f8..471f85976 100644
--- a/numpy/lib/npyio.py
+++ b/numpy/lib/npyio.py
@@ -1067,8 +1067,6 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
r"""
Load data from a text file.
- Each row in the text file must have the same number of values.
-
Parameters
----------
fname : file, str, pathlib.Path, list of str, generator
@@ -1171,6 +1169,11 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
`genfromtxt` function provides more sophisticated handling of, e.g.,
lines with missing values.
+ Each row in the input text file must have the same number of values to be
+ able to read all values. If all rows do not have same number of values, a
+ subset of up to n columns (where n is the least number of values present
+ in all rows) can be read by specifying the columns via `usecols`.
+
.. versionadded:: 1.10.0
The strings produced by the Python float.hex method can be used as
@@ -1279,6 +1282,15 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None,
>>> np.loadtxt(s, dtype="U", delimiter=",", quotechar='"')
array('Hello, my name is "Monty"!', dtype='<U26')
+ Read subset of columns when all rows do not contain equal number of values:
+
+ >>> d = StringIO("1 2\n2 4\n3 9 12\n4 16 20")
+ >>> np.loadtxt(d, usecols=(0, 1))
+ array([[ 1., 2.],
+ [ 2., 4.],
+ [ 3., 9.],
+ [ 4., 16.]])
+
"""
if like is not None: