Skip to content

Commit 1269bc6

Browse files
lithomas1jreback
andauthored
REGR: ValueError raised when both prefix and names are set to None (#42690)
* REGR: ValueError raised when both prefix and names are set to None * Update readers.py * whitespace * Update v1.3.1.rst * Update v1.3.2.rst * Update readers.py * Update readers.py Co-authored-by: Jeff Reback <[email protected]>
1 parent 5f15f5c commit 1269bc6

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

doc/source/whatsnew/v1.3.2.rst

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Fixed regressions
1919
- Regression in :meth:`DataFrame.from_records` with empty records (:issue:`42456`)
2020
- Fixed regression in :meth:`DataFrame.shift` where TypeError occurred when shifting DataFrame created by concatenation of slices and fills with values (:issue:`42719`)
2121
- Regression in :meth:`DataFrame.agg` when the ``func`` argument returned lists and ``axis=1`` (:issue:`42727`)
22+
- Fixed regression where :meth:`pandas.read_csv` raised a ``ValueError`` when parameters ``names`` and ``prefix`` were both set to None (:issue:`42387`)
2223
- Fixed regression in comparisons between :class:`Timestamp` object and ``datetime64`` objects outside the implementation bounds for nanosecond ``datetime64`` (:issue:`42794`)
2324
-
2425

pandas/io/parsers/readers.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1302,7 +1302,12 @@ def _refine_defaults_read(
13021302
if delimiter and (sep is not lib.no_default):
13031303
raise ValueError("Specified a sep and a delimiter; you can only specify one.")
13041304

1305-
if names is not lib.no_default and prefix is not lib.no_default:
1305+
if (
1306+
names is not None
1307+
and names is not lib.no_default
1308+
and prefix is not None
1309+
and prefix is not lib.no_default
1310+
):
13061311
raise ValueError("Specified named and prefix; you can only specify one.")
13071312

13081313
kwds["names"] = None if names is lib.no_default else names

pandas/tests/io/parser/common/test_common_basic.py

+13-4
Original file line numberDiff line numberDiff line change
@@ -764,15 +764,24 @@ def test_read_table_delim_whitespace_non_default_sep(all_parsers, delimiter):
764764

765765

766766
@pytest.mark.parametrize("func", ["read_csv", "read_table"])
767-
@pytest.mark.parametrize("prefix", [None, "x"])
768-
@pytest.mark.parametrize("names", [None, ["a"]])
769-
def test_names_and_prefix_not_lib_no_default(all_parsers, names, prefix, func):
767+
def test_names_and_prefix_not_None_raises(all_parsers, func):
770768
# GH#39123
771769
f = StringIO("a,b\n1,2")
772770
parser = all_parsers
773771
msg = "Specified named and prefix; you can only specify one."
774772
with pytest.raises(ValueError, match=msg):
775-
getattr(parser, func)(f, names=names, prefix=prefix)
773+
getattr(parser, func)(f, names=["a", "b"], prefix="x")
774+
775+
776+
@pytest.mark.parametrize("func", ["read_csv", "read_table"])
777+
@pytest.mark.parametrize("prefix, names", [(None, ["x0", "x1"]), ("x", None)])
778+
def test_names_and_prefix_explicit_None(all_parsers, names, prefix, func):
779+
# GH42387
780+
f = StringIO("a,b\n1,2")
781+
expected = DataFrame({"x0": ["a", "1"], "x1": ["b", "2"]})
782+
parser = all_parsers
783+
result = getattr(parser, func)(f, names=names, sep=",", prefix=prefix, header=None)
784+
tm.assert_frame_equal(result, expected)
776785

777786

778787
def test_dict_keys_as_names(all_parsers):

0 commit comments

Comments
 (0)