BUG: TextFileReader from read_csv() fails to iterate when using a callable skiprows and engine='python' · Issue #55677 · pandas-dev/pandas (original) (raw)

Pandas version checks

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:

Expected Behavior

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

Installed Versions

INSTALLED VERSIONS ------------------ commit : e86ed37python : 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