diff options
author | Warren Weckesser <warren.weckesser@gmail.com> | 2014-10-31 22:18:24 -0400 |
---|---|---|
committer | Charles Harris <charlesr.harris@gmail.com> | 2015-01-23 17:28:48 -0700 |
commit | 7a9e3d9535ac4cdd7b815d23416f36c3f979fc99 (patch) | |
tree | d35b33a6fc7c281a90c9c8884f38e067e4d7da0e /numpy/lib/npyio.py | |
parent | 0091499ec28cd9ceb30cd94c0e40191570b6fec6 (diff) | |
download | numpy-7a9e3d9535ac4cdd7b815d23416f36c3f979fc99.tar.gz |
ENH: genfromtxt: Change 'nrows' to 'max_rows'.
Diffstat (limited to 'numpy/lib/npyio.py')
-rw-r--r-- | numpy/lib/npyio.py | 35 |
1 files changed, 17 insertions, 18 deletions
diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index c8cebaed8..db1d87435 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -1205,7 +1205,7 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, excludelist=None, deletechars=None, replace_space='_', autostrip=False, case_sensitive=True, defaultfmt="f%i", unpack=None, usemask=False, loose=True, invalid_raise=True, - nrows=None): + max_rows=None): """ Load data from a text file, with missing values handled as specified. @@ -1286,9 +1286,10 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, If True, an exception is raised if an inconsistency is detected in the number of columns. If False, a warning is emitted and the offending lines are skipped. - nrows : int, optional - The number of rows to read. Must not be used with skip_footer at the - same time. + max_rows : int, optional + The maximum number of rows to read. Must not be used with skip_footer + at the same time. If given, the value must be at least 1. Default is + to read the entire file. .. versionadded:: 1.10.0 @@ -1359,11 +1360,13 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, dtype=[('intvar', '<i8'), ('fltvar', '<f8'), ('strvar', '|S5')]) """ - # Check keywords conflict - if skip_footer and (nrows is not None): - raise ValueError( - "keywords 'skip_footer' and 'nrows' can not be specified " - "at the same time") + if max_rows is not None: + if skip_footer: + raise ValueError( + "The keywords 'skip_footer' and 'max_rows' can not be " + "specified at the same time.") + if max_rows < 1: + raise ValueError("'max_rows' must be at least 1.") # Py3 data conversions to bytes, for convenience if comments is not None: @@ -1654,15 +1657,13 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, # Parse each line for (i, line) in enumerate(itertools.chain([first_line, ], fhd)): - if (nrows is not None) and (len(rows) >= nrows): - break values = split_line(line) nbvalues = len(values) # Skip an empty line if nbvalues == 0: continue - # Select only the columns we need if usecols: + # Select only the columns we need try: values = [values[_] for _ in usecols] except IndexError: @@ -1675,16 +1676,14 @@ def genfromtxt(fname, dtype=float, comments='#', delimiter=None, append_to_rows(tuple(values)) if usemask: append_to_masks(tuple([v.strip() in m - for (v, m) in zip(values, missing_values)])) + for (v, m) in zip(values, + missing_values)])) + if len(rows) == max_rows: + break if own_fhd: fhd.close() - if (nrows is not None) and (len(rows) != nrows): - raise AssertionError( - "%d rows required but got %d valid rows instead" - %(nrows, len(rows))) - # Upgrade the converters (if needed) if dtype is None: for (i, converter) in enumerate(converters): |