爲什麼執行得厲害下面的代碼片段:如何更快地創建熊貓指數?
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
一百萬。任何想法如何重寫最後一行?
爲什麼執行得厲害下面的代碼片段:如何更快地創建熊貓指數?
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
一百萬。任何想法如何重寫最後一行?
如果需要TimedeltaIndex可以使用to_timedelta
或TimedeltaIndex
:
index = pd.to_timedelta(time, unit='ms')
或者:
index = pd.TimedeltaIndex(time, unit='ms')
這提高了5次性能。你是對的。我應該使用'TimeDeltaIndex'而不是'TimeDelta'數組。謝謝。我會在10分鐘內接受答案。 – major4x
您還可以使用pd.timedelta_range
index = pd.timedelta_range(0, periods=10000, freq='10ms')
什麼'pd.to_timedelta(時間, unit ='ms')'? – jezrael