Skip to content

Commit dc61547

Browse files
phoflnickleus27
authored andcommitted
CLN: Refactor extract multiindex header call (pandas-dev#44399)
1 parent eca9f6c commit dc61547

File tree

3 files changed

+24
-39
lines changed

3 files changed

+24
-39
lines changed

pandas/io/parsers/base_parser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,14 @@ def _should_parse_dates(self, i: int) -> bool:
314314

315315
@final
316316
def _extract_multi_indexer_columns(
317-
self, header, index_names, col_names, passed_names: bool = False
317+
self, header, index_names, passed_names: bool = False
318318
):
319319
"""
320320
extract and return the names, index_names, col_names
321321
header is a list-of-lists returned from the parsers
322322
"""
323323
if len(header) < 2:
324-
return header[0], index_names, col_names, passed_names
324+
return header[0], index_names, None, passed_names
325325

326326
# the names are the tuples of the header that are not the index cols
327327
# 0 is the name of the index, assuming index_col is a list of column

pandas/io/parsers/c_parser_wrapper.py

+12-19
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,18 @@ def __init__(self, src: FilePathOrBuffer, **kwds):
7878
if self._reader.header is None:
7979
self.names = None
8080
else:
81-
if len(self._reader.header) > 1:
82-
# we have a multi index in the columns
83-
# error: Cannot determine type of 'names'
84-
# error: Cannot determine type of 'index_names'
85-
# error: Cannot determine type of 'col_names'
86-
(
87-
self.names, # type: ignore[has-type]
88-
self.index_names,
89-
self.col_names,
90-
passed_names,
91-
) = self._extract_multi_indexer_columns(
92-
self._reader.header,
93-
self.index_names, # type: ignore[has-type]
94-
self.col_names, # type: ignore[has-type]
95-
passed_names,
96-
)
97-
else:
98-
# error: Cannot determine type of 'names'
99-
self.names = list(self._reader.header[0]) # type: ignore[has-type]
81+
# error: Cannot determine type of 'names'
82+
# error: Cannot determine type of 'index_names'
83+
(
84+
self.names, # type: ignore[has-type]
85+
self.index_names,
86+
self.col_names,
87+
passed_names,
88+
) = self._extract_multi_indexer_columns(
89+
self._reader.header,
90+
self.index_names, # type: ignore[has-type]
91+
passed_names,
92+
)
10093

10194
# error: Cannot determine type of 'names'
10295
if self.names is None: # type: ignore[has-type]

pandas/io/parsers/python_parser.py

+10-18
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,16 @@ def __init__(self, f: FilePathOrBuffer | list, **kwds):
117117

118118
# Now self.columns has the set of columns that we will process.
119119
# The original set is stored in self.original_columns.
120-
if len(self.columns) > 1:
121-
# we are processing a multi index column
122-
# error: Cannot determine type of 'index_names'
123-
# error: Cannot determine type of 'col_names'
124-
(
125-
self.columns,
126-
self.index_names,
127-
self.col_names,
128-
_,
129-
) = self._extract_multi_indexer_columns(
130-
self.columns,
131-
self.index_names, # type: ignore[has-type]
132-
self.col_names, # type: ignore[has-type]
133-
)
134-
# Update list of original names to include all indices.
135-
self.num_original_columns = len(self.columns)
136-
else:
137-
self.columns = self.columns[0]
120+
# error: Cannot determine type of 'index_names'
121+
(
122+
self.columns,
123+
self.index_names,
124+
self.col_names,
125+
_,
126+
) = self._extract_multi_indexer_columns(
127+
self.columns,
128+
self.index_names, # type: ignore[has-type]
129+
)
138130

139131
# get popped off for index
140132
self.orig_names: list[int | str | tuple] = list(self.columns)

0 commit comments

Comments
 (0)