我在熊猫索引方面遇到问题,希望您能为我提供帮助:
rng = pd.date_range('2015-12-31 21:00:00', periods=7, freq='H')
df = pd.DataFrame({ 'Val' : np.random.randn(len(rng)) }, index=rng)
first_value_of_year = df['2016'].first('1H').index
将年份的第一个值返回为DatetimeIndex:
DatetimeIndex(['2016-01-01'], dtype='datetime64[ns]', freq='H')
当我使用该索引调用数据框时,一切似乎都工作正常:
df.loc[first_value_of_year]
返回
+------------------------+-----------+
| | Val |
+------------------------+-----------+
| 2016-01-01 | 0.144044 |
所以,到目前为止一切都很好! 但是,如果我想使所有值都大于该值,那么它将不再起作用:
df.loc[df.index >= first_value_of_year]
并给出ValueError(长度必须匹配):
但是如果我将时间戳本身作为字符串使用相同的命令,它将执行应做的事情
df.loc[df.index >= '2016-01-01 00:00:00']
返回
+------------------------+-----------+
| | Val |
+------------------------+-----------+
| 2016-01-01 01:00:00 | 1.454274 |
| 2016-01-01 02:00:00 | 0.761038 |
| 2016-01-01 03:00:00 | 0.121675 |
那么,我在这里想念什么?如何正确访问所有大于DatetimeIndex变量的值?
谢谢!
答案 0 :(得分:2)
我相信您需要通过索引-[0]
选择索引的第一个标量值:
df.loc[df.index >= first_value_of_year[0]]