2

I've created a dataframe in python using pandas. The index used is a series of timestamp of type int64. However, for time series analysis, the index need to be type dates. Can somebody help me to do the conversion ?


>>> import pandas as pd
>>> import time
>>> import statsmodels.api as sm
>>> df = pd.DataFrame(columns=['TCA', 'TCB', 'TCC'])
>>> df.loc[int(time.time() * 1000)] = [1, 2, 3]
>>> df.index
Int64Index([1453299087814], dtype='int64')
>>> arma_mod21 = sm.tsa.ARMA(df['TCA'], (2, 1)).fit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/statsmodels-0.6.1-py2.7-macosx-10.6-intel.egg/statsmodels/tsa/arima_model.py", line 445, in __init__
    super(ARMA, self).__init__(endog, exog, dates, freq, missing=missing)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/statsmodels-0.6.1-py2.7-macosx-10.6-intel.egg/statsmodels/tsa/base/tsa_model.py", line 42, in __init__
    self._init_dates(dates, freq)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/statsmodels-0.6.1-py2.7-macosx-10.6-intel.egg/statsmodels/tsa/base/tsa_model.py", line 51, in _init_dates
    raise ValueError("Given a pandas object and the index does "
ValueError: Given a pandas object and the index does not contain dates
1

1 Answer 1

4

use to_datetime with unit='ms' to convert to datetime:

In [185]:
pd.to_datetime(df.index, unit='ms')

Out[185]:
DatetimeIndex(['2016-01-20 14:16:51.703000'], dtype='datetime64[ns]', freq=None)

In [187]:
df.index = pd.to_datetime(df.index, unit='ms')
df.index

Out[187]:
DatetimeIndex(['2016-01-20 14:16:51.703000'], dtype='datetime64[ns]', freq=None)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.