-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
PERF: Optimize read_excel nrows #46894
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
Changes from 11 commits
6fd0422
e4d52d8
eb83a74
2c643c7
dc14ac2
2e79141
943f866
10a8a3e
30a280c
bbc1f1d
2018d26
c108a97
3aad835
7c913d6
d6e1df3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,6 +70,7 @@ | |
pop_header_name, | ||
) | ||
from pandas.io.parsers import TextParser | ||
from pandas.io.parsers.readers import validate_integer | ||
|
||
_read_excel_doc = ( | ||
""" | ||
|
@@ -563,7 +564,7 @@ def get_sheet_by_index(self, index: int): | |
pass | ||
|
||
@abc.abstractmethod | ||
def get_sheet_data(self, sheet, convert_float: bool): | ||
def get_sheet_data(self, sheet, convert_float: bool, rows: int | None = None): | ||
pass | ||
|
||
def raise_if_bad_sheet_by_index(self, index: int) -> None: | ||
|
@@ -577,6 +578,98 @@ def raise_if_bad_sheet_by_name(self, name: str) -> None: | |
if name not in self.sheet_names: | ||
raise ValueError(f"Worksheet named '{name}' not found") | ||
|
||
def _check_skiprows_func( | ||
self, | ||
skiprows: Callable, | ||
rows_to_use: int, | ||
) -> int: | ||
""" | ||
Determine how many file rows are required to obtain `nrows` data | ||
rows when `skiprows` is a function. | ||
|
||
Parameters | ||
---------- | ||
skiprows : function | ||
The function passed to read_excel by the user. | ||
rows_to_use : int | ||
The number of rows that will be needed for the header and | ||
the data. | ||
|
||
Returns | ||
------- | ||
int | ||
""" | ||
i = 0 | ||
rows_used_so_far = 0 | ||
while rows_used_so_far < rows_to_use: | ||
if not skiprows(i): | ||
rows_used_so_far += 1 | ||
i += 1 | ||
return i | ||
|
||
def _calc_rows( | ||
self, | ||
header: int | Sequence[int] | None, | ||
index_col: int | Sequence[int] | None, | ||
skiprows: Sequence[int] | int | Callable[[int], object] | None, | ||
nrows: int | None, | ||
) -> int | None: | ||
""" | ||
If nrows specified, find the number of rows needed from the | ||
file, otherwise return None. | ||
|
||
|
||
Parameters | ||
---------- | ||
header : int, list of int, or None | ||
See read_excel docstring. | ||
index_col : int, list of int, or None | ||
See read_excel docstring. | ||
skiprows : list-like, int, callable, or None | ||
See read_excel docstring. | ||
nrows : int or None | ||
See read_excel docstring. | ||
|
||
Returns | ||
------- | ||
int or None | ||
""" | ||
if nrows is None: | ||
return None | ||
if header is None: | ||
header_rows = 1 | ||
elif is_integer(header): | ||
assert isinstance(header, int) | ||
header_rows = 1 + header | ||
else: | ||
assert isinstance(header, Sequence) | ||
header_rows = 1 + header[-1] | ||
# If there is a MultiIndex header and an index then there is also | ||
# a row containing just the index name(s) | ||
if is_list_like(header) and index_col is not None: | ||
assert isinstance(header, Sequence) | ||
if len(header) > 1: | ||
header_rows += 1 | ||
if skiprows is None: | ||
return header_rows + nrows | ||
if is_integer(skiprows): | ||
assert isinstance(skiprows, int) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the future we'll be able to have |
||
return header_rows + nrows + skiprows | ||
if is_list_like(skiprows): | ||
assert isinstance(skiprows, Sequence) | ||
return self._check_skiprows_func( | ||
lambda x: x in skiprows, | ||
header_rows + nrows, | ||
) | ||
if callable(skiprows): | ||
return self._check_skiprows_func( | ||
skiprows, | ||
header_rows + nrows, | ||
) | ||
ahawryluk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# else unexpected skiprows type: read_excel will not optimize | ||
# the number of rows read from file | ||
return None | ||
|
||
def parse( | ||
self, | ||
sheet_name: str | int | list[int] | list[str] | None = 0, | ||
|
@@ -613,6 +706,7 @@ def parse( | |
) | ||
|
||
validate_header_arg(header) | ||
validate_integer("nrows", nrows) | ||
|
||
ret_dict = False | ||
|
||
|
@@ -643,7 +737,8 @@ def parse( | |
else: # assume an integer if not a string | ||
sheet = self.get_sheet_by_index(asheetname) | ||
|
||
data = self.get_sheet_data(sheet, convert_float) | ||
file_rows_needed = self._calc_rows(header, index_col, skiprows, nrows) | ||
data = self.get_sheet_data(sheet, convert_float, file_rows_needed) | ||
if hasattr(sheet, "close"): | ||
# pyxlsb opens two TemporaryFiles | ||
sheet.close() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,13 +120,7 @@ def __init__(self, kwds) -> None: | |
|
||
# validate header options for mi | ||
self.header = kwds.get("header") | ||
if isinstance(self.header, (list, tuple, np.ndarray)): | ||
if not all(map(is_integer, self.header)): | ||
raise ValueError("header must be integer or list of integers") | ||
if any(i < 0 for i in self.header): | ||
raise ValueError( | ||
"cannot specify multi-index header with negative integers" | ||
) | ||
Comment on lines
-123
to
-129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might be missing it, is validate_header_arg called somewhere instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but it took me a while to find it. It's called earlier in TextFileReader:
|
||
if is_list_like(self.header): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. allow_sets=False |
||
if kwds.get("usecols"): | ||
raise ValueError( | ||
"cannot specify usecols when specifying a multi-index header" | ||
|
@@ -138,31 +132,20 @@ def __init__(self, kwds) -> None: | |
|
||
# validate index_col that only contains integers | ||
if self.index_col is not None: | ||
is_sequence = isinstance(self.index_col, (list, tuple, np.ndarray)) | ||
if not ( | ||
is_sequence | ||
is_list_like(self.index_col) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. allow_sets=False |
||
and all(map(is_integer, self.index_col)) | ||
or is_integer(self.index_col) | ||
): | ||
raise ValueError( | ||
"index_col must only contain row numbers " | ||
"when specifying a multi-index header" | ||
) | ||
elif self.header is not None: | ||
elif self.header is not None and self.prefix is not None: | ||
# GH 27394 | ||
if self.prefix is not None: | ||
raise ValueError( | ||
"Argument prefix must be None if argument header is not None" | ||
) | ||
# GH 16338 | ||
elif not is_integer(self.header): | ||
raise ValueError("header must be integer or list of integers") | ||
# GH 27779 | ||
elif self.header < 0: | ||
raise ValueError( | ||
"Passing negative integer to header is invalid. " | ||
"For no header, use header=None instead" | ||
) | ||
raise ValueError( | ||
"Argument prefix must be None if argument header is not None" | ||
) | ||
|
||
self._name_processed = False | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.