หากต้องการส่งคืนรหัสกฎที่ใช้กับวัตถุ BusinessDay ที่กำหนด ให้ใช้คุณสมบัติ BusinessDay.rule_code ใน Pandas
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import datetime import pandas as pd
ตั้งค่าวัตถุประทับเวลาใน Pandas -
timestamp = pd.Timestamp('2021-10-30 01:55:02.000045')
สร้างออฟเซ็ตวันธุรกิจ BusinessDay เป็นคลาสย่อย DateOffset -
bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(hours = 8, minutes = 10))
แสดงการประทับเวลาที่อัปเดต -
print("\nUpdated Timestamp...\n",timestamp + bdOffset)
ส่งคืนรหัสกฎของความถี่ที่ใช้กับ BusinessDay Offset ที่กำหนด -
print("\nThe rule code of the BusinessDay object..\n", bdOffset.rule_code) ตัวอย่าง
ต่อไปนี้เป็นรหัส -
import datetime
import pandas as pd
# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-10-30 01:55:02.000045')
# Display the Timestamp
print("Timestamp...\n",timestamp)
# Create the BusinessDay Offset
# BusinessDay is the DateOffset subclass
bdOffset = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(hours = 8, minutes = 10))
# Display the BusinessDay Offset
print("\nBusinessDay Offset...\n",bdOffset)
# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + bdOffset)
# return the frequency applied on the given BusinessDay object as a string
print("\nFrequency on the given BusinessDay Offset...\n",bdOffset.freqstr)
# return the rule code of the frequency applied on the given BusinessDay Offset
print("\nThe rule code of the BusinessDay object..\n", bdOffset.rule_code) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
Timestamp... 2021-10-30 01:55:02.000045 BusinessDay Offset... <BusinessDay: offset=datetime.timedelta(seconds=29400)> Updated Timestamp... 2021-11-01 10:05:02.000045 Frequency on the given BusinessDay Offset... B+8H10Min The rule code of the BusinessDay object.. B