Simple example:
from collections import namedtuple
import pandas
Price = namedtuple('Price', 'ticker date price')
a = Price('GE', '2010-01-01', 30.00)
b = Price('GE', '2010-01-02', 31.00)
l = [a, b]
df = pandas.DataFrame.from_records(l, index='ticker')
Traceback (most recent call last)
...
KeyError: 'ticker'
Harder example:
df2 = pandas.DataFrame.from_records(l, index=['ticker', 'date'])
df2
0 1 2
ticker GE 2010-01-01 30
date GE 2010-01-02 31
Now it thinks that ['ticker', 'date']
is the index itself, rather than the columns I want to use as the index.
Is there a way to do this without resorting to an intermediate numpy ndarray or using set_index
after the fact?