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

Python Pandas - 주어진 기간 객체의 빈도를 초에서 시간별 빈도로 변경

<시간/>

주어진 Period 객체의 빈도를 Seconds에서 Hourly 빈도로 변경하려면 period.asfreq() 메서드를 사용하고 매개변수 'H'를 설정합니다.

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

import pandas as pd

pandas.Period는 기간을 나타냅니다. 기간 개체 만들기. 주파수를 초로 설정했습니다. 'freq' 매개변수를 사용하는 'S'

period = pd.Period(freq="S", year = 2021, month = 4, day = 16, hour = 2, minute = 35, second = 15)

초 빈도로 기간 개체 표시

print("Period...\n", period)

기간을 초에서 시간당 빈도로 변환합니다. asfreq()를 사용하여 초를 시간당 빈도로 변환하도록 "H"를 설정했습니다.

res = period.asfreq('H')

예시

다음은 코드입니다.

import pandas as pd

# The pandas.Period represents a period of time
# Creating a Period object
# We have set the frequency as seconds ie. 'S' using the 'freq' parameter
period = pd.Period(freq="S", year = 2021, month = 4, day = 16, hour = 2, minute = 35, second = 15)

# display the Period object with Seconds frequency
print("Period...\n", period)

# Convert Period from Seconds to Hourly frequency
# We have set the "H" to convert seconds to hourly frequency using asfreq()
res = period.asfreq('H')

# display the result after conversion from Seconds to hourly frequency
print("\nFinal result after converting frequency ...\n", res)

출력

그러면 다음 코드가 생성됩니다.

Period...
2021-04-16 02:35:15

Final result after converting frequency ...
2021-04-16 02:00