Skip to content

BUG: Raise ValueError if names and prefix are both defined #41446

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

Merged
merged 4 commits into from
May 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,7 @@ I/O
- Bug in :func:`read_excel` raising ``AttributeError`` with ``MultiIndex`` header followed by two empty rows and no index, and bug affecting :func:`read_excel`, :func:`read_csv`, :func:`read_table`, :func:`read_fwf`, and :func:`read_clipboard` where one blank row after a ``MultiIndex`` header with no index would be dropped (:issue:`40442`)
- Bug in :meth:`DataFrame.to_string` misplacing the truncation column when ``index=False`` (:issue:`40907`)
- Bug in :func:`read_orc` always raising ``AttributeError`` (:issue:`40918`)
- Bug in :func:`read_csv` and :func:`read_table` silently ignoring ``prefix`` if ``names`` and ``prefix`` are defined, now raising ``ValueError`` (:issue:`39123`)
- Bug in :func:`read_csv` and :func:`read_excel` not respecting dtype for duplicated column name when ``mangle_dupe_cols`` is set to ``True`` (:issue:`35211`)
- Bug in :func:`read_csv` and :func:`read_table` misinterpreting arguments when ``sys.setprofile`` had been previously called (:issue:`41069`)
- Bug in the conversion from pyarrow to pandas (e.g. for reading Parquet) with nullable dtypes and a pyarrow array whose data buffer size is not a multiple of dtype size (:issue:`40896`)
Expand Down
41 changes: 35 additions & 6 deletions pandas/io/parsers/readers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import pandas._libs.lib as lib
from pandas._libs.parsers import STR_NA_VALUES
from pandas._typing import (
ArrayLike,
DtypeArg,
FilePathOrBuffer,
StorageOptions,
Expand Down Expand Up @@ -485,11 +486,11 @@ def read_csv(
delimiter=None,
# Column and Index Locations and Names
header="infer",
names=None,
names=lib.no_default,
index_col=None,
usecols=None,
squeeze=False,
prefix=None,
prefix=lib.no_default,
mangle_dupe_cols=True,
# General Parsing Configuration
dtype: Optional[DtypeArg] = None,
Expand Down Expand Up @@ -546,7 +547,14 @@ def read_csv(
del kwds["sep"]

kwds_defaults = _refine_defaults_read(
dialect, delimiter, delim_whitespace, engine, sep, defaults={"delimiter": ","}
dialect,
delimiter,
delim_whitespace,
engine,
sep,
names,
prefix,
defaults={"delimiter": ","},
)
kwds.update(kwds_defaults)

Expand All @@ -567,11 +575,11 @@ def read_table(
delimiter=None,
# Column and Index Locations and Names
header="infer",
names=None,
names=lib.no_default,
index_col=None,
usecols=None,
squeeze=False,
prefix=None,
prefix=lib.no_default,
mangle_dupe_cols=True,
# General Parsing Configuration
dtype: Optional[DtypeArg] = None,
Expand Down Expand Up @@ -627,7 +635,14 @@ def read_table(
del kwds["sep"]

kwds_defaults = _refine_defaults_read(
dialect, delimiter, delim_whitespace, engine, sep, defaults={"delimiter": "\t"}
dialect,
delimiter,
delim_whitespace,
engine,
sep,
names,
prefix,
defaults={"delimiter": "\t"},
)
kwds.update(kwds_defaults)

Expand Down Expand Up @@ -1174,6 +1189,8 @@ def _refine_defaults_read(
delim_whitespace: bool,
engine: str,
sep: Union[str, object],
names: Union[Optional[ArrayLike], object],
prefix: Union[Optional[str], object],
defaults: Dict[str, Any],
):
"""Validate/refine default values of input parameters of read_csv, read_table.
Expand All @@ -1199,6 +1216,12 @@ def _refine_defaults_read(
sep : str or object
A delimiter provided by the user (str) or a sentinel value, i.e.
pandas._libs.lib.no_default.
names : array-like, optional
List of column names to use. If the file contains a header row,
then you should explicitly pass ``header=0`` to override the column names.
Duplicates in this list are not allowed.
prefix : str, optional
Prefix to add to column numbers when no header, e.g. 'X' for X0, X1, ...
defaults: dict
Default values of input parameters.

Expand Down Expand Up @@ -1232,6 +1255,12 @@ def _refine_defaults_read(
sep is lib.no_default or sep == delim_default
)

if names is not lib.no_default and prefix is not lib.no_default:
raise ValueError("Specified named and prefix; you can only specify one.")

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

# Alias sep -> delimiter.
if delimiter is None:
delimiter = sep
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/io/parser/common/test_common_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,18 @@ def test_read_table_delim_whitespace_non_default_sep(all_parsers, delimiter):
parser.read_table(f, delim_whitespace=True, delimiter=delimiter)


@pytest.mark.parametrize("func", ["read_csv", "read_table"])
@pytest.mark.parametrize("prefix", [None, "x"])
@pytest.mark.parametrize("names", [None, ["a"]])
def test_names_and_prefix_not_lib_no_default(all_parsers, names, prefix, func):
# GH#39123
f = StringIO("a,b\n1,2")
parser = all_parsers
msg = "Specified named and prefix; you can only specify one."
with pytest.raises(ValueError, match=msg):
getattr(parser, func)(f, names=names, prefix=prefix)


def test_dict_keys_as_names(all_parsers):
# GH: 36928
data = "1,2"
Expand Down