使用对数均值重采样(或循环)

有没有一种方法可以使用对数均值重采样?我已经阅读了重采样文档,找不到用于对数均值重采样的任何选项。

我有一个带有日期时间索引的大数据框,每分钟都有观察值。我需要每隔5分钟计算一系列变量(列)的对数平均值。

我在下面提供了一些代码,这些代码显示了一些示例数据和我要执行的计算。可能是,如果没有“开箱即用”的对数均值重采样功能,我将需要编写一个循环代码来完成此操作...?

import numpy as np
import pandas as pd

df1 = pd.DataFrame({'db' : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 ]},index=pd.date_range('2019-05-02T00:00:00','2019-05-02T00:14:00',freq='1T'))

df1 = df1.resample('5T').mean() # <------ is there a way to do log mean for this?

# The calculation i am need to do is:

df2 = np.log10(10**((df1[observation minute 1]/10)) + 10**((df1[observation minute 2]/10)) + 10**((df1[observation minute 3]/10)) + 10**((df1[observation minute 4]/10)) + 10**((df1[observation minute 5]/10)))

# Where 'observation minute 1,5' are the 5 minutes i want to resample for.

# The resulting df i need is:

df_result = pd.DataFrame({'log_mean' : [np.log10(10**((1/10)) + 10**((2/10)) + 10**((3/10)) + 10**((4/10)) + 10**((5/10))),np.log10(10**((6/10)) + 10**((7/10)) + 10**((8/10)) + 10**((9/10)) + 10**((10/10))),np.log10(10**((11/10)) + 10**((12/10)) + 10**((13/10)) + 10**((14/10)) + 10**((15/10)))]},freq='5T'))

我们将不胜感激地收到任何指导。

li3029 回答:使用对数均值重采样(或循环)

结果证明,您可以使用apply使用所选的任何功能来重新采样:

df1 = df1.resample('5T').apply(lambda spl: 10*np.log10(np.mean(np.power(10,spl/10))))

或者您可以单独定义

def log_avg(spl_arraylike):
  return 10*np.log10(np.mean(np.power(10,spl_arraylike/10))))

df1 = df1.resample('5T').apply(log_avg)

这将返回具有以下值的数据框
2019-05-02 00:00:00 3.227668
2019-05-02 00:05:00 8.227668
2019-05-02 00:10:00 13.227668

本文链接:https://www.f2er.com/2636605.html

大家都在问