Skip to content

applying dedup_names func #53964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 7 additions & 26 deletions pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
from typing import (
IO,
TYPE_CHECKING,
Hashable,
Iterator,
List,
DefaultDict,
Literal,
cast,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down