Skip to content

CLN: Refactor extract multiindex header call #44399

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

Merged
merged 1 commit into from
Nov 12, 2021
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions pandas/io/parsers/base_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,14 +314,14 @@ def _should_parse_dates(self, i: int) -> bool:

@final
def _extract_multi_indexer_columns(
self, header, index_names, col_names, passed_names: bool = False
self, header, index_names, passed_names: bool = False
):
"""
extract and return the names, index_names, col_names
header is a list-of-lists returned from the parsers
"""
if len(header) < 2:
return header[0], index_names, col_names, passed_names
return header[0], index_names, None, passed_names

# the names are the tuples of the header that are not the index cols
# 0 is the name of the index, assuming index_col is a list of column
Expand Down
31 changes: 12 additions & 19 deletions pandas/io/parsers/c_parser_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,18 @@ def __init__(self, src: FilePathOrBuffer, **kwds):
if self._reader.header is None:
self.names = None
else:
if len(self._reader.header) > 1:
# we have a multi index in the columns
# error: Cannot determine type of 'names'
# error: Cannot determine type of 'index_names'
# error: Cannot determine type of 'col_names'
(
self.names, # type: ignore[has-type]
self.index_names,
self.col_names,
passed_names,
) = self._extract_multi_indexer_columns(
self._reader.header,
self.index_names, # type: ignore[has-type]
self.col_names, # type: ignore[has-type]
passed_names,
)
else:
# error: Cannot determine type of 'names'
self.names = list(self._reader.header[0]) # type: ignore[has-type]
# error: Cannot determine type of 'names'
# error: Cannot determine type of 'index_names'
(
self.names, # type: ignore[has-type]
self.index_names,
self.col_names,
passed_names,
) = self._extract_multi_indexer_columns(
self._reader.header,
self.index_names, # type: ignore[has-type]
passed_names,
)

# error: Cannot determine type of 'names'
if self.names is None: # type: ignore[has-type]
Expand Down
28 changes: 10 additions & 18 deletions pandas/io/parsers/python_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,16 @@ def __init__(self, f: FilePathOrBuffer | list, **kwds):

# Now self.columns has the set of columns that we will process.
# The original set is stored in self.original_columns.
if len(self.columns) > 1:
# we are processing a multi index column
# error: Cannot determine type of 'index_names'
# error: Cannot determine type of 'col_names'
(
self.columns,
self.index_names,
self.col_names,
_,
) = self._extract_multi_indexer_columns(
self.columns,
self.index_names, # type: ignore[has-type]
self.col_names, # type: ignore[has-type]
)
# Update list of original names to include all indices.
self.num_original_columns = len(self.columns)
else:
self.columns = self.columns[0]
# error: Cannot determine type of 'index_names'
(
self.columns,
self.index_names,
self.col_names,
_,
) = self._extract_multi_indexer_columns(
self.columns,
self.index_names, # type: ignore[has-type]
)

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