5

数日間の 1 分間のデータの時系列があり、時間帯ごとにすべての日を平均したいと考えています。

これは非常に遅いです:

from datetime import datetime
from pandas import date_range, Series
time_ind = date_range(datetime(2013, 1, 1), datetime(2013, 1, 10), freq='1min')
all_data = Series(randn(len(time_ind)), time_ind)
time_mean = all_data.groupby(lambda x: x.time()).mean()

実行には約 1 分かかります。

次のような間:

time_mean = all_data.groupby(lambda x: x.minute).mean()

ほんの一瞬しかかかりません。

時間帯ごとにグループ化するより速い方法はありますか?

なぜこれがとても遅いのですか?

4

2 に答える 2

3

あなたの「ラムダバージョン」とバージョン0.11で導入された時間プロパティの両方が、バージョン0.11.0では遅いようです:

In [4]: %timeit all_data.groupby(all_data.index.time).mean()
1 loops, best of 3: 11.8 s per loop

In [5]: %timeit all_data.groupby(lambda x: x.time()).mean()
Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <type 'exceptions.RuntimeError'> ignored
Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <type 'exceptions.RuntimeError'> ignored
Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <type 'exceptions.RuntimeError'> ignored
1 loops, best of 3: 11.8 s per loop

現在のマスターでは、両方の方法がかなり高速です。

In [1]: pd.version.version
Out[1]: '0.11.1.dev-06cd915'

In [5]: %timeit all_data.groupby(lambda x: x.time()).mean()
1 loops, best of 3: 215 ms per loop

In [6]: %timeit all_data.groupby(all_data.index.time).mean()
10 loops, best of 3: 113 ms per loop
'0.11.1.dev-06cd915'

したがって、マスターに更新するか、今月リリースされる 0.11.1 を待つことができます。

于 2013-06-25T05:33:55.890 に答える