pandas 无法按熊猫数据框中的时间戳编制索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28518944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 22:57:00  来源:igfitidea点击:

Can't index by timestamp in pandas dataframe

pythonexcelpandasdataframeindexing

提问by Delta_Fore

I took an excel sheet which has dates and some values and want to convert them to pandas dataframe and select only rows which are between certain dates.

我拿了一个包含日期和一些值的 excel 表,并希望将它们转换为 Pandas 数据框并只选择特定日期之间的行。

For some reason I cannot select a row by date index

由于某种原因,我无法按日期索引选择一行

Raw Data in Excel file

Excel 文件中的原始数据

MCU                         
Timestamp   50D 10P1    10P2    10P3    10P6    10P9    10P12
12-Feb-15   25.17   5.88    5.92    5.98    6.18    6.23    6.33
11-Feb-15   25.9    6.05    6.09    6.15    6.28    6.31    6.39
10-Feb-15   26.38   5.94    6.05    6.15    6.33    6.39    6.46

Code

代码

xls = pd.ExcelFile('e:/Data.xlsx')
vols = xls.parse(asset.upper()+'VOL',header=1)
vols.set_index('Timestamp',inplace=True)

Data before set_index

set_index 之前的数据

      Timestamp    50D  10P1  10P2  10P3  10P6  10P9  10P12  25P1  25P2  \
0    2015-02-12  25.17  5.88  5.92  5.98  6.18  6.23   6.33  2.98  3.08   
1    2015-02-11  25.90  6.05  6.09  6.15  6.28  6.31   6.39  3.12  3.17   
2    2015-02-10  26.38  5.94  6.05  6.15  6.33  6.39   6.46  3.01  3.16  

Data after set_index

set_index 后的数据

              50D  10P1  10P2  10P3  10P6  10P9  10P12  25P1  25P2  25P3  \
Timestamp                                                                  
2015-02-12  25.17  5.88  5.92  5.98  6.18  6.23   6.33  2.98  3.08  3.21   
2015-02-11  25.90  6.05  6.09  6.15  6.28  6.31   6.39  3.12  3.17  3.32   
2015-02-10  26.38  5.94  6.05  6.15  6.33  6.39   6.46  3.01  3.16  3.31  

Output

输出

>>> vols.index
<class 'pandas.tseries.index.DatetimeIndex'>
[2015-02-12, ..., NaT]
Length: 1478, Freq: None, Timezone: None

>>> vols[date(2015,2,12)]
*** KeyError: datetime.date(2015, 2, 12)

I would expect this not to fail, and also I should be able to select a range of dates. Tried so many combinations but not getting it.

我希望这不会失败,而且我应该能够选择一个日期范围。尝试了这么多组合,但没有得到它。

采纳答案by Alex Riley

Using a datetime.dateinstance to try to retrieve the index won't work, you just need a string representation of the date, e.g. '2015-02-12'or '2015/02/14'.

使用datetime.date实例来尝试检索索引是行不通的,您只需要日期的字符串表示形式,例如'2015-02-12''2015/02/14'

Secondly, vols[date(2015,2,12)]is actually looking in your DataFrame's column headings, not the index. You can use locto fetch row index labels instead. For example you could write vols.loc['2015-02-12']

其次,vols[date(2015,2,12)]实际上是查看 DataFrame 的列标题,而不是索引。您可以使用loc来获取行索引标签。例如你可以写vols.loc['2015-02-12']