pandas将时间序列转换为多列DataFrame

时间:2013-02-16 05:40:53

标签: python pandas dataframe pandas-groupby

我有一个时间序列的日内数据如下所示

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

我希望将数据转换为DataFrame,列为每个日期,行为日期中的时间。

我试过这些,

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

但是如何将组转换为日期columned DataFrame?还是有更好的方法吗?

1 个答案:

答案 0 :(得分: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

出:

          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