시계열 데이터 분석 (3) : 시간대

전창우·2023년 9월 26일
0

python for data analysis

목록 보기
15/23

시간대 다루기

시간대를 처리하는 일은 시계열을 다루는 작업 중에서 가장 음... 까다롭다.

시간대 지역화와 변환

기본적으로 pandas에서 시계열은 시간대를 엄격히 다루지 않는다. 다음 시계열을 살펴보자.

rng = pd.date_range('3/9/2012 9:30', periods=6, freq='D')
ts = pd.Series(np.random.randn(len(rng)), index=rng)
ts
"""
2012-03-09 09:30:00    2.691512
2012-03-10 09:30:00   -0.605512
2012-03-11 09:30:00   -0.883942
2012-03-12 09:30:00    0.467088
2012-03-13 09:30:00   -0.368373
2012-03-14 09:30:00   -0.486764
Freq: D, dtype: float64
"""

현재 tz cloumn은 None이다.

print(ts.index.tz)
#Out:print(ts.index.tz)
pd.date_range('3/9/2012 9:30',periods=10,freq='D',tz='UTC')
"""
DatetimeIndex(['2012-03-09 09:30:00+00:00', '2012-03-10 09:30:00+00:00',
               '2012-03-11 09:30:00+00:00', '2012-03-12 09:30:00+00:00',
               '2012-03-13 09:30:00+00:00', '2012-03-14 09:30:00+00:00',
               '2012-03-15 09:30:00+00:00', '2012-03-16 09:30:00+00:00',
               '2012-03-17 09:30:00+00:00', '2012-03-18 09:30:00+00:00'],
              dtype='datetime64[ns, UTC]', freq='D')
"""

시간대를 지정해서 날짜 범위를 생성할 수 있다.
지역화 시간으로의 변환은 tz_localize 메서드로 처리할 수 있다.

ts
"""
2012-03-09 09:30:00    0.996356
2012-03-10 09:30:00   -0.942317
2012-03-11 09:30:00    0.882562
2012-03-12 09:30:00   -0.055533
2012-03-13 09:30:00   -1.773056
2012-03-14 09:30:00    0.914307
Freq: D, dtype: float64
"""
ts_utc=ts.tz_localize('UTC')
ts_utc
"""
2012-03-09 09:30:00+00:00    0.996356
2012-03-10 09:30:00+00:00   -0.942317
2012-03-11 09:30:00+00:00    0.882562
2012-03-12 09:30:00+00:00   -0.055533
2012-03-13 09:30:00+00:00   -1.773056
2012-03-14 09:30:00+00:00    0.914307
Freq: D, dtype: float64
"""

시계열이 특정 시간대로 지역화되고 나면 tz_convert를 이용해서 다른 시간대로 변환 가능하다.

ts_utc.tz_convert('America/New_York')
"""
2012-03-09 04:30:00-05:00    0.996356
2012-03-10 04:30:00-05:00   -0.942317
2012-03-11 05:30:00-04:00    0.882562
2012-03-12 05:30:00-04:00   -0.055533
2012-03-13 05:30:00-04:00   -1.773056
2012-03-14 05:30:00-04:00    0.914307
Freq: D, dtype: float64
"""

위 시계열의 경우에는 America/New_York 시간대에서 일광절약시간을 사용하고 있는데, 동부표준시(EST)로 맞춘 다음 UTC 혹은 베를린 시간으로 변환할 수 있다.

시간대를 고려해서 Timestamp 객체 다루기

시계열이나 날짜 범위와 비슷하게 개별 Timstamp 객체도 시간대를 고려한 형태로 변환이 가능하다.

stamp=pd.Timestamp('2011-03-12 04:00')
stamp_utc=stamp.tz_localize('utc')
stamp_utc.tz_convert('America/New_York')
#Out:Timestamp('2011-03-11 23:00:00-0500', tz='America/New_York')

Timestamp 객체를 생성할 때 시간대를 직접 넘겨주는 것도 가능하다.

stamp_moscow=pd.Timestamp('2011-03-12 04:00',tz='Europe/Moscow')
stamp_moscow
#Out:Timestamp('2011-03-12 04:00:00+0300', tz='Europe/Moscow')

다른 시간대 간의 연산

서로 다른 시간대를 갖는 두 시계열이 하나로 합쳐지면 결과는 UTC가 된다. 타임스탬프는 내부적으로 UTC로 저장되므로 추가적인 변환이 없는 명료한 연산이다.

ts2=ts1[2:].tz_convert('Europe/Moscow')
result=ts1+ts2
result.index
"""
Out:DatetimeIndex(['2012-03-07 09:30:00+00:00', '2012-03-08 09:30:00+00:00',
               '2012-03-09 09:30:00+00:00', '2012-03-12 09:30:00+00:00',
               '2012-03-13 09:30:00+00:00', '2012-03-14 09:30:00+00:00',
               '2012-03-15 09:30:00+00:00'],
              dtype='datetime64[ns, UTC]', freq=None)
"""

마치며

이번 로그에서는 시간대에 관해서 다루어보았다. 다음 로그에서는 time serise data 중 Period 에 대해서 다뤄보자.

0개의 댓글