Skip to content

RecursionError with pd.DataFrame.from_records #32805

Closed
@kdebrab

Description

@kdebrab

Code Sample, a copy-pastable example if possible

import pandas as pd
index = pd.CategoricalIndex([pd.Interval(-20, -10), pd.Interval(-10, 0), pd.Interval(0, 10)])
series_of_dicts = pd.Series([{'a': 1}, {'a': 2}, {'b': 3}], index=index)
frame = pd.DataFrame.from_records(series_of_dicts, index=index)

raises:

RecursionError: maximum recursion depth exceeded in comparison

Problem description

Though using pd.DataFrame.from_records with pd.Series objects is not explicitly supported, it does work in most of the cases. Only in case the index of the Series is a CategoricalIndex of Intervals, it suddenly doesn't work anymore.

E.g. the following works fine:

series_of_dicts2 = pd.Series([{'a': 1}, {'a': 2}, {'b': 3}])
frame = pd.DataFrame.from_records(series_of_dicts2, index=index)

And it even works when series_of_dicts2.index is a CategoricalIndex or a list of Intervals, but not when it is CategoricalIndex of Intervals. It is especially strange because the index of series_of_dicts2 is not even used for the output.

Expected Output

The same as when the following workaround is used:

frame = pd.DataFrame.from_records(series_of_dicts.values, index=index)
Out[2]: 
              a    b
(-20, -10]  1.0  NaN
(-10, 0]    2.0  NaN
(0, 10]     NaN  3.0

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.6.8.final.0
python-bits : 64
OS : Windows
OS-release : 10
machine : AMD64
processor : Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.None
pandas : 1.0.2
numpy : 1.18.2
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 40.6.2
Cython : 0.29.4
pytest : 5.3.5
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 1.2.8
lxml.etree : 4.5.0
html5lib : 1.0.1
pymysql : None
psycopg2 : None
jinja2 : 2.11.1
IPython : 7.13.0
pandas_datareader: None
bs4 : 4.6.1
bottleneck : 1.3.1
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.2.0
numexpr : 2.7.1
odfpy : None
openpyxl : 3.0.3
pandas_gbq : None
pyarrow : None
pytables : None
pytest : 5.3.5
pyxlsb : None
s3fs : 0.2.1
scipy : 1.4.1
sqlalchemy : 1.3.15
tables : None
tabulate : 0.8.6
xarray : 0.15.0
xlrd : 1.2.0
xlwt : 1.3.0
xlsxwriter : 1.2.8
numba : None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions