Skip to content

BUG: DataFrame.from_records fails when the argument is Series with integer indices not containing 0 #49949

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

Closed
3 tasks done
mgotrik opened this issue Nov 28, 2022 · 1 comment
Closed
3 tasks done
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@mgotrik
Copy link

mgotrik commented Nov 28, 2022

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

>>> import pandas as pd
>>> pd.DataFrame.from_records({0:['bird']}) # WORKS
>>> pd.DataFrame.from_records({1:['bird']}) # WORKS
>>> pd.DataFrame.from_records(pd.Series({0:['bird']})) # WORKS
>>> pd.DataFrame.from_records(pd.Series({1:['bird']})) # FAILS KeyError: 0
>>> pd.DataFrame.from_records([pd.Series({1:['bird']})]) # WORKS
>>> pd.DataFrame(pd.Series({1:['bird']})) #WORKS

Output:

~/python3.9/site-packages/pandas/core/frame.py in from_records(cls, data, index, exclude, columns, coerce_float, nrows)
   2071             arr_columns = columns
   2072         else:
-> 2073             arrays, arr_columns = to_arrays(data, columns)
   2074             if coerce_float:
   2075                 for i, arr in enumerate(arrays):

~/python3.9/site-packages/pandas/core/internals/construction.py in to_arrays(data, columns, dtype)
    773         return [], ensure_index([])
    774 
--> 775     elif isinstance(data[0], Categorical):
    776         # GH#38845 deprecate special case
    777         warnings.warn(

~/python3.9/site-packages/pandas/core/series.py in __getitem__(self, key)
    940 
    941         elif key_is_scalar:
--> 942             return self._get_value(key)
    943 
    944         if is_hashable(key):

~/python3.9/site-packages/pandas/core/series.py in _get_value(self, label, takeable)
   1049 
   1050         # Similar to Index.get_value, but we do not fall back to positional
-> 1051         loc = self.index.get_loc(label)
   1052         return self.index._get_values_for_loc(self, loc, label)
   1053 

~/python3.9/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   3361                 return self._engine.get_loc(casted_key)
   3362             except KeyError as err:
-> 3363                 raise KeyError(key) from err
   3364 
   3365         if is_scalar(key) and isna(key) and not self.hasnans:

KeyError: 0

Issue Description

When generating a DataFrame using DataFrame.from_records() and a series with integer indices as input, the function fails due to a lookup of data[0] in ../pandas/core/internals/construction.to_arrays.

Expected Behavior

When determining if the data is categorical it should look at the first item in the data, not the item with key = 0

Installed Versions

INSTALLED VERSIONS

commit : 91111fd
python : 3.9.15.final.0
python-bits : 64
OS : Linux
OS-release : 5.4.0-124-generic
Version : #140~18.04.1-Ubuntu SMP Fri Aug 5 11:43:34 UTC 2022
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 1.5.1
numpy : 1.23.4
pytz : 2022.1
dateutil : 2.8.2
setuptools : 65.5.0
pip : 22.2.2
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.6.3
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.6.0
pandas_datareader: None
bs4 : 4.11.1
bottleneck : 1.3.5
brotli :
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.5.3
numba : None
numexpr : 2.8.4
odfpy : None
openpyxl : 3.0.10
pandas_gbq : None
pyarrow : None
pyreadstat : None
pyxlsb : None
s3fs : None
scipy : 1.9.3
snappy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : 2.0.1
xlwt : 1.3.0
zstandard : None
tzdata : None

@mgotrik mgotrik added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Nov 28, 2022
@phofl
Copy link
Member

phofl commented Dec 5, 2022

Hi, thanks for your report. This is a duplicate of #40429

@phofl phofl closed this as completed Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants