BUG: read_excel skiprows callable can result in infinite loop (original) (raw)

Pandas version checks

Reproducible Example

import tempfile

import pandas as pd from pandas.testing import assert_frame_equal

source_df = pd.DataFrame([{"row": 0}, {"row": 1}, {"row": 2}, {"row": 3}])

with tempfile.NamedTemporaryFile("w", suffix=".xlsx") as xlsx_fd: source_df.to_excel(xlsx_fd.name, index=False)

df = pd.read_excel(
    xlsx_fd.name, skiprows=lambda x: x not in [0, 2]
)  # infinite loop here

expected_df = pd.DataFrame([{"row": 1}, {"row": 3}])

assert_frame_equal(df, expected_df)

Issue Description

In certain cases the skiprows callable on read_excel will loop infinitely. It's related to the sequence of True and False values coming out of the function. I've already written a unit test that triggers the issue and a fix to address it. Creating this bug to track the fix against.

Expected Behavior

The test provided above should pass, where currently it will loop forever.

To put it another way; skiprows callable should never be called with a row index that is greater than the length of the document.

Installed Versions

❯ ls bug.py | entr -c python bug.py /Users/bram/Code/nca/pandas/pandas/compat/_optional.py:149: UserWarning: Pandas requires version '1.3.1' or newer of 'bottleneck' (version '1.2.1' currently installed). warnings.warn(msg, UserWarning)

INSTALLED VERSIONS

commit : 04f5721
python : 3.9.9.final.0
python-bits : 64
OS : Darwin
OS-release : 21.2.0
Version : Darwin Kernel Version 21.2.0: Sun Nov 28 20:28:41 PST 2021; root:xnu-8019.61.5~1/RELEASE_ARM64_T6000
machine : arm64
processor : arm
byteorder : little
LC_ALL : None
LANG : en_GB.UTF-8
LOCALE : en_GB.UTF-8

pandas : 1.5.0.dev0+130.g04f5721617
numpy : 1.22.1
pytz : 2021.3
dateutil : 2.8.2
pip : 21.2.4
setuptools : 58.1.0
Cython : 0.29.26
pytest : 6.2.5
hypothesis : 6.36.0
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : 1.2.1
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : 3.0.9
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : None
zstandard : None