Skip to content

DataFrame.from_records fails when the argument is Series with non-default index #40429

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

Open
bgbg opened this issue Mar 14, 2021 · 5 comments
Open
Assignees
Labels
Constructors Series/DataFrame/Index/pd.array Constructors Enhancement Error Reporting Incorrect or improved errors from pandas

Comments

@bgbg
Copy link

bgbg commented Mar 14, 2021

Summary: if from_records is called upon a Series with index that doesn't start with 0, it fails.

import pandas as pd
print(f'Pandas version is {pd.__version__}')
data = pd.Series([{'a': 1, 'b': 2}, {'a': 11, 'b': 22}], index=[10, 20])
pd.DataFrame.from_records(data.values) # works
pd.DataFrame.from_records(data)  # fails

Output

Pandas version is 1.2.1
Traceback (most recent call last):
  File "/Users/boris/opt/anaconda3/lib/python3.8/site-packages/pandas/core/indexes/base.py", line 3080, in get_loc
    return self._engine.get_loc(casted_key)
  File "pandas/_libs/index.pyx", line 70, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/index.pyx", line 101, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 1625, in pandas._libs.hashtable.Int64HashTable.get_item
  File "pandas/_libs/hashtable_class_helper.pxi", line 1632, in pandas._libs.hashtable.Int64HashTable.get_item
KeyError: 0
....
@bgbg
Copy link
Author

bgbg commented Mar 14, 2021

Update: it is sufficient that the first element in the Series has the index 0. The rest of the indices are ignored:

data = pd.Series([{'a': 1, 'b': 2}, {'a': 11, 'b': 22}], index=[0, 20])
pd.DataFrame.from_records(data)
Out[16]: 
    a   b
0   1   2
1  11  22

@jreback
Copy link
Contributor

jreback commented Mar 14, 2021

@rhshadrach rhshadrach added Bug Closing Candidate May be closeable, needs more eyeballs Constructors Series/DataFrame/Index/pd.array Constructors labels Mar 14, 2021
@bgbg
Copy link
Author

bgbg commented Mar 15, 2021

@jreback In this case, shouldn't the constructor raise a TypeError or ValueError exception on any Series argument? Potentially with a short explanation?

@mzeitlin11
Copy link
Member

Raising a more helpful error here would be great, contributions welcome!

@mzeitlin11 mzeitlin11 added Error Reporting Incorrect or improved errors from pandas and removed Bug Closing Candidate May be closeable, needs more eyeballs labels Apr 8, 2021
@mzeitlin11 mzeitlin11 added this to the Contributions Welcome milestone Apr 8, 2021
@hasan-yaman
Copy link
Contributor

take

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 Enhancement Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants