How to convert a pandas DataFrame into a TimeSeries? How to convert a pandas DataFrame into a TimeSeries? pandas pandas

How to convert a pandas DataFrame into a TimeSeries?


I know this is late to the game here but a few points.

Whether or not a DataFrame is considered a TimeSeries is the type of index. In your case, your index is already a TimeSeries, so you are good to go. For more information on all the cool slicing you can do with a the pd.timeseries index, take a look at http://pandas.pydata.org/pandas-docs/stable/timeseries.html#datetime-indexing

Now, others might arrive here because they have a column 'DateTime' that they want to make an index, in which case the answer is simple

ts = df.set_index('DateTime')


Here is one possibility

[3]: dfOut[3]:                    A         B         C         D2013-01-01 -0.024362  0.712035 -0.913923  0.7552762013-01-02  2.624298  0.285546  0.142265 -0.0478712013-01-03  1.315157 -0.333630  0.398759 -1.0348592013-01-04  0.713141 -0.109539  0.263706 -0.5880482013-01-05 -1.172163 -1.387645 -0.171854 -0.4586602013-01-06 -0.192586  0.480023 -0.530907 -0.872709In [4]: df.unstack()Out[4]: A  2013-01-01   -0.024362   2013-01-02    2.624298   2013-01-03    1.315157   2013-01-04    0.713141   2013-01-05   -1.172163   2013-01-06   -0.192586B  2013-01-01    0.712035   2013-01-02    0.285546   2013-01-03   -0.333630   2013-01-04   -0.109539   2013-01-05   -1.387645   2013-01-06    0.480023C  2013-01-01   -0.913923   2013-01-02    0.142265   2013-01-03    0.398759   2013-01-04    0.263706   2013-01-05   -0.171854   2013-01-06   -0.530907D  2013-01-01    0.755276   2013-01-02   -0.047871   2013-01-03   -1.034859   2013-01-04   -0.588048   2013-01-05   -0.458660   2013-01-06   -0.872709dtype: float64