본문 바로가기
Python with AI

Pandas resample for date control(W-MON or W-FRI) | 판다스 리샘플링

by Oliver J 2023. 7. 6.
반응형

판다스 resample 함수는 유용하게 시계열 데이터를 샘플링할 수 있다. 

Pandas resample function is useful for sampling of the time series data

 

일반적으로 사용되어지는 옵션으로는 주 단위 샘플링은 W, 월 단위 샘플링은 M, 년 단위는 Y, 분단위나 초 단위도 가능하다. 

Available used options of pandas.resample are W for a week, M for a month, Y for a year, and possibly for minutes and seconds unit.

 

 

하지만 해당 샘플링의 일자가 변경돼야 할 경우가 있다. 

Sometimes, we need different dates for easier control or what we want.

 

예를 들어, 주 단위로 데이터를 샘플링하려고 하지만 해당 주의 월요일이나 금요일로 날짜를 나오게 하고 싶을 때에는 아래와 같은 방법을 사용하면 된다. 

If the data format requirement is weekly resampling but the date should be Monday or Friday, we can use the option 'W-MON' for weekly sampling but Monday date usage or 'W-FRI' for Friday date usage.

 

import FinanceDataReader as fdr
df = fdr.DataReader('AAPL')
df.resample('W-MON').last().tail()

 

df.resample('W-FRI').last().tail()

 

Yay~!

728x90
반응형