Python Pandas - Change the frequency of the given Period object from Seconds to Hourly frequency


To change the frequency of the given Period object from Seconds to Hourly frequency, use the period.asfreq() method and set the parameter ‘H’.

At first, import the required libraries −

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

Example

Following is the code

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)

Output

This will produce the following code

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

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

Updated on: 20-Oct-2021

614 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements