2012-03-31 79 views
0

我很難將numpy數據類型timedelta64乘上浮點數。Python:timedelta64類型與浮點數的劃分和乘法運算

對於我需要用開普勒第二定律計算恆星週期的任務。有很多數據點的,所以我想Python來計算兩個位置之間的區域和時段劃分它,使用下面的代碼:

D = data['data'] 
vect = D-c #Data point minus center of ellipse 
date = data['time'] #time for data point in np.timedelta64 
Area_tot = np.pi*np.sqrt(chi[0])*np.sqrt(chi[1]) #total area of ellipse 
P = np.array([]) 
for i in range(1,len(D[0])): 
    Area = LA.norm(np.cross(vect[i],vect[i-1]))/2 #usie cross product to calculate area 
    Time = date[i]-date[i-1] 
    P = np.append(P,(Area_tot/Area)*Time) 

如果這樣做,但是,我得到以下錯誤:

TypeError: ufunc 'multiply' not supported for the input types, and the inputs could 
not be safely coerced to any supported types according to the casting rule 'safe' 

所以,我想知道我怎麼能倍增timedelta64數據類型與浮...

在此先感謝和溫柔,我很新的這兩個計算器和編程:)

回答

1
Time.tolist().total_seconds() 

以浮點形式獲得差異。

+0

timedelta64數據類型似乎沒有total_seconds() – Corollary 2012-03-31 16:12:25

+0

.tolist()爲您提供了一個datetime.timedelta對象,它具有.total_seconds()。如果您不想使用datetime.timedelta,則[timedelta64] .astype('float')會以微秒爲單位給出差異。 – 2012-03-31 16:30:52

+0

謝謝!我會嘗試那個。 – Corollary 2012-03-31 16:34:30