簡體   English   中英

datetime index KeyError:'標簽[2000-01-03 00:00:00]不在[index]中

[英]datetime index KeyError: 'the label [2000-01-03 00:00:00] is not in the [index]'

我有一個生成的pandas數據幀

dates = pandas.date_range('1/1/2000', periods=8)
df = pandas.DataFrame(rd.randn(8, 5), index=dates, columns=['call/put', 'expiration', 'strike', 'ask', 'bid'])
df.iloc[2,4]=0
df.iloc[2,3]=0
df.iloc[3,4]=0
df.iloc[3,3]=0
df.iloc[2,2]=0.5
df=df.append(df.iloc[2:3])
df.iloc[8:9,3:5]=1
df.iloc[8:9,2:3]=0.6
df=df.append(df.iloc[8:9])
df.iloc[9,2]=0.4

從df我獲得df4

df4=df[(df["ask"]==0) & (df["bid"]==0)]

當通過df4行進行循環時

for index, row in df4.iterrows():
  print index
  df_upperbound = df.iloc[index]

我得到索引KeyError:'標簽[2000-01-03 00:00:00]不在[index]'中。 在嘗試時

for index, row in df4.iterrows():
  print index
  df_upperbound = df.xs[index]

我得到TypeError:'instancemethod'對象沒有屬性' getitem '。 我使用pandas 0.14.0和python 2.7.7。 如何在df內迭代df4的索引?

文檔提供了許多索引datetime索引DataFrame ,例如:

In [64]:

index
Out[64]:
Timestamp('2000-01-03 00:00:00')
In [65]:

print df.ix[index.to_datetime()]
            call/put  expiration  strike  ask  bid
2000-01-03  0.830035    -0.42598     0.5    0    0
2000-01-03  0.830035    -0.42598     0.6    1    1
2000-01-03  0.830035    -0.42598     0.4    1    1
In [66]:

print df.ix[index]
            call/put  expiration  strike  ask  bid
2000-01-03  0.830035    -0.42598     0.5    0    0
2000-01-03  0.830035    -0.42598     0.6    1    1
2000-01-03  0.830035    -0.42598     0.4    1    1
In [67]:

print df[index.strftime('%m/%d/%y')]
            call/put  expiration  strike  ask  bid
2000-01-03  0.830035    -0.42598     0.5    0    0
2000-01-03  0.830035    -0.42598     0.6    1    1
2000-01-03  0.830035    -0.42598     0.4    1    1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM