Skip to content

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

Open
@bgbg

Description

@bgbg

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
....

Metadata

Metadata

Assignees

Labels

ConstructorsSeries/DataFrame/Index/pd.array ConstructorsEnhancementError ReportingIncorrect or improved errors from pandas

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions