Skip to content

Commit d75e1fb

Browse files
committed
MAINT: Use set logic to check dups
1 parent b2b19f4 commit d75e1fb

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

pandas/io/parsers.py

+13-18
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ def _validate_integer(name, val, min_val=0):
384384
return val
385385

386386

387-
def _check_dup_names(names):
387+
def _validate_names(names):
388388
"""
389389
Check if the `names` parameter contains duplicates.
390390
@@ -395,25 +395,20 @@ def _check_dup_names(names):
395395
----------
396396
names : array-like or None
397397
An array containing a list of the names used for the output DataFrame.
398-
"""
399-
400-
if names is None:
401-
return
402398
403-
counts = {}
404-
warn_dups = False
405-
406-
for name in names:
407-
if name in counts:
408-
warn_dups = True
409-
break
399+
Returns
400+
-------
401+
names : array-like or None
402+
The original `names` parameter.
403+
"""
410404

411-
counts[name] = True
405+
if names is not None:
406+
if len(names) != len(set(names)):
407+
msg = ("Duplicate names specified. This "
408+
"will raise an error in the future.")
409+
warnings.warn(msg, FutureWarning, stacklevel=3)
412410

413-
if warn_dups:
414-
msg = ("Duplicate names specified. This "
415-
"will raise an error in the future.")
416-
warnings.warn(msg, FutureWarning, stacklevel=3)
411+
return names
417412

418413

419414
def _read(filepath_or_buffer, kwds):
@@ -440,7 +435,7 @@ def _read(filepath_or_buffer, kwds):
440435

441436
# Check for duplicates in names.
442437
names = kwds.get("names", None)
443-
_check_dup_names(names)
438+
_validate_names(names)
444439

445440
# Create the parser.
446441
parser = TextFileReader(filepath_or_buffer, **kwds)

0 commit comments

Comments
 (0)