Skip to content

Commit

Permalink
fixed bug at line 1058 in file numpy/lib&npyio_impl.py; in function _…
Browse files Browse the repository at this point in the history
…read(), called by loadtxt() method, when files are read in chunks to reduce memory overhead, max_rows lines were always loaded every time, also in the case max_rows>_loadtxt_chunksize, in which case it loaded chunks with the wrong size. A test has been added in numpy/lib/tests/test_loadtxt.py, to check for the array size loaded for different max_rows, less and greater than _loadtxt_chunksize.
  • Loading branch information
l09rin committed Jun 19, 2024
1 parent ccff7fb commit f323664
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion numpy/lib/_npyio_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ def _read(fname, *, delimiter=',', comment='#', quote='"',
next_arr = _load_from_filelike(
data, delimiter=delimiter, comment=comment, quote=quote,
imaginary_unit=imaginary_unit,
usecols=usecols, skiplines=skiplines, max_rows=max_rows,
usecols=usecols, skiplines=skiplines, max_rows=chunk_size,
converters=converters, dtype=dtype,
encoding=encoding, filelike=filelike,
byte_converters=byte_converters,
Expand Down
10 changes: 10 additions & 0 deletions numpy/lib/tests/test_loadtxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1043,3 +1043,13 @@ def test_field_growing_cases():
for i in range(1, 1024):
res = np.loadtxt(["," * i], delimiter=",", dtype=bytes)
assert len(res) == i+1

@pytest.mark.parametrize("nmax", (10000, 50000, 80000, 100000, 120000)) # less, equal, greater, twice and more than twice than _loadtxt_chunksize
def test_maxrows_exceeding_chunksize(nmax):
file_length = 200000
data = ""
for i in range(file_length) :
data += "a 0.5 1\n"
txt = StringIO(data)
res = np.loadtxt(txt, dtype=str, delimiter=" ", max_rows=nmax)
assert len(res) == nmax

0 comments on commit f323664

Please sign in to comment.