2017-06-18 57 views
5

我想從下面兩列的數據框中計算銀行從一個timedelta的平均值和標準偏差。當我運行的代碼(如下所示)我得到的錯誤:pandas.core.base.DataError:沒有數字類型聚集尋找pandas中的timedelta對象的均值和標準差df

我的數據框:

bank       diff 
    Bank of Japan     0 days 00:00:57.416000 
    Reserve Bank of Australia  0 days 00:00:21.452000 
    Reserve Bank of New Zealand 55 days 12:39:32.269000 
    U.S. Federal Reserve   8 days 13:27:11.387000 

我的代碼:

means = dropped.groupby('bank').mean() 
std = dropped.groupby('bank').std() 

謝謝!

+0

你想如何聚合'timedelta'對象?如果您想要聚合,請訪問'.days'或'.seconds'屬性。 – Abdou

回答

5

您需要將timedelta轉換爲某個數值,例如, int64通過values什麼是最準確的,因爲轉換爲ns是什麼,是的timedelta數字表示:

dropped['new'] = dropped['diff'].values.astype(np.int64) 

means = dropped.groupby('bank').mean() 
means['new'] = pd.to_timedelta(means['new']) 

std = dropped.groupby('bank').std() 
std['new'] = pd.to_timedelta(std['new']) 

另一個解決方案是通過total_seconds將值轉換爲seconds,但是這是不準確的:

dropped['new'] = dropped['diff'].dt.total_seconds() 

means = dropped.groupby('bank').mean() 
+0

謝謝,這就像一個魅力 - (我用第一個解決方案)! –