首页 > 解决方案 > 使用对数平均值重新采样(或循环)

问题描述

有没有办法使用对数平均值重新采样?我已阅读重采样文档,但找不到对数均值重采样的任何选项。

我有一个带有日期时间索引的大型数据框,每分钟都有观察结果。我需要为一系列变量(列)计算每 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,2,3,4,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)))]}, index=pd.date_range('2019-05-02T00:00:00', '2019-05-02T00:14:00', freq='5T'))

任何指导将不胜感激。

标签: python-3.xloopstime-seriesresampling

解决方案


事实证明,您可以使用您选择的任何功能重新采样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


推荐阅读