Để trả về mã quy tắc được áp dụng trên đối tượng DateOffset đã cho, hãy sử dụng offset.rule_code trong Pandas. Đầu tiên, hãy nhập các thư viện được yêu cầu -
from pandas.tseries.frequencies import to_offset import pandas as pd
Đặt đối tượng dấu thời gian trong Pandas -
timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')
Tạo DateOffset. Chúng tôi đang tăng các tháng ở đây bằng cách sử dụng tần suất "M" -
offset = to_offset("3M")
Hiển thị Dấu thời gian đã cập nhật -
print("\nUpdated Timestamp...\n",timestamp + offset)
Trả lại mã quy tắc của tần suất được áp dụng trên đối tượng DateOffset đã cho -
print("\nThe rule code of the DateOffset object..\n", offset.rule_code)
Ví dụ
Sau đây là mã -
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("3M") # Display the DateOffset print("\nDateOffset...\n",offset) # Display the Updated Timestamp print("\nUpdated Timestamp...\n",timestamp + offset) # return the rule code of the frequency applied on the given DateOffset object print("\nThe rule code of the DateOffset object..\n", offset.rule_code)
Đầu ra
Điều này sẽ tạo ra mã sau -
Timestamp... 2021-09-26 03:25:02.000045 DateOffset... <3 * MonthEnds> Updated Timestamp... 2021-11-30 03:25:02.000045 The rule code of the DateOffset object.. M