หากต้องการคืนค่าจำนวนที่เพิ่มขึ้นที่ใช้กับวัตถุ DateOffset ที่กำหนด ให้ใช้คุณสมบัติ offset.n ใน Pandas ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
from pandas.tseries.frequencies import to_offset import pandas as pd
ตั้งค่าอ็อบเจ็กต์ประทับเวลาใน Pandas -
timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')
สร้าง DateOffset เรากำลังเพิ่มเดือนที่นี่โดยใช้ความถี่ "M" -
offset = to_offset("5M") แสดงการประทับเวลาที่อัปเดต -
print("\nUpdated Timestamp...\n",timestamp + offset)
ส่งกลับจำนวนที่เพิ่มขึ้นบนวัตถุ DateOffset ที่กำหนด -
print("\nThe count of increments on the DateOffset object..\n", offset.n) ตัวอย่าง
ต่อไปนี้เป็นรหัส -
from pandas.tseries.frequencies import to_offset
import pandas as pd
# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')
# Display the Timestamp
print("Timestamp...\n",timestamp)
# Create the DateOffset
# We are incrementing the months here using the "M" frequency
offset = to_offset("5M")
# Display the DateOffset
print("\nDateOffset...\n",offset)
# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + offset)
# return the count of increments on the given DateOffset object
print("\nThe count of increments on the DateOffset object..\n", offset.n) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
Timestamp... 2021-09-26 03:25:02.000045 DateOffset... <5 * MonthEnds> Updated Timestamp... 2022-01-31 03:25:02.000045 The count of increments on the DateOffset object.. 5