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
4 changes: 4 additions & 0 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1908,6 +1908,10 @@ def __init__(self, src, **kwds):
]
else:
self.names = list(range(self._reader.table_width))
elif self.prefix:
gfyoung marked this conversation as resolved.
Show resolved Hide resolved
raise ValueError(
"Argument prefix must be None if argument header is not None"
)

# gh-9755
#
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/frame/test_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,13 @@ def test_to_csv_headers(self):
recons.reset_index(inplace=True)
tm.assert_frame_equal(to_df, recons)

def test_to_csv_raises_on_header_prefix(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this testing to_csv or read_csv? Looks like it's in the to_csv file, but is testing read_csv?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's testing read_csv that's a mistake in naming it.

But I actually tried putting this test in pandas/tests/io/parser/test_common.py. But the problem there is that all the tests use io.parsers.read_csv there. And io.parsers.read_csv doesn't seem to be going into CParserWrapper. So I tried putting it here. But now I am putting it back in test_common.py and will import the pandas.read_csv there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you take a look, please?

# gh-27394
msg = "Argument prefix must be None if argument header is not None"
s = StringIO("0,1\n2,3")
with pytest.raises(ValueError, match=msg):
read_csv(s, header=0, prefix="_X")

def test_to_csv_multiindex(self, float_frame, datetime_frame):

frame = float_frame
Expand Down