Để tạo DateOffset, hãy sử dụng DateOffset () trong Pandas. Đặt giá trị Tăng làm đối số.
Đầ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-09-11 02:30:55')
DateOffset để tăng ngày. Chúng tôi đang tăng số tháng ở đây bằng cách sử dụng thông số "months" -
print("DateOffset...\n",timestamp + DateOffset(months=2)) 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-09-11 02:30:55')
# Display the Timestamp
print("Timestamp...\n",timestamp)
# DateOffset for date increment
# We are incrementing the months here using the "months" parameter
print("DateOffset...\n",timestamp + DateOffset(months=2)) Đầu ra
Điều này sẽ tạo ra mã sau -
Timestamp... 2021-09-11 02:30:55 DateOffset... 2021-11-11 02:30:55