从Pandas中的resample获取索引

时间:2016-05-10 20:24:37

标签: python pandas time-series resampling

我在Python中有一个时间序列数据帧,每隔一个频率。我正在尝试聚合数据,以便在每分钟获得Speed的最大值。我正在使用此代码:

df = pd.DataFrame({ 'Speed' : [],
                  'Acceleration' : []
            })
rng = pd.date_range('1/1/2011', periods=72, freq='s')
df['Speed'] = np.random.randn(len(rng))
df['Acceleration'] = np.random.randn(len(rng))
df = df.set_index(rng)
df['Acceleration'].resample("1Min").max()

但是,我有另一列Speed,我有兴趣在每分钟将其相关值设为最大Acceleration。例如,假设Acceleration的最高13:15发生在13:15:10,它是1.2 m / s ^ 2。在同一秒,速度为5 m / s。除了最大加速度之外,我还想获得这个速度。感谢。

2 个答案:

答案 0 :(得分:3)

使用您自己的示例

尝试此操作
In [17]: idx = df.resample('1Min').agg({'Acceleration': np.argmax})
In [18]: df.ix[idx.Acceleration]
Out[18]:
                     Acceleration     Speed
2011-01-01 00:00:06      2.754047 -0.274572
2011-01-01 00:01:06      3.258652 -1.302055

答案 1 :(得分:1)

解决方案

import datetime as dt
import numpy as np
import pandas as pd

np.random.seed([3, 1415])

seconds = pd.date_range('2016-03-01', '2016-03-02', freq='S')

data = pd.Series(np.random.rand(len(seconds)), index=seconds)

def minute(x):
    """Returns a datetime object with seconds stripped off"""
    fmt = '%Y%m%d%H%M'
    return dt.datetime.strptime(x.strftime(fmt), fmt)

data.groupby(minute).agg({'max': np.max, 'argmax': np.argmax})

您必须将此应用于您自己的情况。

相关问题