Fitting ARMA model to time series indexed by time in python(将 ARMA 模型拟合到 Python 中按时间索引的时间序列)
问题描述
我正在尝试将 ARMA 模型拟合到存储在 Pandas 数据帧中的时间序列.数据框有一列名为val"的 numpy.float64 类型的值和一个熊猫时间戳索引.时间戳采用年-月-日小时:分钟:秒"格式.我了解以下代码:
I am trying to fit an ARMA model to a time series stored in a pandas dataframe. The dataframe has one column of values of type numpy.float64 named "val" and an index of pandas timestamps. The timestamps are in the "Year-Month-Day Hour:Minute:Second" format. I understand that the following code:
from statsmodels.tsa.arima_model import ARMA
model = ARMA(df["val"], (1,0))
给我错误信息:
ValueError: Given a pandas object and the index does not contain dates
因为我没有正确格式化时间戳.如何索引我的数据帧,以便 ARMA 方法在保留我的日期和时间信息的同时接受它?
because I have not formatted the timestamps correctly. How can I index my dataframe so that the ARMA method accepts it while retaining my date and time information?
推荐答案
我认为您需要将 index 转换为 DatetimeIndex:
I think you need convert index to DatetimeIndex:
df.index = pd.DatetimeIndex(df.index)
示例:
import pandas as pd
from statsmodels.tsa.arima_model import ARMA
df=pd.DataFrame({"val": pd.Series([1.1,1.7,8.4 ], 
                 index=['2015-01-15 12:10:23','2015-02-15 12:10:23','2015-03-15 12:10:23'])})
print df
                     val
2015-01-15 12:10:23  1.1
2015-02-15 12:10:23  1.7
2015-03-15 12:10:23  8.4
print df.index
Index([u'2015-01-15 12:10:23',u'2015-02-15 12:10:23',u'2015-03-15 12:10:23'], dtype='object')
df.index = pd.DatetimeIndex(df.index)
print df.index
DatetimeIndex(['2015-01-15 12:10:23', '2015-02-15 12:10:23',
               '2015-03-15 12:10:23'],
              dtype='datetime64[ns]', freq=None)
model = ARMA(df["val"], (1,0))
print model
<statsmodels.tsa.arima_model.ARMA object at 0x000000000D5247B8>
                        这篇关于将 ARMA 模型拟合到 Python 中按时间索引的时间序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将 ARMA 模型拟合到 Python 中按时间索引的时间序列
				
        
 
            
        基础教程推荐
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
 - 在Python中从Azure BLOB存储中读取文件 2022-01-01
 - 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
 - PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
 - 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
 - 包装空间模型 2022-01-01
 - 求两个直方图的卷积 2022-01-01
 - 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
 - Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
 - 修改列表中的数据帧不起作用 2022-01-01
 
    	
    	
    	
    	
    	
    	
    	
    	
				
				
				
				