ในการส่งคืนความถี่ที่ใช้กับวัตถุ DateOffset ที่ระบุเป็นสตริง ให้ใช้ offset.freqstr ทรัพย์สินในหมีแพนด้า
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
from pandas.tseries.offsets import DateOffset import pandas as pd
ตั้งค่าอ็อบเจ็กต์ประทับเวลาใน Pandas -
timestamp = pd.Timestamp('2021-08-30 02:30:55')
สร้าง DateOffset เรากำลังเพิ่มเดือนที่นี่โดยใช้พารามิเตอร์ "เดือน" -
offset = pd.tseries.offsets.DateOffset(months=3)
แสดงการประทับเวลาที่อัปเดต -
print("\nUpdated Timestamp...\n",timestamp + offset)
ความถี่ที่ใช้กับวัตถุ DateOffset ที่กำหนดเป็นสตริง -
print("\nFrequency on the given DataOffset...\n",offset.freqstr) ตัวอย่าง
ต่อไปนี้เป็นรหัส -
from pandas.tseries.offsets import DateOffset
import pandas as pd
# Set the timestamp object in Pandas
timestamp = pd.Timestamp('2021-08-30 02:30:55')
# Display the Timestamp
print("Timestamp...\n",timestamp)
# Create the DateOffset
# We are incrementing the months here using the "months" parameter
offset = pd.tseries.offsets.DateOffset(months=3)
# Display the DateOffset
print("\nDateOffset...\n",offset)
# Display the Updated Timestamp
print("\nUpdated Timestamp...\n",timestamp + offset)
# frequency applied on the given DateOffset object as a string
print("\nFrequency on the given DataOffset...\n",offset.freqstr) ผลลัพธ์
สิ่งนี้จะสร้างรหัสต่อไปนี้ -
Timestamp... 2021-08-30 02:30:55 DateOffset... <DateOffset: months=3> Updated Timestamp... 2021-11-30 02:30:55 Frequency on the given DataOffset... <DateOffset: months=3>