Python Pandas - Get the frequency for the given Period object

PythonPandasServer Side ProgrammingProgramming

To get the frequency for the given Period object, use the period.freq property.

At first, import the required libraries −

import pandas as pd

The pandas.Period represents a period of time. Creating two Period objects −

period1 = pd.Period("2020-09-23 03:55:20")
period2 = pd.Period(freq="T", year = 2021, month = 2, day = 14, hour = 2, minute = 35)

Display the Period objects −

print("Period1...\n", period1)
print("Period2...\n", period2)

Get the frequency from two Period objects −

res1 = period1.freq
res2 = period2.freq

Example

Following is the code −

import pandas as pd

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

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

# get the frequency from two Period objects
res1 = period1.freq
res2 = period2.freq

# Return the frequency from two Period objects
print("\nDisplay the frequency from the 1st Period object ...\n", res1)
print("\nDisplay the frequency from the 2nd Period object ...\n", res2)

Output

This will produce the following code −

Period1...
2020-09-23 03:55:20
Period2...
2021-02-14 02:35

Display the frequency from the 1st Period object ...
<Second>

Display the frequency from the 2nd Period object ...
<Minute>
raja
Updated on 14-Oct-2021 06:57:23

Advertisements