wesm/pandas — Issue #613: DataFrame list of dicts example fails
Author: lluang
Created: 2012-01-12 02:52:14+00:00
State: open

Working through the tutorial labeled pandas 0.7.0.dev-1139a8d documentation, I was having a problem with the list of dicts example

-----
In [1]: from pandas import *

In [2]: import numpy as np

In [14]: pandas.version
Out[14]:

# Prove that I have everything installed correctly
In [15]: data = np.zeros((2,),dtype=[('A', 'i4'),('B', 'f4'),('C', 'a10')])
In [16]: data[:] = [(1,2.,'Hello'),(2,3.,"World")]
In [17]: DataFrame(data)
Out[17]:
A B C
0 1 2 Hello
1 2 3 World

# List of dicts example that fails

In [18]: data2 = [{'a': 1, 'b': 2}, {'a': 5, 'b': 10, 'c': 20}]
In [19]: DataFrame(data2)
Out[19]:
0
0 {'a': 1, 'b': 2}
1 {'a': 5, 'c': 20, 'b': 10}

In [20]: DataFrame(data2, index=['first', 'second'])
Out[20]:
0
first {'a': 1, 'b': 2}
second {'a': 5, 'c': 20, 'b': 10}

In [21]: DataFrame(data2, columns=['a', 'b', 'c'])
AssertionError:
--------

Looking at the mailing list, you almost have the solution in your post on 9/28/2011 in response to "Re: [pystatsmodels] Getting "stuff" into dataframes..."

In [23]: row_data2 = dict(enumerate(data2))

In [24]: DataFrame.from_dict(row_data2, orient='index'))
Out[24]:
a b c
0 1 2 NaN
1 5 10 20

(which matches the result in the documentation)