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

BUG: pandas-dev#58594 #59258

merged 4 commits into from
Jul 18, 2024

Conversation

kirill-bash
Copy link
Contributor

@kirill-bash kirill-bash commented Jul 16, 2024

@kirill-bash
Copy link
Contributor Author

No errors raised for all cases noted in #58594.

Python 3.10.14 | packaged by conda-forge | (main, Mar 20 2024, 12:45:18) [GCC 12.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 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)  # works now!
>>> df = pd.DataFrame.from_records([], index=[])  # works now!

@@ -148,6 +148,11 @@ def test_from_records_sequencelike_empty(self):
assert len(result) == 0
assert len(result.columns) == 0

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?

@@ -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 []
Copy link
Member

Choose a reason for hiding this comment

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

According to the typing, this should return an Index

@@ -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 as an index (:issue:`58594`)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- Bug in :meth:`DataFrame.from_records()` throwing a ValueError when passed an empty list as an index (:issue:`58594`)
- Bug in :meth:`DataFrame.from_records` throwing a ``ValueError`` when passed an empty list in ``index`` (:issue:`58594`)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mroeschke, I just pushed a commit to address all your feedback. Mind taking another look?

Thanks!

@kirill-bash kirill-bash requested a review from mroeschke July 16, 2024 23:52
@@ -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.

@kirill-bash kirill-bash requested a review from mroeschke July 17, 2024 22:58
@mroeschke mroeschke added the Constructors Series/DataFrame/Index/pd.array Constructors label Jul 18, 2024
@mroeschke mroeschke added this to the 3.0 milestone Jul 18, 2024
@mroeschke mroeschke merged commit 941d079 into pandas-dev:main Jul 18, 2024
45 checks passed
@mroeschke
Copy link
Member

Thanks @kirill-bash

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Constructors Series/DataFrame/Index/pd.array Constructors
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Passing an empty Index to pd.DataFrame.from_records() produces a ValueError
2 participants