Skip to content
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

BUG: TextFileReader from read_csv() fails to iterate when using a callable skiprows and engine='python' #55677

Closed
3 tasks done
aaronfihn opened this issue Oct 25, 2023 · 2 comments · Fixed by #56250
Closed
3 tasks done
Assignees
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@aaronfihn
Copy link

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 pandas as pd

# write a csv file to read with read_csv()
dummy_csv = """col_a
10
20
30
40"""

with open("dummy.csv", "w") as file:
    file.write(dummy_csv)

# fetch a TextFileReader to iterate through the file chunk by chunk
text_file_reader = pd.read_csv("dummy.csv",
                               skiprows=lambda row: False,
                               engine='python',
                               chunksize=3)

with text_file_reader as reader:
    iterator = iter(reader)
    next(iterator)  # TypeError: 'function' object is not iterable

Issue Description

A TypeError is raised when iterating through a TextFileReader that was returned by read_csv() using a callable skiprows argument and the python parsing engine.

Making any one of the following changes to the read_csv() arguments causes this code example to successfully complete without raising an error:

  • Setting skiprows=None
  • Setting skiprows=[]
  • Setting skiprows=[1]
  • Setting engine='c'

Expected Behavior

This code example should complete without printing any output or raising any error.

Installed Versions

INSTALLED VERSIONS ------------------ commit : e86ed37 python : 3.11.5.final.0 python-bits : 64 OS : Linux OS-release : 6.2.0-35-generic Version : #35~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Fri Oct 6 10:23:26 UTC 2 machine : x86_64 processor : x86_64 byteorder : little LC_ALL : None LANG : en_US.UTF-8 LOCALE : en_US.UTF-8

pandas : 2.1.1
numpy : 1.26.0
pytz : 2023.3.post1
dateutil : 2.8.2
setuptools : 68.0.0
pip : 23.3
Cython : None
pytest : None
hypothesis : None
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.3.5
dataframe-api-compat: None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : 2.8.7
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None
None

@aaronfihn aaronfihn added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Oct 25, 2023
@atetali
Copy link

atetali commented Nov 7, 2023

take

@atetali
Copy link

atetali commented Nov 30, 2023

Flytre and I worked together on fixing this bug (he did not "steal" my take)

mroeschke pushed a commit that referenced this issue Dec 5, 2023
… specified (#55677, #56323)  (#56250)

* Fix -GH 55677:
Added support for the python parser to handle using skiprows and chunk_size options at the same time to ensure API contract is met.

Added a regression test to ensure this bug can be quickly caught in the future if it reappears.

* Fix -GH 55677:
Added support for the python parser to handle using skiprows and chunk_size options at the same time to ensure API contract is met.

Added a regression test to ensure this bug can be quickly caught in the future if it reappears.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix -GH 55677:
Added support for the python parser to handle using skiprows and chunk_size options at the same time to ensure API contract is met.

Added a regression test to ensure this bug can be quickly caught in the future if it reappears.

* Fix -GH 55677:
Made changes consistment with mypy checking

* Fix -GH 55677:
Made changes consistment with mypy checking and pre-commit

* Fix -GH 55677 & 56323:
This commit:
-Fixes GH 56323 by replacing the python engine chunksize logic
-Fixes formatting on the added test_skiprows test case
-Fixes incorrect test in read_fwf that expected an output chunk of size 3 when chunksize=2 was specified.

* Trigger CI

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
2 participants