KeyError:' EventTime'

时间:2018-04-17 20:25:56

标签: python pandas keyerror

这是重新格式化之前的数据集(示例)

,Unnamed: 0,#=TimeAndSale,EventSymbol,EventTime,Time,Sequence,ExchangeCode,Price,Size,BidPrice,AskPrice,SaleConditions,Flags
0,367,TimeAndSale,ZNGA,2015-03-30 09:30:00.405,20150331-093000-0400,395:2,Q,2.72,138914,2.76,2.8,@O X,22596
1,368,TimeAndSale,ZNGA,2015-03-30 09:30:00.405,20150331-093000-0400,395:3,Q,2.71,138914,2.76,2.8,@  Q,8256
2,391,TimeAndSale,ZNGA,2015-03-30 09:30:01.000,20150331-093000-0400,990:4,D,2.73,300,2.75,2.76,@4  ,22592
3,399,TimeAndSale,ZNGA,2015-03-30 09:30:02.115,20150331-093002-0400,105:5,D,2.43,200,2.75,2.76,@4  ,22592
4,401,TimeAndSale,ZNGA,2015-03-30 09:30:02.201,20150331-093002-0400,192:6,D,2.23,2,2.75,2.76,@4 I,22592
5,404,TimeAndSale,ZNGA,2015-03-30 09:30:02.660,20150331-093002-0400,649:7,Q,2.54,4,2.75,2.76,@  I,8256
6,422,TimeAndSale,ZNGA,2015-03-30 09:30:04.337,20150331-093004-0400,327:9,D,2.23,287,2.75,2.76,@   ,8260
7,433,TimeAndSale,ZNGA,2015-03-30 09:30:06.814,20150331-093006-0400,803:10,D,2.23,17,2.75,2.76,@  I,8256
8,437,TimeAndSale,ZNGA,2015-03-30 09:30:07.922,20150331-093007-0400,912:12,Q,2.742,100,2.75,2.76,@F  ,22596
9,444,TimeAndSale,ZNGA,2015-03-30 09:30:09.081,20150331-093009-0400,71:13,D,2.731,3,2.75,2.76,@  I,8256
10,445,TimeAndSale,ZNGA,2015-03-31 09:30:09.468,20150331-093009-0400,458:15,Q,2.72,100,2.75,2.76,@F  ,22596
11,448,TimeAndSale,ZNGA,2015-03-31 09:30:15.828,20150331-093015-0400,818:16,D,2.72,5,2.75,2.76,@4ZI,22592
12,450,TimeAndSale,ZNGA,2015-03-31 09:30:15.855,20150331-093015-0400,845:17,D,2.71,100,2.75,2.76,@4Z ,22592
13,451,TimeAndSale,ZNGA,2015-03-31 09:30:15.858,20150331-093015-0400,848:18,D,2.75,66,2.75,2.76,@4ZI,22592
14,452,TimeAndSale,ZNGA,2015-03-31 09:30:15.858,20150331-093015-0400,848:19,D,2.714,8,2.75,2.76,@4ZI,22592
15,453,TimeAndSale,ZNGA,2015-03-31 09:30:15.858,20150331-093015-0400,848:20,D,2.732,2,2.75,2.76,@4ZI,22592
16,454,TimeAndSale,ZNGA,2015-03-31 09:30:15.858,20150331-093015-0400,848:21,D,2.732,2,2.75,2.76,@4ZI,22592
17,455,TimeAndSale,ZNGA,2015-03-31 09:30:15.858,20150331-093015-0400,848:22,D,2.714,10,2.75,2.76,@4ZI,22592
18,459,TimeAndSale,ZNGA,2015-03-31 09:30:20.840,20150331-093020-0400,830:23,D,2.742,40,2.75,2.76,@  I,8256
19,460,TimeAndSale,ZNGA,2015-03-31 09:30:21.659,20

这是格式化上述数据的代码

CANDLESTICKS_LENGTH = '1min'

time_and_sales = pd.read_csv('/HFT/Data_HFT/TnS/TnS_ZNGA.csv', nrows=10000)
time_and_sales['EventTime'] = time_and_sales['EventTime'].map(lambda x: datetime.strptime(str(x), "%Y-%m-%d %H:%M:%S.%f"))

time_and_sales = time_and_sales.loc[:, ["EventTime", "Price", "Size"]]
time_and_sales = time_and_sales.set_index('EventTime')
price = time_and_sales.resample(CANDLESTICKS_LENGTH, how={'Price': 'ohlc'}).bfill()['Price']
vol = time_and_sales.resample(CANDLESTICKS_LENGTH, how={'Size': 'sum'}).bfill()
stock = pd.concat([price, vol], axis=1)
stock.columns = ["Open", "High", "Low", "Close", "Volume"]

这是上面代码生成的数据框

    Open    High    Low Close   Volume
EventTime                   
2015-03-31 09:30:00 2.7500  2.7599  2.7500  2.7500  288625
2015-03-31 09:31:00 2.7500  2.7600  2.7450  2.7550  77171
2015-03-31 09:32:00 2.7600  2.7600  2.7500  2.7600  3559
2015-03-31 09:33:00 2.7550  2.7600  2.7501  2.7501  700
2015-03-31 09:34:00 2.7550  2.7600  2.7500  2.7550  3500
2015-03-31 09:35:00 2.7600  2.7600  2.7500  2.7501  1170
2015-03-31 09:36:00 2.7550  2.7600  2.7500  2.7500  6000
2015-03-31 09:37:00 2.7550  2.7600  2.7500  2.7600  5400
2015-03-31 09:38:00 2.7600  2.7600  2.7500  2.7600  2100
2015-03-31 09:39:00 2.7600  2.7700  2.7550  2.7700  70395
2015-03-31 09:40:00 2.7700  2.7700  2.7500  2.7590  26336
2015-03-31 09:41:00 2.7600  2.7600  2.7500  2.7550  15810
2015-03-31 09:42:00 2.7600  2.7600  2.7500  2.7500  6400

当我尝试使用KeyError: 'EventTime'stock['EventTime']进行查询时,我获得了stock.EventTime。如何从stock属性

访问EventTime

1 个答案:

答案 0 :(得分:2)

将EventTime设置为您的索引。访问它的一种简单方法是通过重置索引使EventTime再次成为常规列。

df.reset_index()