From c22f6d4221581e0bb6f74599e95c4b8f4f5239f2 Mon Sep 17 00:00:00 2001 From: Chandrakant Gopalan Date: Mon, 11 Jun 2018 18:19:38 -0400 Subject: [PATCH 1/2] BUG: Nrows cannot be zero for read_csv. Fixes #21141 --- pandas/io/parsers.py | 2 +- pandas/tests/io/parser/common.py | 5 ++++- pandas/tests/io/test_excel.py | 9 ++++++++- 3 files changed, 13 insertions(+), 3 deletions(-) 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..218dc58878050 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 16645 + 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) From 947168bbcbfc6300f1e6424107ed318219c85103 Mon Sep 17 00:00:00 2001 From: Chandrakant Gopalan Date: Mon, 11 Jun 2018 18:23:29 -0400 Subject: [PATCH 2/2] Corrected issue number --- pandas/tests/io/test_excel.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/io/test_excel.py b/pandas/tests/io/test_excel.py index 218dc58878050..b5d955c5309ea 100644 --- a/pandas/tests/io/test_excel.py +++ b/pandas/tests/io/test_excel.py @@ -1001,7 +1001,7 @@ def test_read_excel_nrows_non_integer_parameter(self, ext): nrows='5') def test_read_excel_nrows_zero_parameter(self, ext): - # GH 16645 + # 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),