Skip to content

BUG: pandas-dev#58594 #59258

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

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ Interval
Indexing
^^^^^^^^
- Bug in :meth:`DataFrame.__getitem__` returning modified columns when called with ``slice`` in Python 3.12 (:issue:`57500`)
- Bug in :meth:`DataFrame.from_records` throwing a ``ValueError`` when passed an empty list in ``index`` (:issue:`58594`)
-

Missing
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7475,7 +7475,9 @@ def ensure_index_from_sequences(sequences, names=None) -> Index:
"""
from pandas.core.indexes.multi import MultiIndex

if len(sequences) == 1:
if len(sequences) == 0:
return Index([])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you use default_index(0) here? Can import it from pandas.core.indexes.api

Copy link
Contributor Author

@kirill-bash kirill-bash Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mroeschke,

That's not working since pandas.core.indexes.api imports pandas.core.indexes.base on L19 so I get the following error:

Traceback (most recent call last):
  .
  .
  .
  File "/home/op/git/pandas/pandas/core/indexes/base.py", line 177, in <module>
    from pandas.core.indexes.api import default_index
ImportError: cannot import name 'default_index' from partially initialized module 'pandas.core.indexes.api' (most likely due to a circular import) (/home/op/git/pandas/pandas/core/indexes/api.py)

Just out of curiosity why is default_index(0) preferred over Index([])?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll probably need to import it in the method.

default_index is, as the name implies, the "default" implementation of an index used in constructors if not explicitly specified

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for clarifying.

elif len(sequences) == 1:
if names is not None:
names = names[0]
return Index(maybe_sequence_to_range(sequences[0]), name=names)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/frame/constructors/test_from_records.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ def test_from_records_sequencelike_empty(self):
assert len(result) == 0
assert len(result.columns) == 0

def test_from_records_sequencelike_empty_index(self):
result = DataFrame.from_records([], index=[])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you make this a separate test?

assert len(result) == 0
assert len(result.columns) == 0
assert len(result.index) == 0

def test_from_records_dictlike(self):
# test the dict methods
df = DataFrame(
Expand Down