【问题标题】:Numerical integration of a pandas dataframe indexed by datetime using resample.()使用 resample.() 按日期时间索引的 pandas 数据帧的数值积分
【发布时间】:2017-06-23 18:38:28
【问题描述】:

我想将 5 分钟的间隔整合为 1 小时。有没有办法在熊猫本身中做到这一点?类似于如何使用重新采样来缩小和平均?我想使用 5 分钟的数据缩小并返回每 1 小时的积分。

import pandas as pd
np.random.seed(1234)
df = pd.DataFrame(np.random.rand(300,4), columns=list('ABCD'), index=pd.date_range('2017-01-01 09:00:00', periods=300, freq='5min'))
df=df.resample('1h').mean()  #can as similar method be used to find the numerical integral (eg. with trapezoidal rule)?

【问题讨论】:

    标签: python pandas scipy


    【解决方案1】:

    您可以使用 scipy.integrate.simps 应用 simpson 规则进行集成。您必须使用自定义重采样器并在作为参数提供给自定义重采样器的函数中应用 Simpson 集成。

    你可以这样做:

    def custom_function(array_like):
        return  sp.integrate.simps(array_like)   
    
    df = df.resample('1H').apply(custom_function)
    

    【讨论】:

      【解决方案2】:

      我有一个类似的问题,只是使用了 scipy 集成 例如

      import pandas as pd
      import numpy as np
      from scipy import integrate
      
      data = np.abs(np.random.randn(36000))
      ts = pd.Series(data,pd.date_range(start='today', periods=len(data), freq='s'))
      ts_int = ts.resample('H').apply(integrate.trapz)
      

      【讨论】:

        猜你喜欢
        • 2018-12-18
        • 2015-03-22
        • 2020-10-09
        • 2012-12-04
        • 2020-07-29
        • 1970-01-01
        • 2019-10-17
        • 2020-09-13
        • 2017-01-16
        相关资源
        最近更新 更多