在pandas中,如何从时间戳索引中获取行?

时间:2017-04-12 16:00:54

标签: python pandas dataframe

我正在尝试使用quantopian。我真是太沮丧了。

我有这个:

import pandas as pd
import numpy as np
spy_minute_opens = get_pricing(
    'SPY', fields='open_price',
    start_date='2005-01-01', end_date = '2017-04-01', 
    frequency='minute')
spy_minute_opens.index.tz = 'US/Eastern'
spy_minute_opens = spy_minute_opens.to_frame()
spy_5min = spy_minute_opens.groupby(pd.TimeGrouper('5T')).agg(['first'])
spy_5min.columns = ['SPY']

这会产生以下结果:

spy_5min.head(5)
                               SPY
2005-01-03 09:30:00-05:00   95.507
2005-01-03 09:35:00-05:00   95.531
2005-01-03 09:40:00-05:00   95.625
2005-01-03 09:45:00-05:00   95.547
2005-01-03 09:50:00-05:00   95.586

我正在尝试获取最小值的行。我得到了一个keyError。

spy_5min.idxmin()

SPY   2009-03-06 15:10:00-05:00
dtype: datetime64[ns, US/Eastern]

spy_5min[spy_5min.idxmin()]

KeyError: "['2009-03-06T20:10:00.000000000'] not in index"

任何帮助?!?!

1 个答案:

答案 0 :(得分:2)

您在数据框上调用idxmin,而该数据框会返回一系列而不是索引值,而是在SPY列上调用它:

spy_5min.idxmin()
#SPY    2005-01-03 09:30:00-05:00
#dtype: object

spy_5min.SPY.idxmin()
#'2005-01-03 09:30:00-05:00'

同样在@MaxU评论时,使用loc提取具有特定索引的行:

spy_5min.loc[spy_5min.SPY.idxmin()]
#SPY    95.507
#Name: 2005-01-03 09:30:00-05:00, dtype: float64