■ Series 클래스의 tz_convert 메소드를 사용해 시계열을 특정 시간대로 변환하는 방법을 보여준다.
▶ main.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import pandas as pd import numpy as np datetimeIndex = pd.date_range("2012/03/01 00:00:00", periods = 5, freq = "3D") series1 = pd.Series(np.random.randn(len(datetimeIndex)), datetimeIndex) print(series1) """ 2012-03-01 -1.639957 2012-03-04 0.449630 2012-03-07 -1.289113 2012-03-10 -0.215940 2012-03-13 0.419875 Freq: 3D, dtype: float64 """ print() series2 = series1.tz_localize("UTC") print(series2) """ 2012-03-01 00:00:00+00:00 -1.639957 2012-03-04 00:00:00+00:00 0.449630 2012-03-07 00:00:00+00:00 -1.289113 2012-03-10 00:00:00+00:00 -0.215940 2012-03-13 00:00:00+00:00 0.419875 Freq: 3D, dtype: float64 """ print() series3 = series2.tz_convert("US/Eastern") print(series3) """ 2012-02-29 19:00:00-05:00 -1.639957 2012-03-03 19:00:00-05:00 0.449630 2012-03-06 19:00:00-05:00 -1.289113 2012-03-09 19:00:00-05:00 -0.215940 2012-03-12 20:00:00-04:00 0.419875 Freq: 3D, dtype: float64 """ |
▶ requirements.txt
1 2 3 4 5 6 7 8 |
numpy==2.1.2 pandas==2.2.3 python-dateutil==2.9.0.post0 pytz==2024.2 six==1.16.0 tzdata==2024.2 |
※ pip install pandas 명령을 실행했다.