Reputation: 7746
This code sets up a dictionary of datafames from data
which is itself a dataframe. data
is read from a file:
data = pandas.read_csv(QuoteData, usecols=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22])
Because the data
dataframe mixes many different time-frames, I separate them out into a dictionary of dataframes indexed by time. Then each dataframe in the dictionary are sorted by the shown criteria.
No matter what I try, I can't access the fist element of dodf[k]['underlying_bid']
. So for example dodf[k]['underlying_bid'][0]
works for the first dataframe, bur crashes on the second time through the loop. 0
is a key of the first dataframe column but not of the second?
Traceback (most recent call last):
File "cboelivedata.py", line 481, in <module>
main()
File "cboelivedata.py", line 422, in main
underlyingprice = dodf[k]['underlying_bid'][0]
File "/home/idf/anaconda2/lib/python2.7/site-packages/pandas/core/series.py", line 583, in __getitem__
result = self.index.get_value(self, key)
File "/home/idf/anaconda2/lib/python2.7/site-packages/pandas/indexes/base.py", line 1980, in get_value
tz=getattr(series.dtype, 'tz', None))
File "pandas/index.pyx", line 103, in pandas.index.IndexEngine.get_value (pandas/index.c:3332)
File "pandas/index.pyx", line 111, in pandas.index.IndexEngine.get_value (pandas/index.c:3035)
File "pandas/index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas/index.c:4018)
File "pandas/hashtable.pyx", line 303, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6610)
File "pandas/hashtable.pyx", line 309, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6554)
KeyError: 0
The code fragment:
dodf = {name: group for name, group in data.groupby('quote_datetime')}
dodf = collections.OrderedDict(sorted(dodf.items(), key=lambda x: x[0]))#key=operator.itemgetter))
for k in dodf:
df = dodf[k].sort_values(['option_type', 'Strike', 'Expiration'], ascending=True)
underlyingprice = dodf[k]['underlying_bid'] # **What goes here?**
Upvotes: 2
Views: 202