Để trả về tần suất được áp dụng trên đối tượng DateOffset đã cho dưới dạng một chuỗi, hãy sử dụng offset.freqstr tài sản ở Pandas.
Đầu tiên, hãy nhập các thư viện được yêu cầu -
from pandas.tseries.offsets import DateOffset import pandas as pd
Đặt đối tượng dấu thời gian trong Pandas -
timestamp = pd.Timestamp('2021-08-30 02:30:55')
Tạo DateOffset. Chúng tôi đang tăng số tháng ở đây bằng cách sử dụng thông số "months" -
offset = pd.tseries.offsets.DateOffset(months=3)
Hiển thị Dấu thời gian đã cập nhật -
print("\nUpdated Timestamp...\n",timestamp + offset)
Tần suất được áp dụng trên đối tượng DateOffset đã cho dưới dạng một chuỗi -
print("\nFrequency on the given DataOffset...\n",offset.freqstr) Ví dụ
Sau đây là mã -
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) Đầu ra
Điều này sẽ tạo ra mã sau -
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>