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

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

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