diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 2c8f98732c92f..897019c5d4065 100755 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -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'): diff --git a/pandas/tests/io/parser/common.py b/pandas/tests/io/parser/common.py index 2b7ff1f5a9879..482acdca24f81 100644 --- a/pandas/tests/io/parser/common.py +++ b/pandas/tests/io/parser/common.py @@ -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) @@ -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): + 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) diff --git a/pandas/tests/io/test_excel.py b/pandas/tests/io/test_excel.py index 05423474f330a..b5d955c5309ea 100644 --- a/pandas/tests/io/test_excel.py +++ b/pandas/tests/io/test_excel.py @@ -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): # 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)