python - 如何检测和过滤时间序列数据的峰值?
问题描述
我有一个用户登录的熊猫数据框,如下所示:
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)
如何在我的图中检测时间序列数据中的峰值?
如何将时间序列数据中的峰值过滤到数组中?
我试过了:
import peakutils
indices = peakutils.indexes(df, thres=0.4, min_dist=1000)
print(indices)
但是,我得到了:
TypeError: unsupported operand type(s) for -: 'datetime.date' and 'int'
但是,我得到了:
解决方案
在哪里
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
推荐阅读
- sas - 如何将表从 SAS 移动到 Teradata SQL Assistant?
- python - 得到一个错误的时间戳,但它的 ASCII 格式是正确的
- swift - 摆脱按钮的标准 onTouch 动画
- css - 如何将 css 样式元素添加到 Rmd 闪亮的 renderImage?
- sql - 使用临时表时 SQL COUNT 查询返回 NULL
- python - 即使在添加 export PYTHONDONTWRITEBYTECODE=1 后仍然得到 __pycache__ 文件夹
- r - psidR:build.panel() 返回重复错误
- three.js - 着色器材质中的多个纹理
- c# - 快速排序实现的堆栈溢出
- pipeline - 从 numexpr 导入评估 Quantopian