Skip to content

Commit 2786cda

Browse files
ENH: GH34946 Check type of names argument to read_csv, read_table… (#34956)
1 parent 526f404 commit 2786cda

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,7 @@ Other
11211121
- :class:`IntegerArray` now implements the ``sum`` operation (:issue:`33172`)
11221122
- Bug in :class:`Tick` comparisons raising ``TypeError`` when comparing against timedelta-like objects (:issue:`34088`)
11231123
- Bug in :class:`Tick` multiplication raising ``TypeError`` when multiplying by a float (:issue:`34486`)
1124+
- 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`)
11241125

11251126
.. ---------------------------------------------------------------------------
11261127

pandas/io/parsers.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,8 @@ def _validate_integer(name, val, min_val=0):
397397

398398
def _validate_names(names):
399399
"""
400-
Raise ValueError if the `names` parameter contains duplicates.
400+
Raise ValueError if the `names` parameter contains duplicates or has an
401+
invalid data type.
401402
402403
Parameters
403404
----------
@@ -407,11 +408,13 @@ def _validate_names(names):
407408
Raises
408409
------
409410
ValueError
410-
If names are not unique.
411+
If names are not unique or are not ordered (e.g. set).
411412
"""
412413
if names is not None:
413414
if len(names) != len(set(names)):
414415
raise ValueError("Duplicate names are not allowed.")
416+
if not is_list_like(names, allow_sets=False):
417+
raise ValueError("Names should be an ordered collection.")
415418

416419

417420
def _read(filepath_or_buffer: FilePathOrBuffer, kwds):

pandas/tests/io/parser/test_common.py

+10
Original file line numberDiff line numberDiff line change
@@ -2135,3 +2135,13 @@ def test_no_header_two_extra_columns(all_parsers):
21352135
parser = all_parsers
21362136
df = parser.read_csv(stream, header=None, names=column_names, index_col=False)
21372137
tm.assert_frame_equal(df, ref)
2138+
2139+
2140+
def test_read_csv_names_not_accepting_sets(all_parsers):
2141+
# GH 34946
2142+
data = """\
2143+
1,2,3
2144+
4,5,6\n"""
2145+
parser = all_parsers
2146+
with pytest.raises(ValueError, match="Names should be an ordered collection."):
2147+
parser.read_csv(StringIO(data), names=set("QAZ"))

0 commit comments

Comments
 (0)