2016-12-14 72 views
1

我想追加包含一些大熊貓時間戳和使用下面的代碼我怎麼能在一個數據幀

pair_columns = ['T1 Time', 'T1 Active', 'T1 Reactive', 'T2 Time', 'T2 Active', 'T2 Reactive'] 

# an empty dataframe 
matched_pairs = pd.DataFrame(columns=pair_columns) 


# A list with some Timestamp 
value_with_timestamp = [pd.Timestamp('2011-10-21 20:08:42+0000', tz='UTC'), 21.847724815467735, -78.998453511820344, pd.Timestamp('2011-10-21 20:08:54+0000', tz='UTC'), -74.608437575303114, 48.537725275212779] 
ser_timestamp = pd.Series(value_with_timestamp) 


# This pass, but the dataframe get a row containing only NaN 
matched_pairs.loc[len(matched_pairs)] = ser_timestamp 
print("Dataframe with series containing timestamp") 
print(matched_pairs.head()) 

# Exception TypeError: data type not understood 
matched_pairs.loc[len(matched_pairs)] = value_with_timestamp 
print(matched_pairs.head()) 

# Exception TypeError: data type not understood 
matched_pairs = matched_pairs.append(ser_timestamp, ignore_index=True) 
print(matched_pairs.head()) 

一些浮點值的數據幀的行添加pd.Timestamp和float組成的排此代碼不起作用,但與字符串,而不是時間戳,一切正常

import pandas as pd 

matched_pairs_string = pd.DataFrame(columns=pair_columns) 

# The same list but with string instend of timestamp 
value_string = ['2011-10-21 20:08:42+0000', 21.847724815467735, -78.998453511820344, '2011-10-21 20:08:54+0000', -74.608437575303114, 48.537725275212779] 

# Add the list with the string to the dataframe, this work like a charm 
matched_pairs_string.loc[len(matched_pairs_string)] = value_string 
print("Dataframe with string instead of timestamp") 
print(matched_pairs_string.head()) 

我在做什麼錯在這裏?有沒有辦法實現我想要的?我只是想將這些數據按原樣添加,而不是將時間戳轉換爲另一種類型?

回答

1

從技術上講,它不是,這是問題,但你分配什麼類型的對象能排時間戳:Vs的列表(你在第二次嘗試系列(你在第一個代碼塊嘗試)代碼塊)。

由於熊貓數據框中的每一列都是熊貓系列,所以不能爲系列分配一行。考慮轉換到一個列表行分配與series.tolist()或使用原來的列表:

matched_pairs.loc[len(matched_pairs)] = ser_timestamp.tolist() 
#    T1 Time T1 Active T1 Reactive    T2 Time T2 Active T2 Reactive 
# 0 2011-10-21 20:08:42 21.847725 -78.998454 2011-10-21 20:08:54 -74.608438  48.53772 

matched_pairs.loc[len(matched_pairs)] = value_with_timestamp 
#    T1 Time T1 Active T1 Reactive    T2 Time T2 Active T2 Reactive 
# 0 2011-10-21 20:08:42 21.847725 -78.998454 2011-10-21 20:08:54 -74.608438  48.53772 

並在這樣做,可以分配適當的數據類型:

print(matched_pairs.dtypes) 

# T1 Time  datetime64[ns] 
# T1 Active    float64 
# T1 Reactive   float64 
# T2 Time  datetime64[ns] 
# T2 Active    float64 
# T2 Reactive   float64 
# dtype: object 

由於OP表示,有可能成爲熊貓0.19以上版本的問題拋出異常:

TypeError: data type not understood

One possi ble分辨率可以在行分配之前在空數據框上顯式定義數據類型(時間戳和浮點數)。由於沒有單dtype()調用,一個循環運行到每一列轉換:

pair_columns = ['T1 Time', 'T1 Active', 'T1 Reactive', 'T2 Time', 'T2 Active', 'T2 Reactive'] 
pair_dtypes = ['M8[ms]', 'float', 'float', 'M8[ms]', 'float', 'float'] 

# an empty dataframe 
matched_pairs = pd.DataFrame(columns=pair_columns) 
datatypes = {k:v for k,v in zip(pair_columns, pair_dtypes)} 

for k,v in datatypes.items(): 
    matched_pairs[k] = matched_pairs[k].astype(v) 

... 
matched_pairs.loc[len(matched_pairs)] = ser_timestamp.tolist() 
# matched_pairs.loc[len(matched_pairs)] = value_with_timestamp 
+0

感謝您的答覆,但我不認爲我們的代碼和我之間的差異。如果我在嘗試將純列表分配給數據框行時採用該部分,則此嘗試已經完成。在我的系統上,我們的解決方案無法工作(通過轉換系列或純粹的列表),它返回一個TypeError:數據類型不明白。 Ubuntu 16.04上的熊貓版本是0.19.1。 – RossierFl

+0

嗯......非常有趣。事實上,我確實在第一個代碼塊中使用列表看到了下一行,但是確切地發佈了數據,我無法重新創建。我在Windows 10上使用python 3.4,pandas 0.18。沒有發生錯誤。你可以嘗試從另一個CPU環境?你確定這張貼的例子是你使用的嗎?嘗試乾淨,新的會議只是這個代碼(不是實際的項目)。 – Parfait

+0

是的,你說得對,那是一個環境問題。有趣的是,我測試過的三種環境(2 Ubuntu 14.04,python 2.7.12,pandas 0.19.1和1 Windows 10 python 2.7.12以及大熊貓0.19.1)只有其中一個正常工作...謝謝 ! – RossierFl