Search code examples
pandasdatagram

turning datagram into timeseries using set_index pandas


I have the following datagram:

        date      DP
0  1/31/1900  0.0357
1  2/28/1900  0.0362
2  3/31/1900  0.0371
3  4/30/1900  0.0379
4  5/31/1900  0.0410

I want to turn it into a timeseries, and I'm using set_index to do so. After running this line of code:

df.set_index('date', inplace=True)

I get the following timeseries:

               DP
date             
1/31/1900  0.0357
2/28/1900  0.0362
3/31/1900  0.0371
4/30/1900  0.0379
5/31/1900  0.0410

This is turning date into an index itself, which messes up my program later on when I concatenate datagrams via date. I know I can fix this manually, but I'd like to know what I'm doing wrong here. As far as I know, I'm using the function correctly, and if I don't do it inplace pandas will make a new datagram and have the indexes be 0-n by default.


Solution

  • Option 1

    pd.Series(df.DP.values, pd.to_datetime(df.date))
    
    date
    1900-01-31    0.0357
    1900-02-28    0.0362
    1900-03-31    0.0371
    1900-04-30    0.0379
    1900-05-31    0.0410
    dtype: float64
    

    Option 2

    df.DP.set_axis(pd.to_datetime(df.date), inplace=False)
    
    date
    1900-01-31    0.0357
    1900-02-28    0.0362
    1900-03-31    0.0371
    1900-04-30    0.0379
    1900-05-31    0.0410
    Name: DP, dtype: float64
    

    Option 3
    @Wen's solution (will delete when he posts)

    df.set_index(pd.to_datetime(df.date)).DP
    
    date
    1900-01-31    0.0357
    1900-02-28    0.0362
    1900-03-31    0.0371
    1900-04-30    0.0379
    1900-05-31    0.0410
    Name: DP, dtype: float64