Skip to content

Commit dfaaa39

Browse files
authored
BUG: Fix read_csv raising TypeError when iterator and nrows are specified without chunksize (#59080)
BUG: Fix read_csv raising TypeError when iterator and nrows are specified without a chunksize
1 parent fe785cc commit dfaaa39

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

Diff for: doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ I/O
554554
- Bug in :meth:`DataFrame.to_string` that raised ``StopIteration`` with nested DataFrames. (:issue:`16098`)
555555
- Bug in :meth:`HDFStore.get` was failing to save data of dtype datetime64[s] correctly (:issue:`59004`)
556556
- Bug in :meth:`read_csv` raising ``TypeError`` when ``index_col`` is specified and ``na_values`` is a dict containing the key ``None``. (:issue:`57547`)
557+
- Bug in :meth:`read_csv` raising ``TypeError`` when ``nrows`` and ``iterator`` are specified without specifying a ``chunksize``. (:issue:`59079`)
557558
- Bug in :meth:`read_stata` raising ``KeyError`` when input file is stored in big-endian format and contains strL data. (:issue:`58638`)
558559

559560
Period

Diff for: pandas/io/parsers/readers.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1534,7 +1534,10 @@ def get_chunk(self, size: int | None = None) -> DataFrame:
15341534
if self.nrows is not None:
15351535
if self._currow >= self.nrows:
15361536
raise StopIteration
1537-
size = min(size, self.nrows - self._currow)
1537+
if size is None:
1538+
size = self.nrows - self._currow
1539+
else:
1540+
size = min(size, self.nrows - self._currow)
15381541
return self.read(nrows=size)
15391542

15401543
def __enter__(self) -> Self:

Diff for: pandas/tests/io/parser/common/test_iterator.py

+25
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ def test_iterator_stop_on_chunksize(all_parsers):
9898
tm.assert_frame_equal(concat(result), expected)
9999

100100

101+
def test_nrows_iterator_without_chunksize(all_parsers):
102+
# GH 59079
103+
parser = all_parsers
104+
data = """A,B,C
105+
foo,1,2,3
106+
bar,4,5,6
107+
baz,7,8,9
108+
"""
109+
if parser.engine == "pyarrow":
110+
msg = "The 'iterator' option is not supported with the 'pyarrow' engine"
111+
with pytest.raises(ValueError, match=msg):
112+
parser.read_csv(StringIO(data), iterator=True, nrows=2)
113+
return
114+
115+
with parser.read_csv(StringIO(data), iterator=True, nrows=2) as reader:
116+
result = reader.get_chunk()
117+
118+
expected = DataFrame(
119+
[[1, 2, 3], [4, 5, 6]],
120+
index=["foo", "bar"],
121+
columns=["A", "B", "C"],
122+
)
123+
tm.assert_frame_equal(result, expected)
124+
125+
101126
@pytest.mark.parametrize(
102127
"kwargs", [{"iterator": True, "chunksize": 1}, {"iterator": True}, {"chunksize": 1}]
103128
)

0 commit comments

Comments
 (0)