BUG: Regression creating DataFrame from nested dict
See original GitHub issueCode Sample, a copy-pastable example if possible
# Your code here
pop = {'Nevada': {2001: 2.4, 2002: 2.9},
'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}
pd.DataFrame(pop, index=[2001, 2002, 2003])
Problem description
Raises exception:
In [6]: pd.DataFrame(pop, index=[2001, 2002, 2003])
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-6-84df27ae30f4> in <module>()
----> 1 pd.DataFrame(pop, index=[2001, 2002, 2003])
~/miniconda/envs/arrow-dev/lib/python3.6/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
346 dtype=dtype, copy=copy)
347 elif isinstance(data, dict):
--> 348 mgr = self._init_dict(data, index, columns, dtype=dtype)
349 elif isinstance(data, ma.MaskedArray):
350 import numpy.ma.mrecords as mrecords
~/miniconda/envs/arrow-dev/lib/python3.6/site-packages/pandas/core/frame.py in _init_dict(self, data, index, columns, dtype)
457 arrays = [data[k] for k in keys]
458
--> 459 return _arrays_to_mgr(arrays, data_names, index, columns, dtype=dtype)
460
461 def _init_ndarray(self, values, index, columns, dtype=None, copy=False):
~/miniconda/envs/arrow-dev/lib/python3.6/site-packages/pandas/core/frame.py in _arrays_to_mgr(arrays, arr_names, index, columns, dtype)
7357
7358 # don't force copy because getting jammed in an ndarray anyway
-> 7359 arrays = _homogenize(arrays, index, dtype)
7360
7361 # from BlockManager perspective
~/miniconda/envs/arrow-dev/lib/python3.6/site-packages/pandas/core/frame.py in _homogenize(data, index, dtype)
7659 if isinstance(v, dict):
7660 if oindex is None:
-> 7661 oindex = index.astype('O')
7662
7663 if isinstance(index, (DatetimeIndex, TimedeltaIndex)):
AttributeError: 'list' object has no attribute 'astype'
This code has worked for about 10 years; is this a deliberate change?
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (8 by maintainers)
Top Results From Across the Web
Construct pandas DataFrame from items in nested dictionary
for me, the first method created a dataframe with a single index with tuples. the second method worked as desired/expected! – arturomp. Apr...
Read more >Version 0.16.2 (June 12, 2015) — pandas 1.5.2 documentation
Documentation on how to use numba with pandas, see here. What's new in v0.16.2 ... Bug in DataFrame construction from nested dict with...
Read more >Spark Release 3.2.0
In this release, Spark supports the Pandas API layer on Spark. ... Support to infer nested dict as a struct when creating a...
Read more >Release 0.13.0 - Statsmodels
PR #7543: Betareg rebased3 Beta regression. PR #7545: BUG: Correct index location of seasonal. PR #7546: MAINT: Fix contrasts for Pandas changes.
Read more >Python 3.10: Cool New Features for You to Try
Watch Now This tutorial has a related video course created by the Real Python team. ... This nested dictionary has a key called...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Pretty sure it’s this change: https://github.com/pandas-dev/pandas/commit/4efb39f01f5880122fa38d91e12d217ef70fad9e#diff-1e79abbbdd150d4771b91ea60a4e1cc7L7231
Thanks @wesm for noticing this, I had completely overlooked one code branch.