1

I need to run a time-series model using StatsModels, and it requires my indices to be dates. However, currently my dates are all in string form. Is there any quick way for me to convert the dates to the format satisfied by statsmodel timeseries models?

My date string is currently like the following:

    1/8/2015
    1/15/2015
    1/22/2015
    1/29/2015
    2/5/2015
CobbDG
  • 19
  • 3

2 Answers2

1

I've found a way to solve it by using the following code:

    df.index = pd.to_datetime(df.index, format='%m/%d/%Y', errors='ignore')

After this, i'm able to run the time-series modules under StatsModels.

CobbDG
  • 19
  • 3
0

You can use the datetime module to convert those dates:

Code:

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))

Test Data:

my_dates = """
    1/8/2015
    1/15/2015
    1/22/2015
    1/29/2015
    2/5/2015
""".split('\n')[1:-1]
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135