简体   繁体   English

Python:使用Datetime索引在熊猫中建立索引

[英]Python: Indexing in Pandas with Datetime indices

I've got problems regarding to indexing in Pandas and hope you can help me: 我在熊猫索引方面遇到了问题,希望您能为我提供帮助:

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

returns the first value of the year as DatetimeIndex: 返回年份的第一个值作为DatetimeIndex:

DatetimeIndex(['2016-01-01'], dtype='datetime64[ns]', freq='H')

When I call the dataframe with this index, everything seems to be working fine: 当我使用该索引调用数据帧时,一切似乎都工作正常:

df.loc[first_value_of_year]

returns 回报

+------------------------+-----------+
|                        |  Val      |
+------------------------+-----------+
| 2016-01-01             | 0.144044  |

So, everything is OK up to here! 因此,到目前为止一切正常! But if I want to get all values greater than this value, it doesn't work anymore: 但是,如果我想获得所有大于此值的值,它将不再起作用:

df.loc[df.index >= first_value_of_year]

and gives ValueError (lenghts must match): 并给出ValueError(长度必须匹配):

but if I take the same command with the timestamp itself as string it does what it should do 但是如果我将时间戳本身作为字符串使用相同的命令,它将执行应做的事情

df.loc[df.index >= '2016-01-01 00:00:00']

returns 回报

+------------------------+-----------+
|                        |  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  |

so, what am I missing here? 所以,我在这里想念什么? How do I correctly access all values greater than the DatetimeIndex variable? 如何正确访问所有大于DatetimeIndex变量的值?

Thanks! 谢谢!

我相信您需要通过索引- [0]索引的第一个值选择为标量:

df.loc[df.index >= first_value_of_year[0]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM