如何使用字符串访问Pandas DataFrame日期时间索引 [英] How to access pandas DataFrame datetime index using strings
问题描述
这是一个非常简单实用的问题.我觉得它必须是一个愚蠢的细节,并且应该有类似的问题.我找不到他们.如果有人这样做,我会很乐意将其删除.
我找到的最接近的是: 熊猫:使用loc遍历DataFrame索引 >
pandas正在研究[]
内部的内容并决定应该做什么.如果它是列名的子集,则将返回包含这些列的DataFrame.如果它是索引值的范围,它将返回这些行的子集.无法处理的是获取单个索引值.
解决方案
两个解决方法
1.将参数转换为pandas解释为范围的某种东西.
df['2008-01-01':'2008-01-01']
2.使用为您提供此结果的方法. loc[]
df.loc['2008-01-01']
链接到文档 >
This is a very simple and practical question. I have the feeling that it must be a silly detail and that there should be similar questions. I wasn't able to find them tho. If someone does I'll happily delete this one.
The closest I found were these: pandas: iterating over DataFrame index with loc
How to select rows within a pandas dataframe based on time only when index is date and time
anyway, the thing is, I have a datetime indexed panda dataframe as follows:
In[81]: y
Out[81]:
PETR4 CSNA3 VALE5
2008-01-01 0.0 0.0 0.0
2008-01-02 1.0 1.0 1.0
2008-01-03 7.0 7.0 7.0
In[82]: y.index
Out[82]: DatetimeIndex(['2008-01-01', '2008-01-02', '2008-01-03'], dtype='datetime64[ns]', freq=None)
Oddly enough, I can't access its values using none of the following methods:
In[83]: y[datetime.datetime(2008,1,1)]
In[84]: y['2008-1-1']
In[85]: y['1/1/2008']
I get the KeyError
error.
Even more weird is that the following methods DO work:
In[86]: y['2008']
Out[86]:
PETR4 CSNA3 VALE5
2008-01-01 0.0 0.0 0.0
2008-01-02 1.0 1.0 1.0
2008-01-03 7.0 7.0 7.0
In[87]: y['2008-1']
Out[87]:
PETR4 CSNA3 VALE5
2008-01-01 0.0 0.0 0.0
2008-01-02 1.0 1.0 1.0
2008-01-03 7.0 7.0 7.0
I'm fairly new to pandas so maybe I'm missing something here?
pandas is taking what's inside the []
and deciding what it should do. If it's a subset of column names, it'll return a DataFrame with those columns. If it's a range of index values, it'll return a subset of those rows. What is does not handle is taking a single index value.
Solution
Two work around's
1.Turn the argument into something pandas interprets as a range.
df['2008-01-01':'2008-01-01']
2.Use the method designed to give you this result. loc[]
df.loc['2008-01-01']
这篇关于如何使用字符串访问Pandas DataFrame日期时间索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!