python 时间序列突变检测_Python Pandas:检测时间序列的频率

该博客介绍了如何使用Python的Pandas库来检测时间序列数据的频率,并通过找到的频率重建完整的日期时间范围。通过分析数据间隔的分布,确定最频繁出现的间隔作为时间序列的频率,然后利用这个频率生成完整的时间范围。
摘要由CSDN通过智能技术生成

也许尝试区分时间索引并使用模式(或最小差异)作为频率.

import pandas as pd

import numpy as np

# simulate some data

# ===================================

np.random.seed(0)

dt_rng = pd.date_range('2015-03-02 00:00:00', '2015-07-19 23:00:00', freq='H')

dt_idx = pd.DatetimeIndex(np.random.choice(dt_rng, size=2000, replace=False))

df = pd.DataFrame(np.random.randn(2000), index=dt_idx, columns=['col']).sort_index()

df

col

2015-03-02 01:00:00 2.0261

2015-03-02 04:00:00 1.3325

2015-03-02 05:00:00 -0.9867

2015-03-02 06:00:00 -0.0671

2015-03-02 08:00:00 -1.1131

2015-03-02 09:00:00 0.0494

2015-03-02 10:00:00 -0.8130

2015-03-02 11:00:00 1.8453

... ...

2015-07-19 13:00:00 -0.4228

2015-07-19 14:00:00 1.1962

2015-07-19 15:00:00 1.1430

2015-07-19 16:00:00 -1.0080

2015-07-19 18:00:00 0.4009

2015-07-19 19:00:00 -1.8434

2015-07-19 20:00:00 0.5049

2015-07-19 23:00:00 -0.5349

[2000 rows x 1 columns]

# processing

# ==================================

# the gap distribution

res = (pd.Series(df.index[1:]) - pd.Series(df.index[:-1])).value_counts()

01:00:00 1181

02:00:00 499

03:00:00 180

04:00:00 93

05:00:00 24

06:00:00 10

07:00:00 9

08:00:00 3

dtype: int64

# the mode can be considered as frequency

res.index[0] # output: Timedelta('0 days 01:00:00')

# or maybe the smallest difference

res.index.min() # output: Timedelta('0 days 01:00:00')

# get full datetime rng

full_rng = pd.date_range(df.index[0], df.index[-1], freq=res.index[0])

full_rng

DatetimeIndex(['2015-03-02 01:00:00', '2015-03-02 02:00:00',

'2015-03-02 03:00:00', '2015-03-02 04:00:00',

'2015-03-02 05:00:00', '2015-03-02 06:00:00',

'2015-03-02 07:00:00', '2015-03-02 08:00:00',

'2015-03-02 09:00:00', '2015-03-02 10:00:00',

...

'2015-07-19 14:00:00', '2015-07-19 15:00:00',

'2015-07-19 16:00:00', '2015-07-19 17:00:00',

'2015-07-19 18:00:00', '2015-07-19 19:00:00',

'2015-07-19 20:00:00', '2015-07-19 21:00:00',

'2015-07-19 22:00:00', '2015-07-19 23:00:00'],

dtype='datetime64[ns]', length=3359, freq='H', tz=None)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值