Skip to content

ENH: GH34946 Check type of names argument to read_csv, read_table #34956

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 5 commits into from
Jun 24, 2020
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.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ Other
- :class:`IntegerArray` now implements the ``sum`` operation (:issue:`33172`)
- Bug in :class:`Tick` comparisons raising ``TypeError`` when comparing against timedelta-like objects (:issue:`34088`)
- Bug in :class:`Tick` multiplication raising ``TypeError`` when multiplying by a float (:issue:`34486`)
- Passing a `set` as `names` argument to :func:`pandas.read_csv`, :func:`pandas.read_table`, or :func:`pandas.read_fwf` will raise ``ValueError: Names should be an ordered collection.`` (:issue:`34946`)

.. ---------------------------------------------------------------------------

Expand Down
7 changes: 5 additions & 2 deletions pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ def _validate_integer(name, val, min_val=0):

def _validate_names(names):
"""
Raise ValueError if the `names` parameter contains duplicates.
Raise ValueError if the `names` parameter contains duplicates or has an
invalid data type.

Parameters
----------
Expand All @@ -407,11 +408,13 @@ def _validate_names(names):
Raises
------
ValueError
If names are not unique.
If names are not unique or are not ordered (e.g. set).
"""
if names is not None:
if len(names) != len(set(names)):
raise ValueError("Duplicate names are not allowed.")
if not is_list_like(names, allow_sets=False):
raise ValueError("Names should be an ordered collection.")


def _read(filepath_or_buffer: FilePathOrBuffer, kwds):
Expand Down
10 changes: 10 additions & 0 deletions pandas/tests/io/parser/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2135,3 +2135,13 @@ def test_no_header_two_extra_columns(all_parsers):
parser = all_parsers
df = parser.read_csv(stream, header=None, names=column_names, index_col=False)
tm.assert_frame_equal(df, ref)


def test_read_csv_names_not_accepting_sets(all_parsers):
# GH 34946
data = """\
1,2,3
4,5,6\n"""
parser = all_parsers
with pytest.raises(ValueError, match="Names should be an ordered collection."):
parser.read_csv(StringIO(data), names=set("QAZ"))