Keyerror Python Dict raise KeyError(key) from err trading app

I am printing my dictionary and i get the output like this:

opening_range_bars = minute_bars.loc[opening_range_mask]

print(opening_range_bars)
                       open     high      low  close  volume
time                                                             
2021-02-16 00:00:00-05:00  51.16  51.7500  50.4850  51.04   99823
2021-02-17 00:00:00-05:00  50.73  51.3921  49.7025  50.91  149788
2021-02-18 00:00:00-05:00  50.45  51.1200  49.7650  50.69  133951
2021-02-19 00:00:00-05:00  50.64  51.4600  50.6400  51.44  133578
                              CMI                                 
                             open     high     low   close  volume
time                                                              
2021-02-16 00:00:00-05:00  244.47  247.565  243.26  244.52  726380
2021-02-17 00:00:00-05:00  244.23  245.810  238.85  241.81  719238
2021-02-18 00:00:00-05:00  239.50  241.693  237.72  241.67  878032
2021-02-19 00:00:00-05:00  243.24  248.550  242.34  246.89  895406

and as i print :

for key in opening_range_bars.keys():
        print(opening_range_bars.keys())

i get :

MultiIndex([('CMI',   'open'),
            ('CMI',   'high'),
            ('CMI',    'low'),
            ('CMI',  'close'),
            ('CMI', 'volume')],
           )
MultiIndex([('CMI',   'open'),
            ('CMI',   'high'),
            ('CMI',    'low'),
            ('CMI',  'close'),
            ('CMI', 'volume')],

but if i want to refer to 'low' column :

opening_range_low = opening_range_bars['low'].min()
print(opening_range_low)

i get key error:

raise KeyError(key) from err
KeyError: 'low'

thanks for help in advance.

1 answer

  • answered 2021-02-27 17:22 farbiondriven

    Please try with:

    opening_range_low = opening_range_bars.loc[:, [('CMI', 'low')]].min()