Skip to content

Commit 0970f19

Browse files
committed
ENH: Enable cleaning of unnamed columns for CSV parsers and adjust strictness in zip() calls
1 parent 09951eb commit 0970f19

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

pandas/io/parsers/base_parser.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,11 @@ def _extract_multi_indexer_columns(
237237
def extract(r):
238238
return tuple(r[i] for i in range(field_count) if i not in sic)
239239

240-
columns = list(zip(*(extract(r) for r in header), strict=False))
241-
# Clean the columns by removing placeholders.
242-
columns = self._clean_column_levels(columns)
240+
columns = list(zip(*(extract(r) for r in header), strict=True))
241+
242+
# Clean unnamed placeholders for CSV parsers only (GH#59560)
243+
if getattr(self, "_clean_csv_unnamed_columns", False):
244+
columns = self._clean_column_levels(columns)
243245

244246
names = columns.copy()
245247
for single_ic in sorted(ic):
@@ -732,7 +734,7 @@ def _is_generated_unnamed(level: str | None) -> bool:
732734
""
733735
if (
734736
level is None
735-
or str(level).strip() == ""
737+
or (isinstance(level, str) and level.strip() == "")
736738
or _is_generated_unnamed(level)
737739
)
738740
else level

pandas/io/parsers/c_parser_wrapper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class CParserWrapper(ParserBase):
6464

6565
def __init__(self, src: ReadCsvBuffer[str], **kwds) -> None:
6666
super().__init__(kwds)
67+
self._clean_csv_unnamed_columns = True
6768
self.kwds = kwds
6869
kwds = kwds.copy()
6970

pandas/io/parsers/python_parser.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ def __init__(self, f: ReadCsvBuffer[str] | list, **kwds) -> None:
101101
Workhorse function for processing nested list into DataFrame
102102
"""
103103
super().__init__(kwds)
104-
104+
# Only enable cleaning for CSV (file/buffer), not Excel (list)
105+
if not isinstance(f, list) and "has_index_names" not in kwds:
106+
self._clean_csv_unnamed_columns = True
105107
self.data: Iterator[list[str]] | list[list[Scalar]] = []
106108
self.buf: list = []
107109
self.pos = 0

0 commit comments

Comments
 (0)