Skip to content
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

Raise error in read_csv when arguments header and prefix both are not None #31383

Merged
merged 17 commits into from
Feb 3, 2020
Merged
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,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 arguments `header` and `prefix` both are not None. (:issue:`27394`)
rushabh-v marked this conversation as resolved.
Show resolved Hide resolved
-

Plotting
Expand Down
25 changes: 15 additions & 10 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1400,16 +1400,21 @@ def __init__(self, kwds):
"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:
# GH 16338
if not is_integer(self.header):
raise ValueError("header must be integer or list of integers")
# GH 27394
elif self.prefix:
raise ValueError(
"Argument prefix must be None if argument header is not None"
)
gfyoung marked this conversation as resolved.
Show resolved Hide resolved
# 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: 10 additions & 1 deletion pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from pandas._libs.tslib import Timestamp
from pandas.errors import DtypeWarning, EmptyDataError, ParserError

from pandas import DataFrame, Index, MultiIndex, Series, compat, concat
from pandas import DataFrame, Index, MultiIndex, Series, compat, concat, read_csv
import pandas._testing as tm

from pandas.io.parsers import CParserWrapper, TextFileReader, TextParser
Expand Down Expand Up @@ -2040,6 +2040,15 @@ 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")
gfyoung marked this conversation as resolved.
Show resolved Hide resolved
s = StringIO("0,1\n2,3")
rushabh-v marked this conversation as resolved.
Show resolved Hide resolved
with pytest.raises(ValueError, match=msg):
parser.read_csv(s, header=0, prefix = "_X")
gfyoung marked this conversation as resolved.
Show resolved Hide resolved


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