Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 运行时间序列模型时,将索引更改为日期_Python_Time Series_Statsmodels - Fatal编程技术网

Python 运行时间序列模型时,将索引更改为日期

Python 运行时间序列模型时,将索引更改为日期,python,time-series,statsmodels,Python,Time Series,Statsmodels,我需要使用StatsModels运行一个时间序列模型,它要求我的索引是日期。但是,目前我的日期都是字符串形式。是否有任何快速的方法可以将日期转换为statsmodel timeseries模型所满足的格式 我的日期字符串当前如下所示: 1/8/2015 1/15/2015 1/22/2015 1/29/2015 2/5/2015 您可以使用datetime模块转换这些日期: 代码: import datetime as dt def make_date

我需要使用StatsModels运行一个时间序列模型,它要求我的索引是日期。但是,目前我的日期都是字符串形式。是否有任何快速的方法可以将日期转换为statsmodel timeseries模型所满足的格式

我的日期字符串当前如下所示:

    1/8/2015
    1/15/2015
    1/22/2015
    1/29/2015
    2/5/2015

您可以使用
datetime
模块转换这些日期:

代码:

import datetime as dt

def make_date(date_string):
    m, d, y = tuple(int(x) for x in my_date.split('/'))
    return dt.date(year=y, month=m, day=d)

for my_date in my_dates:
    print(make_date(my_date))
my_dates = """
    1/8/2015
    1/15/2015
    1/22/2015
    1/29/2015
    2/5/2015
""".split('\n')[1:-1]
测试数据:

import datetime as dt

def make_date(date_string):
    m, d, y = tuple(int(x) for x in my_date.split('/'))
    return dt.date(year=y, month=m, day=d)

for my_date in my_dates:
    print(make_date(my_date))
my_dates = """
    1/8/2015
    1/15/2015
    1/22/2015
    1/29/2015
    2/5/2015
""".split('\n')[1:-1]

我已经找到了一种解决方法,使用以下代码:

    df.index = pd.to_datetime(df.index, format='%m/%d/%Y', errors='ignore')
在此之后,我可以在StatsModels下运行时间序列模块