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: Passing an empty Index to pd.DataFrame.from_records() produces a ValueError #58594

Open
3 tasks done
uzzell opened this issue May 6, 2024 · 0 comments
Open
3 tasks done
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@uzzell
Copy link
Contributor

uzzell commented May 6, 2024

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

empty_df = pd.DataFrame([])  # works
df = pd.DataFrame([], index=empty_df.index)  # works
df = pd.DataFrame([], index=[])  # works
df = pd.DataFrame.from_records([])  # works
df = pd.DataFrame.from_records([], index=empty_df.index)  # raises ValueError
df = pd.DataFrame.from_records([], index=[])  # raises ValueError

Issue Description

Passing an empty list or Index to the index parameter of pd.DataFrame.from_records() leads to
ValueError: Must pass non-zero number of levels/codes. This is true even if one passes an empty list of records to the method.

This is surprising for a couple of reasons. First, the lengths of the records and the index match, so it seems as though it should work properly. Second, as shown above, the DataFrame constructor doesn't complain when I pass an empty list or Index to its index parameter.

Finally, the documentation for from_records() describes the index parameter as, "Field of array to use as the index, alternately a specific set of input labels to use." Intuitively, an empty set should be acceptable when there's an empty list of input data.

Expected Behavior

No error

Installed Versions

INSTALLED VERSIONS

commit : d9cdd2e
python : 3.12.2.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 165 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : English_United States.1252

pandas : 2.2.2
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : None
pip : 24.0
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
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : None
numba : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@uzzell uzzell added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 6, 2024
@uzzell uzzell changed the title BUG: Passing an empty list or Index to the pd.DataFrame.from_records() index parameter produces a ValueError BUG: Passing an empty Index to pd.DataFrame.from_records() produces a ValueError May 13, 2024
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
Development

No branches or pull requests

1 participant