Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python Pandas - DateOffset 생성 및 날짜 증가

<시간/>

DateOffset을 만들려면 DateOffset()을 사용하세요. Pandas의 메소드. Increment 값을 인수로 설정합니다.

먼저 필요한 라이브러리를 가져옵니다 -

from pandas.tseries.offsets import DateOffset
import pandas as pd

Pandas에서 타임스탬프 개체 설정 -

timestamp = pd.Timestamp('2021-09-11 02:30:55')

날짜 증분에 대한 DateOffset입니다. 여기서 "months" 매개변수를 사용하여 월을 증가시킵니다. −

print("DateOffset...\n",timestamp + DateOffset(months=2))

예시

다음은 코드입니다 -

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))

출력

이것은 다음 코드를 생성합니다 -

Timestamp...
2021-09-11 02:30:55
DateOffset...
2021-11-11 02:30:55