Python으로 시계열 데이터 다루기
Stefan Jansen
Founder & Lead Data Scientist at Applied Artificial Intelligence
대표적인 시계열 조작 방법:
값을 과거 또는 미래로 이동(시프트 또는 래그)
특정 기간의 값 변화량 계산
임의의 기간에 걸친 퍼센트 변화율 계산
pandas 내장 메서드는 pd.DateTimeIndex를 기반으로 합니다
pd.read_csv()로 파싱을 자동으로 처리하세요!google = pd.read_csv('google.csv', parse_dates=['date'], index_col='date')
google.info()
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 504 entries, 2015-01-02 to 2016-12-30
Data columns (total 1 columns):
price 504 non-null float64
dtypes: float64(1)
google.head()
price
date
2015-01-02 524.81
2015-01-05 513.87
2015-01-06 501.96
2015-01-07 501.10
2015-01-08 502.68
.shift():periods=1google['shifted'] = google.price.shift() # default: periods=1google.head(3)
price shifted
date
2015-01-02 542.81 NaN
2015-01-05 513.87 542.81
2015-01-06 501.96 513.87
.shift(periods=-1):google['lagged'] = google.price.shift(periods=-1)google[['price', 'lagged', 'shifted']].tail(3)
price lagged shifted
date
2016-12-28 785.05 782.79 791.55
2016-12-29 782.79 771.82 785.05
2016-12-30 771.82 NaN 782.79
google['change'] = google.price.div(google.shifted)google[['price', 'shifted', 'change']].head(3)
price shifted change
Date
2017-01-03 786.14 NaN NaN
2017-01-04 786.90 786.14 1.000967
2017-01-05 794.02 786.90 1.009048
google['return'] = google.change.sub(1).mul(100)google[['price', 'shifted', 'change', 'return']].head(3)
price shifted change return
date
2015-01-02 524.81 NaN NaN NaN
2015-01-05 513.87 524.81 0.98 -2.08
2015-01-06 501.96 513.87 0.98 -2.32
인접한 두 기간의 값 변화량
$x_t - x_{t-1}$
google['diff'] = google.price.diff()google[['price', 'diff']].head(3)
price diff
date
2015-01-02 524.81 NaN
2015-01-05 513.87 -10.94
2015-01-06 501.96 -11.91
인접한 두 기간의 퍼센트 변화율
$\frac{x_t}{x_{t-1}}$
google['pct_change'] = google.price.pct_change().mul(100)google[['price', 'return', 'pct_change']].head(3)
price return pct_change
date
2015-01-02 524.81 NaN NaN
2015-01-05 513.87 -2.08 -2.08
2015-01-06 501.96 -2.32 -2.32
google['return_3d'] = google.price.pct_change(periods=3).mul(100)google[['price', 'return_3d']].head()
price return_3d
date
2015-01-02 524.81 NaN
2015-01-05 513.87 NaN
2015-01-06 501.96 NaN
2015-01-07 501.10 -4.517825
2015-01-08 502.68 -2.177594
Python으로 시계열 데이터 다루기