【问题标题】:pandas group by n seconds and apply arbitrary rolling functionpandas 按 n 秒分组并应用任意滚动功能
【发布时间】:2013-11-20 12:08:33
【问题描述】:

我有一些加速度计读数的csv数据,格式如下(不完全是这个,真实数据具有更高的采样率):

2013-09-28 17:36:50.322120, 0.152695, -0.545074, -0.852997 2013-09-28 17:36:50.622988, 0.141800, -0.554947, -0.867935 2013-09-28 17:36:51.923802, 0.132431, -0.547089, -0.879333 2013-09-28 17:36:52.124641, 0.124329, -0.530243, -0.887741 2013-09-28 17:36:52.425341, 0.122269, -0.519669, -0.900269 2013-09-28 17:36:52.926202, 0.122879, -0.502151, -0.902023 …… …… …… …… 2013-09-28 17:49:14.440343, 0.005447, -0.623016, -0.773529 2013-09-28 17:49:14.557806, 0.009048, -0.623093, -0.790909 2013-09-28 17:49:14.758442, 0.007217, -0.617386, -0.815796

我使用 pandas 加载它们

import pandas as pd
accDF=pd.read_csv(accFileName,header=0, sep=',') 
accDF.columns=['time','x','y','z']
accDF=accDF.set_index(['time'])

加速度计数据不是均匀采样的,我想每 10 或 20 或 30 秒对数据进行分组,并将自定义函数应用于数据组。

如果数据是均匀采样的,那么应用滚动函数会很容易。 但是,既然不是,我想使用时间戳间隔应用 groupby 。 以一秒的间隔这样做很容易:

accDF_win=accDF.groupby(accDF.index.second).apply... etc

但是,我无法弄清楚如何按任意秒数进行分组,然后对其应用函数。

使用 TimeGrouper,我可以做到以下几点:

accDF_win=accDF.groupby(pd.TimeGrouper(freq='3Min'))

任意分钟数,但似乎 TimeGrouper 没有“秒”分辨率。

提前感谢您的帮助

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    首先,您必须将 datetime-column 转换为 python-datetime 对象(以防万一)。

    >>> import pandas as pd
    >>> from dateutil import parser
    >>> df=pd.read_csv("test.csv",header=None,date_parser=True)
    #convert to datetime index, f.e. with dateutil
    >>> df=df.set_index(df[0].map(parser.parse)
    

    然后像这样使用pd.TimeGrouper:

    >>> df[3].groupby(pd.TimeGrouper('10S')).head()
    2013-09-28 17:36:40  2013-09-28 17:36:40.322120   -0.852997
                         2013-09-28 17:36:41.622988   -0.867935
                         2013-09-28 17:36:42.923802   -0.879333
                         2013-09-28 17:36:43.124641   -0.887741
                         2013-09-28 17:36:45.425341   -0.900269
    2013-09-28 17:36:50  2013-09-28 17:36:52.926202   -0.902023
                         2013-09-28 17:36:53.322120   -0.852997
                         2013-09-28 17:36:53.622988   -0.867935
                         2013-09-28 17:36:54.923802   -0.879333
                         2013-09-28 17:36:54.124641   -0.887741
    2013-09-28 17:49:50  2013-09-28 17:49:56.440343   -0.773529
                         2013-09-28 17:49:56.557806   -0.790909
                         2013-09-28 17:49:57.758442   -0.815796
    

    或者看看重采样函数here。也许您可以应用自定义重采样函数而不是使用 groupby 方法。

    df[3].resample("10S",how=lambda x: Whateveryouwanttodo)
    

    没有任何函数,它用NaN填充:

    >>> df[3].resample("10S")
    0
    2013-09-28 17:36:40   -0.877655
    2013-09-28 17:36:50   -0.884617
    2013-09-28 17:37:00         NaN
    2013-09-28 17:37:10         NaN
    2013-09-28 17:37:20         NaN
    2013-09-28 17:37:30         NaN
    2013-09-28 17:37:40         NaN
    

    【讨论】:

    • 请注意,TimeGrouper 在 pandas 0.21 中一直是 deprecated,支持 Grouper,带有参数 freq。
    【解决方案2】:

    我认为您不需要 TimeGrouper。支持按秒重采样。你是not the first person 尝试“S”几秒钟(所以也许熊猫应该支持它?);正确的字符串是's'。

    df = pd.read_csv(filename, parse_dates=True, sep=',', index_col=0, header=None)
    df.columns = ['x', 'y', 'z']
    df.resample('10s', how=f)  # where f is your function
    

    编辑:实际上,在我的版本(即将发布的 0.13)上,我发现“10S”也可以。也许您的整个问题都不是解析日期。

    【讨论】:

    • 其实他连“秒”都没试过;)
    • 看起来这在某个时候发生了变化;也许他有一个旧版本的熊猫,其中 S 和 Sec 不好。
    猜你喜欢
    • 2017-01-15
    • 1970-01-01
    • 2015-04-22
    • 2016-03-14
    • 2019-09-30
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-07-10
    相关资源
    最近更新 更多