주어진 타임스탬프가 오프셋에 있는지 확인하려면 Pandas에서 CustomBusinessHour.is_on_offset()을 사용하세요. 확인할 인수로 타임스탬프를 전달합니다.
먼저 필요한 라이브러리를 가져옵니다 -
import pandas as pd
Pandas에서 타임스탬프 개체 설정 -
timestamp = pd.Timestamp('2021-11-14 05:20:30')
CustomBusinessHour 오프셋을 만듭니다. CustomBusinessHour는 DateOffset 하위 클래스입니다 -
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30")
타임스탬프에 오프셋을 추가하고 업데이트된 타임스탬프를 표시합니다.
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)
주어진 타임스탬프가 오프셋에 있는지 확인하십시오 -
offset = cbhOffset.is_on_offset(pd.Timestamp('2021-11-20 05:20:30')) 결과 표시 -
print("\nCheck if the given timestamp is on offset or not...\n",offset)
예시
다음은 코드입니다 -
import pandas as pd
# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-11-14 05:20:30')
# Display the Timestamp
print("Timestamp...\n",timestamp)
# Create the CustomBusinessHour Offset
# CustomBusinessHour is the DateOffset subclass
# Here, "start" is the start time of your custom business hour in 24h format.
# The "end" is the end time of your custom business hour in 24h format.
cbhOffset = pd.tseries.offsets.CustomBusinessHour(start="09:30", end = "18:30")
# Display the CustomBusinessHour Offset
print("\nCustomBusinessHour Offset...\n",cbhOffset)
# Add the offset to the Timestamp and display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + cbhOffset)
# Check if the given timestamp is on offset or not
offset = cbhOffset.is_on_offset(pd.Timestamp('2021-11-20 05:20:30'))
# display the result
print("\nCheck if the given timestamp is on offset or not...\n",offset) 출력
이것은 다음 코드를 생성합니다 -
Timestamp... 2021-11-14 05:20:30 CustomBusinessHour Offset... <CustomBusinessHour: CBH=09:30-18:30> Updated Timestamp... 2021-11-15 10:30:00 Check if the given timestamp is on offset or not... False