TimeDeltaIndex를 시간(hourly) 빈도 기준으로 반올림하려면 TimeDeltaIndex.round() 메서드를 사용합니다. 이때 freq 매개변수에 'H' 값을 지정하면 시간 단위로 반올림할 수 있습니다.
먼저 필요한 라이브러리를 임포트합니다.
import pandas as pd
TimeDeltaIndex 객체를 생성합니다. 'data' 매개변수에 timedelta 형식의 데이터를 설정합니다.
tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 3us 10ns', '+22:39:19.999999', '2 day 4h 03:08:02.000045', '+21:15:45.999999'])
생성된 TimedeltaIndex를 화면에 출력합니다.
print("TimedeltaIndex...\n", tdIndex)
시간 빈도('H')를 기준으로 반올림 연산을 수행합니다.
print("\nPerforming round operation with hourly frequency...\n",
tdIndex.round(freq='H'))전체 예제 코드
다음은 위 과정을 하나로 정리한 전체 코드입니다.
import pandas as pd
# TimeDeltaIndex 객체 생성
# 'data' 매개변수에 timedelta 형식의 데이터를 설정합니다
tdIndex = pd.TimedeltaIndex(data =['10 day 5h 2 min 3us 10ns', '+22:39:19.999999',
'2 day 4h 03:08:02.000045', '+21:15:45.999999'])
# TimedeltaIndex 출력
print("TimedeltaIndex...\n", tdIndex)
# TimeDelta 구성 요소(components)를 데이터프레임 형태로 반환
print("\nThe Dataframe of the components of TimeDeltas...\n", tdIndex.components)
# 시간 빈도('H')를 기준으로 반올림 연산 수행
print("\nPerforming round operation with hourly frequency...\n",
tdIndex.round(freq='H'))실행 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
TimedeltaIndex...
TimedeltaIndex(['10 days 05:02:00.000003010', '0 days 22:39:19.999999',
'2 days 07:08:02.000045', '0 days 21:15:45.999999'],
dtype='timedelta64[ns]', freq=None)
The Dataframe of the components of TimeDeltas...
days hours minutes seconds milliseconds microseconds nanoseconds
0 10 5 2 0 0 3 10
1 0 22 39 19 999 999 0
2 2 7 8 2 0 45 0
3 0 21 15 45 999 999 0
Performing round operation with hourly frequency...
TimedeltaIndex(['10 days 05:00:00', '0 days 23:00:00', '2 days 07:00:00',
'0 days 21:00:00'],
dtype='timedelta64[ns]', freq=None)