diff --git a/pandas/io/parsers/python_parser.py b/pandas/io/parsers/python_parser.py index f2c9be66c0905..373a35f3102ef 100644 --- a/pandas/io/parsers/python_parser.py +++ b/pandas/io/parsers/python_parser.py @@ -17,6 +17,9 @@ from typing import ( IO, TYPE_CHECKING, + Hashable, + Iterator, + List, DefaultDict, Literal, cast, @@ -36,7 +39,6 @@ is_integer, is_numeric_dtype, ) -from pandas.core.dtypes.inference import is_dict_like from pandas.io.common import ( dedup_names, @@ -447,39 +449,18 @@ def _infer_columns( this_columns.append(c) if not have_mi_columns: - counts: DefaultDict = defaultdict(int) + defaultdict(int) # Ensure that regular columns are used before unnamed ones # to keep given names and mangle unnamed columns - col_loop_order = [ + [ i for i in range(len(this_columns)) if i not in this_unnamed_cols ] + this_unnamed_cols # TODO: Use pandas.io.common.dedup_names instead (see #50371) - for i in col_loop_order: - col = this_columns[i] - old_col = col - cur_count = counts[col] - - if cur_count > 0: - while cur_count > 0: - counts[old_col] = cur_count + 1 - col = f"{old_col}.{cur_count}" - if col in this_columns: - cur_count += 1 - else: - cur_count = counts[col] - - if ( - self.dtype is not None - and is_dict_like(self.dtype) - and self.dtype.get(old_col) is not None - and self.dtype.get(col) is None - ): - self.dtype.update({col: self.dtype.get(old_col)}) - this_columns[i] = col - counts[col] = cur_count + 1 + this_columns = list(dedup_names(this_columns, False)) + elif have_mi_columns: # if we have grabbed an extra line, but its not in our # format so save in the buffer, and create an blank extra