Pandas에서 DateTimeIndex의 날짜·시간 값을 특정 단위 아래로 잘라내는 floor(내림) 연산을 수행하려면 DateTimeIndex.floor() 메서드를 사용합니다. 이때 마이크로초(microseconds) 빈도를 기준으로 내림하려면 freq 매개변수에 'us' 값을 지정하면 됩니다.
필요한 라이브러리 임포트
먼저 pandas 라이브러리를 임포트합니다.
import pandas as pd
DateTimeIndex 생성
시작 시점 '2021-10-18 07:20:32.261811624'부터 나노초까지 포함된 타임스탬프를 기준으로, 40초 간격('40S')으로 총 5개의 요소를 가지는 DatetimeIndex를 생성합니다. 타임존은 'Australia/Adelaide'로 설정합니다.
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')마이크로초 빈도로 floor 연산 수행
생성된 DateTimeIndex에 대해 floor(freq='us')를 호출하면 나노초 자릿수가 잘리고 마이크로초 단위까지 값이 유지됩니다.
print("\n마이크로초 빈도로 floor 연산 수행...\n",
datetimeindex.floor(freq='us'))전체 예제 코드
import pandas as pd
# 40초(S) 간격, 총 5개 요소를 가지는 DatetimeIndex 생성
# 타임존은 Australia/Adelaide
datetimeindex = pd.date_range('2021-10-18 07:20:32.261811624', periods=5,
tz='Australia/Adelaide', freq='40S')
# DateTimeIndex 출력
print("DateTimeIndex...\n", datetimeindex)
# DateTimeIndex의 빈도 출력
print("DateTimeIndex frequency...\n", datetimeindex.freq)
# 마이크로초 빈도('us')로 floor 연산 수행
print("\nPerforming floor operation with microseconds frequency...\n",
datetimeindex.floor(freq='us'))실행 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
DateTimeIndex... DatetimeIndex(['2021-10-18 07:20:32.261811624+10:30', '2021-10-18 07:21:12.261811624+10:30', '2021-10-18 07:21:52.261811624+10:30', '2021-10-18 07:22:32.261811624+10:30', '2021-10-18 07:23:12.261811624+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq='40S') DateTimeIndex frequency... <40 * Seconds> Performing floor operation with microseconds frequency... DatetimeIndex(['2021-10-18 07:20:32.261811+10:30', '2021-10-18 07:21:12.261811+10:30', '2021-10-18 07:21:52.261811+10:30', '2021-10-18 07:22:32.261811+10:30', '2021-10-18 07:23:12.261811+10:30'], dtype='datetime64[ns, Australia/Adelaide]', freq=None)
결과 해석
출력 결과를 보면 원래 타임스탬프가 나노초(nanosecond) 자릿수인 .261811624까지 표시되었지만, floor 연산 후에는 마이크로초 자릿수인 .261811까지만 남은 것을 확인할 수 있습니다. 즉, 나노초 단위의 값이 마이크로초 경계로 잘려나가며 내림 처리된 것입니다. 참고로 floor 연산이 적용된 새 인덱스에는 별도의 고정 빈도(freq=None)가 설정되지 않습니다.