반응형

16-02 특정기간 필터링 (between_time)


DataFrame.between_time(start_time, end_time, include_start=True, include_end=True, axis=None)

 

개요


between_time메서드는 시계열(날짜-시간형태) 데이터에서 특정 시간대의 값을 필터링하는 메서드 입니다.

 

사용법


기본 사용법
※ 자세한 내용은 아래 예시를 참고 바랍니다.
df.between_time(start_time, end_time, include_start=True, include_end=True, axis=None)
start_time : 기준이 될 시작 시간입니다.
end_time : 기준이 될 끝 시간입니다.
include_start : 시작 시간을 포함할지 여부입니다.
include_end : 끝 시간을 포함할지 여부입니다.
axis : 기준이 될 축 입니다.

반응형

 

예시


먼저 기본적인 사용법 예시를 위해 1시간 간격의 10행 데이터를 만들어보겠습니다.

i = pd.date_range('2021-12-24', periods=10, freq='1H')
# 2021-12-24를 시작으로 10기간(간격 1H)의 데이터 생성.
df = pd.DataFrame({'col1':[1,2,3,4,5,6,7,8,9,10]}, index=i)
print(df)
>>
                     col1
2021-12-24 00:00:00     1
2021-12-24 01:00:00     2
2021-12-24 02:00:00     3
2021-12-24 03:00:00     4
2021-12-24 04:00:00     5
2021-12-24 05:00:00     6
2021-12-24 06:00:00     7
2021-12-24 07:00:00     8
2021-12-24 08:00:00     9
2021-12-24 09:00:00    10

기본적인 사용법
위 시간에 대해서 시간이 03:00~06:00인 값만 출력해보도록 하겠습니다.

print(df.between_time(start_time='03:00',end_time='06:00'))
>>
                     col1
2021-12-24 03:00:00     4
2021-12-24 04:00:00     5
2021-12-24 05:00:00     6
2021-12-24 06:00:00     7


include_start/end의 사용
include_start / end를 False로 하면 시작/끝 시간을 제외할 수 있습니다.

print(df.between_time(start_time='03:00',end_time='06:00',
                      include_start=False, include_end=False))
>>
                     col1
2021-12-24 04:00:00     5
2021-12-24 05:00:00     6
# 03:00과 06:00이 제외됨.


해당 시간을 제외한 시간 필터링
start_time을 end_time보다 늦은 시간으로 둘 경우, 두 시간사이를 제외한 값을 출력합니다.

print(df.between_time(start_time='06:00',end_time='03:00'))
# start_time이 end_time보다 늦음
>>
                     col1
2021-12-24 00:00:00     1
2021-12-24 01:00:00     2
2021-12-24 02:00:00     3
2021-12-24 03:00:00     4
2021-12-24 06:00:00     7
2021-12-24 07:00:00     8
2021-12-24 08:00:00     9
2021-12-24 09:00:00    10
#03:00 ~ 06:00을 제외한 시간이 출력됨. 
반응형

+ 최근 글