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: DataFrame.from_records() ignores columns with iterator and nrows=0 #61140

Open
2 of 3 tasks
jlumpe opened this issue Mar 17, 2025 · 1 comment · May be fixed by #61143
Open
2 of 3 tasks

BUG: DataFrame.from_records() ignores columns with iterator and nrows=0 #61140

jlumpe opened this issue Mar 17, 2025 · 1 comment · May be fixed by #61143
Labels
Bug IO Data IO issues that don't fit into a more specific label

Comments

@jlumpe
Copy link

jlumpe commented Mar 17, 2025

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
>>> rows = []
>>> pd.DataFrame.from_records(iter(rows), columns=['a', 'b'], nrows=0)
Empty DataFrame
Columns: []
Index: []

Issue Description

Passing an empty iterator to DataFrame.from_records() along with nrows=0 results in a DataFrame with no columns, even if the columns argument is provided.

Oddly, the correct result is obtained with an empty iterator and nrows > 0. However, columns are still ignored if the iterator is non-empty and nrows=0.

Expected Behavior

Should return a DataFrame with 0 rows and the columns provided.

Installed Versions

INSTALLED VERSIONS
------------------
commit                : 0691c5cf90477d3503834d983f69350f250a6ff7
python                : 3.12.9
python-bits           : 64
OS                    : Linux
OS-release            : 5.15.167.4-microsoft-standard-WSL2
Version               : #1 SMP Tue Nov 5 00:21:55 UTC 2024
machine               : x86_64
processor             : x86_64
byteorder             : little
LC_ALL                : None
LANG                  : C.UTF-8
LOCALE                : C.UTF-8

pandas                : 2.2.3
numpy                 : 2.2.3
pytz                  : 2024.1
dateutil              : 2.9.0.post0
pip                   : 25.0.1
Cython                : None
sphinx                : None
IPython               : None
adbc-driver-postgresql: None
adbc-driver-sqlite    : None
bs4                   : None
blosc                 : None
bottleneck            : None
dataframe-api-compat  : None
fastparquet           : None
fsspec                : None
html5lib              : None
hypothesis            : None
gcsfs                 : None
jinja2                : None
lxml.etree            : None
matplotlib            : None
numba                 : None
numexpr               : None
odfpy                 : None
openpyxl              : None
pandas_gbq            : None
psycopg2              : None
pymysql               : None
pyarrow               : None
pyreadstat            : None
pytest                : None
python-calamine       : None
pyxlsb                : None
s3fs                  : None
scipy                 : None
sqlalchemy            : None
tables                : None
tabulate              : None
xarray                : None
xlrd                  : None
xlsxwriter            : None
zstandard             : None
tzdata                : 2025.1
qtpy                  : None
pyqt5                 : None
@jlumpe jlumpe added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 17, 2025
@kpvenkat47
Copy link

Hi, I’ve already resolved this issue and submitted a pull request #61143 . I’m currently awaiting feedback from the pandas community. Thank you!

@rhshadrach rhshadrach added IO Data IO issues that don't fit into a more specific label and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 19, 2025
@rhshadrach rhshadrach linked a pull request Mar 26, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO Data IO issues that don't fit into a more specific label
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants