3

I have a timeseries of intraday day data looks like below

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

I am hoping to transform the data into a DataFrame, with the columns as each date, and rows as the time in the date.

I have tried these,

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

But how do I transform the groups into date columned DataFrame? or is there any better way?

1 Answer 1

2
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

out:

          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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.