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

PythonServer Side ProgrammingProgramming

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

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 Daily frequency. We have set the "D" to convert seconds to daily frequency using asfreq()

res = period.asfreq('D')

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 Daily frequency
# We have set the "D" to convert seconds to daily frequency using asfreq()
res = period.asfreq('D')

# display the result after conversion from Seconds to Daily 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
raja
Updated on 20-Oct-2021 06:39:16

Advertisements