2016-05-25 105 views
1

創建一個數據幀我有大熊貓時間戳的numpy的數組:大熊貓無法從numpy的數組的時間戳

array([[Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'), 
     Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'), 
     Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T')], 
     [Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'), 
     Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'), 
     Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T')], 
     [Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'), 
     Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'), 
     Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T')]], dtype=object) 

我不能創建此數組一個數據幀,作爲試圖這樣做將引發以下錯誤:

AssertionError: Number of Block dimensions (1) must equal number of axes (2) 

你可以看到數組顯然是二維的,我使用ndim進行了驗證。

爲什麼我無法創建DataFrame?

回答

1

我認爲你可以使用list理解:

import pandas as pd 
import numpy as np 

a =np.array([[pd.Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'), 
     pd.Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T'), 
     pd.Timestamp('2016-05-02 15:50:00+0000', tz='UTC', offset='5T')], 
     [pd.Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'), 
     pd.Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T'), 
     pd.Timestamp('2016-05-02 17:10:00+0000', tz='UTC', offset='5T')], 
     [pd.Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'), 
     pd.Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T'), 
     pd.Timestamp('2016-05-02 20:25:00+0000', tz='UTC', offset='5T')]], dtype=object) 

df = pd.DataFrame([x for x in a], columns=['a','b','c']) 
print (df) 
          a       b \ 
0 2016-05-02 15:50:00+00:00 2016-05-02 15:50:00+00:00 
1 2016-05-02 17:10:00+00:00 2016-05-02 17:10:00+00:00 
2 2016-05-02 20:25:00+00:00 2016-05-02 20:25:00+00:00 

          c 
0 2016-05-02 15:50:00+00:00 
1 2016-05-02 17:10:00+00:00 
2 2016-05-02 20:25:00+00:00 

另一種解決方案是DataFrame.from_records

print (pd.DataFrame.from_records(a, columns=['a','b','c'])) 
          a       b \ 
0 2016-05-02 15:50:00+00:00 2016-05-02 15:50:00+00:00 
1 2016-05-02 17:10:00+00:00 2016-05-02 17:10:00+00:00 
2 2016-05-02 20:25:00+00:00 2016-05-02 20:25:00+00:00 

          c 
0 2016-05-02 15:50:00+00:00 
1 2016-05-02 17:10:00+00:00 
2 2016-05-02 20:25:00+00:00 

alternate constructors of df

+0

這肯定回答我的問題,所以謝謝。我在問,因爲我遇到的最初問題是我試圖轉置時間戳的DataFrame。即使使用'from_records'構造DataFrame,轉置也會像以前一樣拋出相同的'AssertionError'。 現在,我在構建DataFrame之前轉置numpy數組。 –