How to convert pandas time series using index attributes?

For a time series data frame that looks like this:

                      Close
2015-02-20 14:00:00  1200.1
2015-02-20 14:10:00  1199.8
2015-02-21 14:00:00  1199.3
2015-02-21 14:10:00  1199.0
2015-02-22 14:00:00  1198.4
2015-02-22 14:10:00  1199.7

How can I apply a function that converts it to a dataframe as follows:

          '14:00' '14:10'
2015-02-20 1200.1 1199.8
2015-02-21 1199.3 1199.0
2015-02-22 1198.4 1199.7

Note. This is a simplified example. The actual DataFrame has many days and all intraday minutes too. Therefore, it would be useful if this is an effective procedure.

thank

+4
source share
1 answer

you can rotate components dateand timeindex:

Create a frame:

i =pd.to_datetime(['2015-02-20 14:00:00','2015-02-20 14:10:00','2015-02-21 14:20:00'\
               ,'2015-02-21 14:30:00','2015-02-22 14:40:00','2015-02-22 14:50:00'])
df =pd.DataFrame(index=i, data={'Close':[1200.1,1199.8,1199.3,1199.0,1198.4,1199.7]})

axis:

pd.pivot_table(df, index= df.index.date, columns=df.index.time, values = 'Close')

returns:

          14:00:00  14:10:00    14:20:00    14:30:00    14:40:00    14:50:00
2015-02-20  1200.1  1199.8      NaN         NaN         NaN         NaN
2015-02-21  NaN     NaN         1199.3      1199        NaN         NaN
2015-02-22  NaN     NaN         NaN         NaN         1198.4     1199.7

use aggfuncas an argument pivot_tableto determine how data is aggregated if necessary

+1

All Articles