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

Python Pandas - 주어진 DateOffset 객체에 적용된 규칙 코드를 반환합니다.

<시간/>

주어진 DateOffset 객체에 적용된 규칙 코드를 반환하려면 Pandas에서 offset.rule_code를 사용하세요. 먼저 필요한 라이브러리를 가져옵니다 -

from pandas.tseries.frequencies import to_offset
import pandas as pd

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

timestamp = pd.Timestamp('2021-09-26 03:25:02.000045')

DateOffset을 만듭니다. 여기에서 "M" 빈도를 사용하여 월을 증가시킵니다. −

offset = to_offset("3M")

업데이트된 타임스탬프 표시 -

print("\nUpdated Timestamp...\n",timestamp + offset)

주어진 DateOffset 객체에 적용된 빈도의 규칙 코드를 반환 -

print("\nThe rule code of the DateOffset object..\n", offset.rule_code)

예시

다음은 코드입니다 -

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)

출력

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

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