2017-06-18 25 views
1

爲什麼執行得厲害下面的代碼片段:如何更快地創建熊貓指數?

import numpy 
import pandas 

time = numpy.array(range(0, 1000000, 10), dtype = numpy.uint32) 
index = [ pandas.Timedelta(str(t) + 'ms') for t in time ] 

大約需要一個第二和一個體面的桌面上半和我們談論僅pandas.Timedelta一百萬。任何想法如何重寫最後一行?

+1

什麼'pd.to_timedelta(時間, unit ='ms')'? – jezrael

回答

3

如果需要TimedeltaIndex可以使用to_timedeltaTimedeltaIndex

index = pd.to_timedelta(time, unit='ms') 

或者:

index = pd.TimedeltaIndex(time, unit='ms') 
+0

這提高了5次性能。你是對的。我應該使用'TimeDeltaIndex'而不是'TimeDelta'數組。謝謝。我會在10分鐘內接受答案。 – major4x