【问题标题】:How to detect and filter peaks over time series data?如何检测和过滤时间序列数据中的峰值?
【发布时间】:2018-06-25 15:50:53
【问题描述】:

我有一个这样的用户登录数据框:

    id     datetime_login 
    646  2017-03-15 15:30:25
    611  2017-04-14 11:38:30
    611  2017-05-15 08:49:01
    651  2017-03-15 15:30:25
    611  2017-03-15 15:30:25
    652  2017-03-08 14:03:56
    652  2017-03-08 14:03:56
    652  2017-03-15 15:30:25
    654  2017-03-15 15:30:25
    649  2017-03-15 15:30:25
    902  2017-09-09 15:00:00
    902  2017-02-13 16:39:53
    902  2017-11-15 12:00:00
    902  2017-11-15 12:00:00
    902  2017-09-09 15:00:00
    902  2017-05-15 08:48:47
    902  2017-11-15 12:00:00

绘制登录后:

df.datetime_login = df.datetime_login.apply(lambda x: str(x)[:10])
df.datetime_login = df.datetime_login.apply(lambda x: date(int(x[:4]), int(x[5:7]), int(x[8:10])))


fig, ax = subplots()
df.datetime_login.value_counts().sort_index().plot(figsize=(25,10), colormap='jet',fontsize=20)
  1. 如何在我的图中检测时间序列数据中的峰值?

  2. 如何将时间序列数据中的峰值过滤到数组中?

我尝试过:

import peakutils
indices = peakutils.indexes(df, thres=0.4, min_dist=1000)
print(indices) 

但是,我得到了:

TypeError: unsupported operand type(s) for -: 'datetime.date' and 'int'

但是,我得到了:

【问题讨论】:

    标签: python python-3.x pandas time-series


    【解决方案1】:

    在哪里 df.datetime_login.value_counts().sort_index().plot(figsize=(25,10), colormap='jet',fontsize=20)地块:

    我们试试下面的,你需要使用value_counts返回的系列而不是你原来的df,peakutils.indexes

    df_counts = df.datetime_login.value_counts().sort_index()
    df_counts[peakutils.indexes(df_counts, thres=0.4, min_dist=1000)]
    

    输出:

    2017-03-15 15:30:25    6
    Name: datetime_login, dtype: int64
    

    【讨论】:

    • 感谢您的帮助,但它只是给我返回了一个峰...有没有办法返回所有的峰?
    猜你喜欢
    • 2020-06-16
    • 2012-08-28
    • 2020-09-26
    • 2020-10-08
    • 2023-03-06
    • 2020-04-20
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多