Skip to content

Commit 4d6c510

Browse files
ENH: GH34946 Check type of names argument to read_csv, read_table, read_fwf to not be a set.
1 parent 3e8f14f commit 4d6c510

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,7 @@ Other
11191119
- :class:`IntegerArray` now implements the ``sum`` operation (:issue:`33172`)
11201120
- Bug in :class:`Tick` comparisons raising ``TypeError`` when comparing against timedelta-like objects (:issue:`34088`)
11211121
- Bug in :class:`Tick` multiplication raising ``TypeError`` when multiplying by a float (:issue:`34486`)
1122+
- 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 have consistent ordering. Consider a list instead.`` (:issue:`34946`)
11221123

11231124
.. ---------------------------------------------------------------------------
11241125

pandas/io/parsers.py

+7-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,15 @@ def _validate_names(names):
407408
Raises
408409
------
409410
ValueError
410-
If names are not unique.
411+
If names are not unique or have incosistent ordering (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(
418+
"Names should have consistent ordering. Consider a list instead."
419+
)
415420

416421

417422
def _read(filepath_or_buffer: FilePathOrBuffer, kwds):

0 commit comments

Comments
 (0)