diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index bec66134eae57d..d26d57bb8fcf85 100755 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -384,7 +384,7 @@ def _validate_integer(name, val, min_val=0): return val -def _check_dup_names(names): +def _validate_names(names): """ Check if the `names` parameter contains duplicates. @@ -395,25 +395,20 @@ def _check_dup_names(names): ---------- names : array-like or None An array containing a list of the names used for the output DataFrame. - """ - - if names is None: - return - counts = {} - warn_dups = False - - for name in names: - if name in counts: - warn_dups = True - break + Returns + ------- + names : array-like or None + The original `names` parameter. + """ - counts[name] = True + if names is not None: + if len(names) != len(set(names)): + msg = ("Duplicate names specified. This " + "will raise an error in the future.") + warnings.warn(msg, FutureWarning, stacklevel=3) - if warn_dups: - msg = ("Duplicate names specified. This " - "will raise an error in the future.") - warnings.warn(msg, FutureWarning, stacklevel=3) + return names def _read(filepath_or_buffer, kwds): @@ -439,8 +434,7 @@ def _read(filepath_or_buffer, kwds): nrows = _validate_integer('nrows', kwds.get('nrows', None)) # Check for duplicates in names. - names = kwds.get("names", None) - _check_dup_names(names) + names = _validate_names(kwds.get("names", None)) # Create the parser. parser = TextFileReader(filepath_or_buffer, **kwds)