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

Python Pandas - 기간의 시간 구성 요소 가져오기

<시간/>

기간의 시간 구성요소를 가져오려면 period.hour를 사용하세요. 재산.

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

import pandas as pd

pandas.Period는 기간을 나타냅니다. 두 개의 기간 개체 만들기 -

period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="H", year = 2021, month = 7, day = 16, hour = 2, minute = 35)

두 개의 Period 개체에서 하루 중 시간 가져오기 -

res1 = period1.hour
res2 = period2.hour

예시

다음은 코드입니다 -

import pandas as pd

# The pandas.Period represents a period of time
# creating two Period objects
period1 = pd.Period("2020-09-23 05:55:30")
period2 = pd.Period(freq="H", year = 2021, month = 7, day = 16, hour = 2, minute = 35)

# display the Period objects
print("Period1...\n", period1)
print("Period2...\n", period2)

# get the hour of the day from two Period objects
res1 = period1.hour
res2 = period2.hour

# Return the hour of the day from the two Period objects
print("\nHour of the day from the 1st Period object ...\n", res1)
print("\nHour of the day from the 2nd Period object...\n", res2)

출력

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

Period1...
2020-09-23 05:55:30

Period2...
2021-07-16 02:00

Hour of the day from the 1st Period object ...
5

Hour of the day from the 2nd Period object...
2