【问题标题】:use time series data in python to calculate mean, variance std deviation在python中使用时间序列数据计算均值、方差std偏差
【发布时间】:2016-11-17 18:27:07
【问题描述】:

我从传感器收集的数据如下:

sec   nanosec value 

1001   1       0.2 

1001   2       0.2

1001   3       0.2 

1002   1       0.1  

1002   2       0.2   

1002   3       0.1 

1003   1       0.2 

1003   2       0.2

1003   3       0.1  

1004   1       0.2   

1004   2       0.2 

1004   3       0.2 

1004   4      0.1 

我想计算average,std deviation 和其他一些统计数据,例如每 2 秒一列的最大值、最小值。 所以平均 (1001, 1002)= 0.167,平均 (1003,1004)=0.17

从教程http://earthpy.org/pandas-basics.html,我认为我应该将其转换为时间序列并使用 pandas 的滚动 _means,但我是时间序列数据的新手,所以我不确定这是否是正确的方法。 另外,我如何在此处指定转换频率,因为第一秒的观察结果较少。因此,对于实际数据,我在 1001 秒内有不到 100 个读数,然后在 1002 秒后有 100 个观察值。

我也可以按秒进行简单的 groupby,但它会每秒而不是每 2 秒对读数进行分组,那么我如何将 groupby 中 2 个连续组的观察结果结合起来,然后进行分析。

【问题讨论】:

    标签: python pandas time-series


    【解决方案1】:

    我认为您可以先将列secto_timedeltaset_indexresample 转换为2 seconds (2S):

    df['sec'] = pd.to_timedelta(df.sec, unit='s')
    df.set_index('sec', inplace=True)
    print (df)
              nanosec  value
    sec                     
    00:16:41        1    0.2
    00:16:41        2    0.2
    00:16:41        3    0.2
    00:16:42        1    0.1
    00:16:42        2    0.2
    00:16:42        3    0.1
    00:16:43        1    0.2
    00:16:43        2    0.2
    00:16:43        3    0.1
    00:16:44        1    0.2
    00:16:44        2    0.2
    00:16:44        3    0.2
    00:16:44        4    0.1
    
    print (df.value.resample('2S').mean())
    sec
    00:16:41    0.166667
    00:16:43    0.171429
    00:16:45         NaN
    Freq: 2S, Name: value, dtype: float64
    
    print (df.value.resample('2S').std())
    sec
    00:16:41    0.051640
    00:16:43    0.048795
    00:16:45         NaN
    Freq: 2S, Name: value, dtype: float64
    
    print (df.value.resample('2S').max())
    sec
    00:16:41    0.2
    00:16:43    0.2
    00:16:45    NaN
    Freq: 2S, Name: value, dtype: float64
    

    也许您需要将base 更改为resample

    print (df.value.resample('2S', base=1).mean())
    sec
    00:16:42    0.166667
    00:16:44    0.171429
    00:16:46         NaN
    Freq: 2S, Name: value, dtype: float64
    
    print (df.value.resample('2S', base=1).std())
    sec
    00:16:42    0.051640
    00:16:44    0.048795
    00:16:46         NaN
    Freq: 2S, Name: value, dtype: float64
    
    print (df.value.resample('2S', base=1).max())
    sec
    00:16:42    0.2
    00:16:44    0.2
    00:16:46    NaN
    Freq: 2S, Name: value, dtype: float64
    
    print (df.value.resample('2S', base=2).mean())
    sec
    00:16:43    0.166667
    00:16:45    0.171429
    00:16:47         NaN
    Freq: 2S, Name: value, dtype: float64
    
    print (df.value.resample('2S', base=2).std())
    sec
    00:16:43    0.051640
    00:16:45    0.048795
    00:16:47         NaN
    Freq: 2S, Name: value, dtype: float64
    
    print (df.value.resample('2S', base=2).max())
    sec
    00:16:43    0.2
    00:16:45    0.2
    00:16:47    NaN
    Freq: 2S, Name: value, dtype: float64
    

    【讨论】:

    • 我认为这可行,但我收到警告尝试使用 .loc[row_index,col_indexer] = value 代替 df1['header_stamp_secs'] = pd.to_timedelta(df1.header_stamp_secs, unit='s' )。后面跟着一个错误仅对 DatetimeIndex 或 PeriodIndex 有效
    • 有趣。你的熊猫是什么版本的?
    • 我使用的是熊猫 0.13.1
    • 嗯,最新版本是0.18.1,我想你可以升级pandas
    【解决方案2】:

    借用 jezrael 的代码进行设置:

    df['sec'] = pd.to_timedelta(df.sec, unit='s')
    df.set_index('sec', inplace=True)
    print (df)
              nanosec  value
    sec                     
    00:16:41        1    0.2
    00:16:41        2    0.2
    00:16:41        3    0.2
    00:16:42        1    0.1
    00:16:42        2    0.2
    00:16:42        3    0.1
    00:16:43        1    0.2
    00:16:43        2    0.2
    00:16:43        3    0.1
    00:16:44        1    0.2
    00:16:44        2    0.2
    00:16:44        3    0.2
    00:16:44        4    0.1
    

    使用pd.TimeGrouper('2S')describe()

    df.groupby(pd.TimeGrouper('2S')).describe()
    

    【讨论】:

      猜你喜欢
      • 2022-01-06
      • 2020-05-13
      • 1970-01-01
      • 2022-06-27
      • 2020-09-20
      • 2020-07-27
      • 1970-01-01
      • 2017-09-26
      • 1970-01-01
      相关资源
      最近更新 更多