2017-04-21 92 views
1
def dataset(): 
    with sqlite3.connect("project.db") as db: 
      cursor1=db.cursor() 
      d1= "select date1,delta from Payment" 
      cursor1.execute(d1) 
      df=DataFram(data=cursor1.fetchall(),columns=['date','delta']) 
      print df.head() 
      print '\n Data Types' 
      print df.dtypes 
      print df.index 
      df.info() 

如何使此数据帧成为时间序列? 谢谢大家。如何将数据帧转换为时间序列

enter image description here

+0

时间序列意味着索引是'DatetimeIndex',那么当前的索引是什么? 'df.info()'显示什么? – EdChum

+0

我编辑了这个问题:-) –

+0

你需要做'df.index = pd.to_datetime(df ['date'])'将你的索引设置为'datetimeindex' – EdChum

回答

1

的时间序列意味着指数必须DatetimeIndex,从链路判断你的形象,你需要转换后设置索引:

df.index = pd.to_datetime(df['date']) 
+0

,然后输入(df)它仍然会打印pandas.core.frame.DataFrame。我相信我们正在寻找的是时间序列对象,对吧?据McKinney说,Wes。用于数据分析的Python:用Pandas,NumPy和IPython修改数据(Kindle Location 6861)。 O'Reilly媒体。 Kindle版。 ...当我们运行'df.index'时,它应该打印:'pandas.tseries.index.DatetimeIndex' –

+0

@RyanChase我的笔记本上没有最新版本,首先'df'的类型应该保持为一个'pandas.DataFrame'我的机器上的索引更改为'pandas.tseries.index.DatetimeIndex',因为我无法访问您的数据和代码我无法进一步评论为什么它不适用于您 – EdChum

1

能否请您出示的数据的一个具体的例子吗?

只要索引是格式为pandas.datetime的日期,您的数据帧就可以是时间序列。 要做到这一点,你可以输入:

df.index = pandas.datetime.strptime("date_field") 
相关问题