Skip to content

BUG: read_excel skiprows callable can result in infinite loop #45585

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

Closed
3 tasks done
bram2000 opened this issue Jan 24, 2022 · 0 comments · Fixed by #45586
Closed
3 tasks done

BUG: read_excel skiprows callable can result in infinite loop #45585

bram2000 opened this issue Jan 24, 2022 · 0 comments · Fixed by #45586
Labels
Bug IO Excel read_excel, to_excel
Milestone

Comments

@bram2000
Copy link
Contributor

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

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

@bram2000 bram2000 added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 24, 2022
@lithomas1 lithomas1 added IO Excel read_excel, to_excel and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 27, 2022
@jreback jreback added this to the 1.5 milestone Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Excel read_excel, to_excel
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants