DataFrame에서 여러 열을 통해 함수를 적용하는 똑똑한 솔루션을 제공하는 this question을 개선하면서 솔루션의 속도를 최적화 할 수 있는지 궁금합니다.팬더 데이터 프레임에 rolling_apply가 더 빠릅니까?
환경 : Python 2.7.8, Pandas 14.1, Numpy 1.8. meanmax
기능이 페어되지 않도록
import pandas as pd
import numpy as np
import random
def meanmax(ii,df):
xdf = df.iloc[map(int,ii)]
n = max(xdf['A']) + max(xdf['B'])
return n/2.0
df = pd.DataFrame(np.random.randn(2500,2)/10000,
index=pd.date_range('2001-01-01',periods=2500),
columns=['A','B'])
df['ii'] = range(len(df))
res = pd.rolling_apply(df.ii, 26, lambda x: meanmax(x, df))
참고 작동하지 않습니다 rolling_mean(df['A'] + df['B'],26)
같은 따라서 뭔가 :
다음은 예제 설정이다.
내가 좋아하는 뭔가를 할 수있다 그러나 : 빠른 약 3000x를 완료
res2 = (pd.rolling_max(df['A'],26) + pd.rolling_max(df['B'],26))/2
:
%timeit res = pd.rolling_apply(df.ii, 26, lambda x: meanmax(x, df))
1 loops, best of 3: 1 s per loop
%timeit res2 = (pd.rolling_max(df['A'],26) + pd.rolling_max(df['B'],26))/2
1000 loops, best of 3: 325 µs per loop
위의 두 번째 옵션보다는 동등한 더 나은 아무것도 /, 주어진 예 기능과 rolling_apply
를 사용하여 거기를 ? A의 크기 n
배열 위에 일반 압연 함수 컴퓨팅 성능 타이밍 보정
어 ..그것은 1 초에 비해 325 마이크로 초입니다. 1000 배 이상 빨라졌습니다. – EdChum
사실, 거기서 'μ'를 알아 채지 못했습니다 ... 수정 해 주셔서 감사합니다. – bazel