Skip to content

Commit

Permalink
Raise error in read_csv when arguments header and prefix both are not…
Browse files Browse the repository at this point in the history
… None (#31383)

Closes #27394
  • Loading branch information
rushabh-v authored Feb 3, 2020
1 parent a9d2450 commit a2721fd
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ MultiIndex
I/O
^^^
- Bug in :meth:`read_json` where integer overflow was occuring when json contains big number strings. (:issue:`30320`)
- `read_csv` will now raise a ``ValueError`` when the arguments `header` and `prefix` both are not `None`. (:issue:`27394`)
- Bug in :meth:`DataFrame.to_json` was raising ``NotFoundError`` when ``path_or_buf`` was an S3 URI (:issue:`28375`)
-

Expand Down
26 changes: 15 additions & 11 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1399,17 +1399,21 @@ def __init__(self, kwds):
"index_col must only contain row numbers "
"when specifying a multi-index header"
)

# GH 16338
elif self.header is not None and not is_integer(self.header):
raise ValueError("header must be integer or list of integers")

# GH 27779
elif self.header is not None and self.header < 0:
raise ValueError(
"Passing negative integer to header is invalid. "
"For no header, use header=None instead"
)
elif self.header is not None:
# GH 27394
if self.prefix is not None:
raise ValueError(
"Argument prefix must be None if argument header is not None"
)
# GH 16338
elif not is_integer(self.header):
raise ValueError("header must be integer or list of integers")
# GH 27779
elif self.header < 0:
raise ValueError(
"Passing negative integer to header is invalid. "
"For no header, use header=None instead"
)

self._name_processed = False

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2040,6 +2040,17 @@ def test_read_csv_memory_growth_chunksize(all_parsers):
pass


def test_read_csv_raises_on_header_prefix(all_parsers):
# gh-27394
parser = all_parsers
msg = "Argument prefix must be None if argument header is not None"

s = StringIO("0,1\n2,3")

with pytest.raises(ValueError, match=msg):
parser.read_csv(s, header=0, prefix="_X")


def test_read_table_equivalency_to_read_csv(all_parsers):
# see gh-21948
# As of 0.25.0, read_table is undeprecated
Expand Down

0 comments on commit a2721fd

Please sign in to comment.