本文介绍了日期时间索引KeyError:'标签[2000-01-03 00:00:00]不在[索引]中'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫数据帧,

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=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]'中。
尝试

I get index KeyError: 'the label [2000-01-03 00:00:00] is not in the [index]'.When trying

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的索引项?

I get TypeError: 'instancemethod' object has no attribute 'getitem'.I use pandas 0.14.0 and python 2.7.7.How can I iterate through the index entrys of df4 within df?

推荐答案

提供了多种方法来索引 datetime 索引 DataFrame ,例如:

The documentation provides a number of ways to index a datetime index DataFrame, a few examples:

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

这篇关于日期时间索引KeyError:'标签[2000-01-03 00:00:00]不在[索引]中'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:27