pandas 如何把时间转成index_如何在pandas dataframe中将int索引转换为日期索引?(How to convert int index to dates index in pa...

这篇博客介绍了如何在Python的pandas库中,将DataFrame的Int64Index类型的时间戳转换为日期索引,以便进行时间序列分析。示例代码展示了创建一个包含整数时间戳的DataFrame,并尝试使用statsmodels进行ARMA分析时遇到的问题,由于索引不是日期类型导致的错误。解决方法涉及将int64时间戳转换为日期格式的index。
摘要由CSDN通过智能技术生成

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 "", line 1, in

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值