如何寻找跨越微时间单位的时间序列的周期性?

2024-04-25 04:43:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个时间序列,时间单位是毫秒 大约有3000个条目。我想找出季节性 例如检测数据中的任何周期性故障。数据是 表示为数据帧。你知道吗

我尝试使用statsmodel中的季节分解()方法,如下所示:

import pandas as pd
data = pd.read_csv('Sample_data.csv',index_col=0)
data.index = pd.to_datetime(data.index)
print(data.head())
##                              Sample_values
## Dates    
## 1970-01-01 05:30:00.000000   0.466812
## 1970-01-01 05:30:00.016667   0.218692
## 1970-01-01 05:30:00.033333   0.938067
## 1970-01-01 05:30:00.050000   0.480025
## 1970-01-01 05:30:00.066667   0.915175
print(type(data))
##  <class 'pandas.core.frame.DataFrame'>
from statsmodels.tsa.seasonal import seasonal_decompose
result = seasonal_decompose(data, model='additive')
fig = result.plot()

但会导致以下错误:

ValueError: You must specify a freq or x must be a pandas object with a timeseries index with a freq not set to None

但是,如果我对x轴单位为月的数据集使用相同的代码(例如从https://www.analyticsvidhya.com/wp-content/uploads/2016/02/AirPassengers.csv下载),那么我不会得到任何错误,我会像预期的那样从^{u decompose()得到4个图。你知道吗

那么,我怎样才能检测出我的这类数据的季节性模式呢 几个小时?你知道吗


Tags: csvto数据sampleimportpandasdataindex
1条回答
网友
1楼 · 发布于 2024-04-25 04:43:31

您需要为datetime索引定义一个freq。你知道吗

使用以下方法:

                            sample_values
1970-01-01 05:30:00.000000       0.466812
1970-01-01 05:30:00.016667       0.218692
1970-01-01 05:30:00.033333       0.938067
1970-01-01 05:30:00.050000       0.480025
1970-01-01 05:30:00.066667       0.915175

当前频率为None

In [1]: print df.index
DatetimeIndex([       '1970-01-01 05:30:00', '1970-01-01 05:30:00.016667',
               '1970-01-01 05:30:00.033333', '1970-01-01 05:30:00.050000',
               '1970-01-01 05:30:00.066667'],
              dtype='datetime64[ns]', freq=None)

而Pandasinfer_freq函数无法检测到它:

In [2]: print pd.infer_freq(df.index)
None

如果你知道数据的频率应该是多少,你可以重新索引到这个频率。但是,这对于索引来说很困难,因为时间戳中的重复小数需要四舍五入到某个定义的间隔。这很接近:

In [3]: df_freq = df.resample('.000001S').ffill().reindex(pd.date_range(df.index[0],df.index[-1],freq='0.016667S'))

In [4]: print df_freq
                            sample_values
1970-01-01 05:30:00.000000       0.466812
1970-01-01 05:30:00.016667       0.218692
1970-01-01 05:30:00.033334       0.938067
1970-01-01 05:30:00.050001       0.480025

In [5]: print df_freq.index
DatetimeIndex([       '1970-01-01 05:30:00', '1970-01-01 05:30:00.016667',
               '1970-01-01 05:30:00.033334', '1970-01-01 05:30:00.050001'],
              dtype='datetime64[ns]', freq='16667U')

现在您已经定义了一个freq。在完整的数据集上试试,看看seasonal_decompose()是否会运行。但是,时间戳可能会随着时间的推移变得不准确。你知道吗

你也可以这样做:

In [6]: df_freq = df.resample('.000001S').interpolate().resample('.005S').first()

In [7]: print df_freq
                         sample_values
1970-01-01 05:30:00.000       0.466812
1970-01-01 05:30:00.005       0.392377
1970-01-01 05:30:00.010       0.317943
1970-01-01 05:30:00.015       0.243508
1970-01-01 05:30:00.020       0.362558
1970-01-01 05:30:00.025       0.578380
1970-01-01 05:30:00.030       0.794201
1970-01-01 05:30:00.035       0.892255
1970-01-01 05:30:00.040       0.754845
1970-01-01 05:30:00.045       0.617435
1970-01-01 05:30:00.050       0.480025
1970-01-01 05:30:00.055       0.610567
1970-01-01 05:30:00.060       0.741110
1970-01-01 05:30:00.065       0.871652

它具有freq='5L',并使用线性插值来用常规频率索引近似原始数据的趋势。你可以用.005S进行第二次下采样频率的实验,以获得更高或更低的频率。你知道吗

相关问题 更多 >