時系列データ(株価データ)のある値(終値)の移動平均を計算するのに、どのやり方が早いのかやってみた。
- pandas
- numpy
- bottleneck(中身はnumpyが使われているらしい)
PCスペック ryzen3600
結論. bottleneck
import pandas as pd
import numpy as np
import bottleneck as bn
# 読み込み
path = 'データ.csv'
df = pd.read_csv(path)
# 終値
close = df['Close']
# pandas
%time ma_close = close.rolling(25).mean()
# numpy
def moving_average(a, n) :
ret = np.cumsum(a, dtype=float)
ret[n:] = ret[n:] - ret[:-n]
return ret[n - 1:] / n
%time ma_close = moving_average(close.to_numpy(), 25)
# bottleneck
def rollavg_bottlneck(a,n):
return bn.move_mean(a, window=n,min_count = None)
%time ma_close = rollavg_bottlneck(close.to_numpy(), 25)
%time
CPU times: user 332 µs, sys: 0 ns, total: 332 µs Wall time: 280 µs
CPU times: user 130 µs, sys: 0 ns, total: 130 µs Wall time: 100 µs
CPU times: user 42 µs, sys: 0 ns, total: 42 µs Wall time: 45.5 µs