切片Pandas MultiIndex时的奇怪行为

时间:2014-03-04 09:29:38

标签: python pandas

我在尝试使用pandas MultiIndex切片数据时观察到一些非常奇怪的行为。我使用的版本是Pandas 0.13.1

我有一个包含一些时间序列股票指数数据的csv文件,我正在读取数据框架。然后我重置DataFrame的行索引,以便它由TradingDate和PriceType索引,其中PriceType是['open','close','high']之一 以下是代码:

sharesIndexDataDF=pd.read_csv('./stock_index_data.csv')
sharesIndexDF=sharesIndexDataDF.set_index(['TradingDate','PriceType'])

我现在尝试切片:

sharesIndexDF.ix[('2014/02/21','open'):('2014/02/24','open')]

但我最终得到了这个错误


KeyError Traceback(最近一次调用最后一次)  in() ----> 1 sharesIndexDF.ix [('2014/02/21','open'):('2014/02/24','open')] ... KeyError:'密钥长度(2)大于MultiIndex lexsort depth(1)'

陌生的事情是,当我使用一个稍微不同的文件所在的字符串 ['open','close','high']已被['categA','categB','categoriesC']取代。代码工作正常:

sharesIndexDataDF=pd.read_csv('./stock_index_data_reps.csv')
sharesIndexDF=sharesIndexDataDF.set_index(['TradingDate','PriceType'])
sharesIndexDF.ix[('2014/02/21','categA'):('2014/02/24','categA')]

产生

                      Nasdaq    S&P 500          Russell 2000
TradingDate PriceType           
2014/02/21  categA   4282.17     1841.07     1166.25
            categB   4263.41     1836.25     1164.63
            categC   4284.85     1846.13     1168.43
2014/02/24  categA   4273.32     1836.78     1166.74

文件内容如下:

stock_index_data.csv:

TradingDate,PriceType,Nasdaq,S&P 500,Russell 2000
2014/02/21,open,4282.17,1841.07,1166.25
2014/02/21,close,4263.41,1836.25,1164.63
2014/02/21,high,4284.85,1846.13,1168.43
2014/02/24,open,4273.32,1836.78,1166.74
2014/02/24,close,4292.97,1847.61,1174.55
2014/02/24,high,4311.13,1858.71,1180.29
2014/02/25,open,4298.48,1847.66,1176
2014/02/25,close,4287.59,1845.12,1173.95
2014/02/25,high,4307.51,1852.91,1179.43
2014/02/26,open,4300.45,1845.79,1176.11
2014/02/26,close,4292.06,1845.16,1181.72
2014/02/26,high,4316.82,1852.65,1188.06
2014/02/27,open,4291.47,1844.9,1179.28
2014/02/27,close,4318.93,1854.29,1187.94
2014/02/27,high,4322.46,1854.53,1187.94
2014/02/28,open,4323.52,1855.12,1189.19
2014/02/28,close,4308.12,1859.45,1183.03
2014/02/28,high,4342.59,1867.92,1193.5

stock_index_data_reps.csv:

TradingDate,PriceType,Nasdaq,S&P 500,Russell 2000
2014/02/21,categA,4282.17,1841.07,1166.25
2014/02/21,categB,4263.41,1836.25,1164.63
2014/02/21,categC,4284.85,1846.13,1168.43
2014/02/24,categA,4273.32,1836.78,1166.74
2014/02/24,categB,4292.97,1847.61,1174.55
2014/02/24,categC,4311.13,1858.71,1180.29
2014/02/25,categA,4298.48,1847.66,1176
2014/02/25,categB,4287.59,1845.12,1173.95
2014/02/25,categC,4307.51,1852.91,1179.43
2014/02/26,categA,4300.45,1845.79,1176.11
2014/02/26,categB,4292.06,1845.16,1181.72
2014/02/26,categC,4316.82,1852.65,1188.06
2014/02/27,categA,4291.47,1844.9,1179.28
2014/02/27,categB,4318.93,1854.29,1187.94
2014/02/27,categC,4322.46,1854.53,1187.94
2014/02/28,categA,4323.52,1855.12,1189.19
2014/02/28,categB,4308.12,1859.45,1183.03
2014/02/28,categC,4342.59,1867.92,1193.5

对此的任何帮助将不胜感激。

0 个答案:

没有答案