32

I am looking for a way to convert a DataFrame to a TimeSeries without splitting the index and value columns. Any ideas? Thanks.

In [20]: import pandas as pd

In [21]: import numpy as np

In [22]: dates = pd.date_range('20130101',periods=6)

In [23]: df = pd.DataFrame(np.random.randn(6,4),index=dates,columns=list('ABCD'))

In [24]: df
Out[24]:
                   A         B         C         D
2013-01-01 -0.119230  1.892838  0.843414 -0.482739
2013-01-02  1.204884 -0.942299 -0.521808  0.446309
2013-01-03  1.899832  0.460871 -1.491727 -0.647614
2013-01-04  1.126043  0.818145  0.159674 -1.490958
2013-01-05  0.113360  0.190421 -0.618656  0.976943
2013-01-06 -0.537863 -0.078802  0.197864 -1.414924

In [25]: pd.Series(df)
Out[25]:
0    A
1    B
2    C
3    D
dtype: object
morgan
  • 363
  • 1
  • 3
  • 5

2 Answers2

36

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')
Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
EngineeredE
  • 741
  • 5
  • 4
  • @Cristian Ciupitu after running what you suggest, then type(ts) it still prints pandas.core.frame.DataFrame. I believe what we want is a we're looking for is a timeseries object, right? According to McKinney, Wes. Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython (Kindle Location 6861). O'Reilly Media. Kindle Edition. ...when we run ts.index, it should print: 'pandas.tseries.index.DatetimeIndex' . – Ryan Chase Aug 28 '17 at 16:41
  • @RyanChase, I'm not the author of this answer. – Cristian Ciupitu Aug 28 '17 at 22:45
  • @EngineeredE after running what you suggest, then type(ts) it still prints pandas.core.frame.DataFrame. I believe what we want is a we're looking for is a timeseries object, right? According to McKinney, Wes. Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython (Kindle Location 6861). O'Reilly Media. Kindle Edition. ...when we run ts.index, it should print: 'pandas.tseries.index.DatetimeIndex' – Ryan Chase Aug 28 '17 at 23:08
11

Here is one possibility

[3]: df

Out[3]: 
                   A         B         C         D
2013-01-01 -0.024362  0.712035 -0.913923  0.755276
2013-01-02  2.624298  0.285546  0.142265 -0.047871
2013-01-03  1.315157 -0.333630  0.398759 -1.034859
2013-01-04  0.713141 -0.109539  0.263706 -0.588048
2013-01-05 -1.172163 -1.387645 -0.171854 -0.458660
2013-01-06 -0.192586  0.480023 -0.530907 -0.872709

In [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.192586
B  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.480023
C  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.530907
D  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.872709
dtype: float64
Jeff
  • 125,376
  • 21
  • 220
  • 187
  • 1
    I just saw this answer. What if the dataframe only has one column? `unstack` would return a series with a two-level index, and `pd.Series(df)` does not seem to work (it's really odd what it does, since it splits the column title into characters and populates the Series with copies of this splitting) – Amelio Vazquez-Reina Sep 11 '14 at 23:08
  • 1
    The only way I am getting it to work is with `df[df.columns[0]]` but that's is a bit an unnatural of doing a conversion. – Amelio Vazquez-Reina Sep 11 '14 at 23:13