파이썬
시계열데이터에서 데이터 생성 빈도 알아내기
주경야매
2022. 6. 15. 02:19
외부에서 가져온 시계열 데이터를 이용할 때, 데이터 생성 빈도를 알아내고 싶은 경우가 있다.
이럴땐 pandas 에서 제공하는 infer_freq() 함수를 이용하자.
pandas.infer_freq(index, warn=True)
주어진 데이터를 이용해 가장 근접한 빈도를 추측한다. 추측이 불가할 경우 warning이 출력된다.
Inputs
- index: 시계열데이터. Series인 경우 인덱스가 아닌 밸류를 사용.
- warn: bool, default True.
Returns
str or None (빈도 추측이 불가할 경우 None)
Errors
TypeError: 입력값이 날짜 형식이 아닌 경우
ValueError: 입력값이 3개 미만인 경우
예제
>>> idx = pd.date_range(start='2020/12/01', end='2020/12/30', periods=30)
>>> pd.infer_freq(idx)
'D'
결과값은 다음과 같이 해석하면 된다.
| B | business day frequency |
| C | custom business day frequency |
| D | calendar day frequency |
| W | weekly frequency |
| M | month end frequency |
| SM | semi-month end frequency (15th and end of month) |
| BM | business month end frequency |
| CBM | custom business month end frequency |
| MS | month start frequency |
| SMS | semi-month start frequency (1st and 15th) |
| BMS | business month start frequency |
| CBMS | custom business month start frequency |
| Q | quarter end frequency |
| BQ | business quarter end frequency |
| QS | quarter start frequency |
| BQS | business quarter start frequency |
| A, Y | year end frequency |
| BA, BY | business year end frequency |
| AS, YS | year start frequency |
| BAS, BYS | business year start frequency |
| BH | business hour frequency |
| H | hourly frequency |
| T, min | minutely frequency |
| S | secondly frequency |
| L, ms | milliseconds |
| U, us | microseconds |
| N | nanoseconds |