Skip to content

BUG: Nrows cannot be zero for read_csv. Fixes #21141 #21431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ def _failover_to_python(self):
raise com.AbstractMethodError(self)

def read(self, nrows=None):
nrows = _validate_integer('nrows', nrows)
nrows = _validate_integer('nrows', nrows, min_val=1)

if nrows is not None:
if self.options.get('skipfooter'):
Expand Down
5 changes: 4 additions & 1 deletion pandas/tests/io/parser/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ def test_read_nrows(self):
df = self.read_csv(StringIO(self.data1), nrows=3.0)
tm.assert_frame_equal(df, expected)

msg = r"'nrows' must be an integer >=0"
msg = r"'nrows' must be an integer >=1"

with tm.assert_raises_regex(ValueError, msg):
self.read_csv(StringIO(self.data1), nrows=1.2)
Expand All @@ -375,6 +375,9 @@ def test_read_nrows(self):
with tm.assert_raises_regex(ValueError, msg):
self.read_csv(StringIO(self.data1), nrows=-1)

with tm.assert_raises_regex(ValueError, msg):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you reference the issue number in a comment above this line.

self.read_csv(StringIO(self.data1), nrows=0)

def test_read_chunksize(self):
reader = self.read_csv(StringIO(self.data1), index_col=0, chunksize=2)
df = self.read_csv(StringIO(self.data1), index_col=0)
Expand Down
9 changes: 8 additions & 1 deletion pandas/tests/io/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -995,11 +995,18 @@ def test_read_excel_nrows_greater_than_nrows_in_file(self, ext):

def test_read_excel_nrows_non_integer_parameter(self, ext):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be better to parametrize this test for cases like [-1, 0, '1'] instead of having separate tests

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WillAyd sure, I will make these changes as soon as we decide if we are keeping this PR :)

# GH 16645
msg = "'nrows' must be an integer >=0"
msg = "'nrows' must be an integer >=1"
with tm.assert_raises_regex(ValueError, msg):
pd.read_excel(os.path.join(self.dirpath, 'test1' + ext),
nrows='5')

def test_read_excel_nrows_zero_parameter(self, ext):
# GH 21141
msg = "'nrows' must be an integer >=1"
with tm.assert_raises_regex(ValueError, msg):
pd.read_excel(os.path.join(self.dirpath, 'test1' + ext),
nrows=0)

def test_read_excel_squeeze(self, ext):
# GH 12157
f = os.path.join(self.dirpath, 'test_squeeze' + ext)
Expand Down