Pandas convert timers to multiple columns DataFrame

I have temporary data of intraday data as shown below

ts =pd.Series(np.random.randn(60),index=pd.date_range('1/1/2000',periods=60, freq='2h'))

      

I am hoping to convert data to a DataFrame with columns as each date and row as time in date.

I have tried these,

key = lambda x:x.date()
grouped = ts.groupby(key)

      

But how to convert groups to DataFrame columns with date date? or is there a better way?

+3


source to share


1 answer


import pandas as pd
import numpy as np

index = pd.date_range('1/1/2000', periods=60, freq='2h')
ts = pd.Series(np.random.randn(60), index = index)

key = lambda x: x.time()
groups = ts.groupby(key)

print pd.DataFrame({k:g for k,g in groups}).resample('D').T

      

of



          2000-01-01  2000-01-02  2000-01-03  2000-01-04  2000-01-05  2000-01-06  \
00:00:00    0.109959   -0.124291   -0.137365    0.054729   -1.305821   -1.928468   
03:00:00    1.336467    0.874296    0.153490   -2.410259    0.906950    1.860385   
06:00:00   -1.172638   -0.410272   -0.800962    0.568965   -0.270307   -2.046119   
09:00:00   -0.707423    1.614732    0.779645   -0.571251    0.839890    0.435928   
12:00:00    0.865577   -0.076702   -0.966020    0.589074    0.326276   -2.265566   
15:00:00    1.845865   -1.421269   -0.141785    0.433011   -0.063286    0.129706   
18:00:00   -0.054569    0.277901    0.383375   -0.546495   -0.644141   -0.207479   
21:00:00    1.056536    0.031187   -1.667686   -0.270580   -0.678205    0.750386   

          2000-01-07  2000-01-08  
00:00:00   -0.657398   -0.630487  
03:00:00    2.205280   -0.371830  
06:00:00   -0.073235    0.208831  
09:00:00    1.720097   -0.312353  
12:00:00   -0.774391         NaN  
15:00:00    0.607250         NaN  
18:00:00    1.379823         NaN  
21:00:00    0.959811         NaN

      

+2


source







All Articles