Skip to content

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

Closed
@aaronfihn

Description

@aaronfihn

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

Metadata

Metadata

Assignees

Labels

BugNeeds TriageIssue that has not been reviewed by a pandas team member

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions