Pandas asfreq – Python Pandas Period.asfreq() Function

Pandas Period.asfreq() Function:

Asfreq pandas: The Period.asfreq() function of the Pandas module converts the Period object to the specified frequency, either at the start or end of the interval.

Syntax:

 Period.asfreq()

Parameters:

freq: It is a string. This is the frequency given as input. It may be S(secondly), D(Day), T(Minutely), H(Hourly), etc.

how: This is optional. It specifies whether the Start or end of the timespan

Return Value:

The Period Object with the change in the frequency is returned by the Period.asfreq() function

Pandas Period.asfreq() Function in Python

Example1

Pandas asfreq: Here, it changes the frequency from S(secondly frequency) to D(Daily frequency) of the period object

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here S= secondly frequency), year, month, day, hour, minute, second as the arguments to the Period() function of the pandas module to get the Period object.
  • Store it in a variable.
  • Print the given period object.
  • Pass freq =’D’ as an argument to the asfreq() function to change the frequency of the given Period object.
  • Here, it changes the frequency from S(secondly frequency) to D(Daily frequency).
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random frequency(here S= secondly frequency), year, month, day,
# hour, minute, second as the arguments to the Period() function
# of the pandas module to get the Period object
# Store it in a variable.
period_obj = pd.Period(freq ='S', year = 2015, month = 5, day = 16,
                         hour = 12, minute = 28, second = 10)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Pass freq ='D' as an argument to the asfreq() function
# to change the frequency of the given Period object.
# Here, it changes the frequency from S(secondly frequency) to D(Daily frequency)
print("Changing the frequency from secondly(S) to Daily(D) frequency:")
period_obj.asfreq(freq ='D')

Output:

The given period object:
2015-05-16 12:28:10

Changing the frequency from secondly(S) to Daily(D) frequency:
Period('2015-05-16', 'D')

Example2

Here, it changes the frequency from H(Hourly frequency) to S(Secondly frequency) of the period object

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here H= Hourly frequency), year, month, day, hour, minute, second as the arguments to the Period() function of the pandas module to get the Period object
  • Store it in a variable.
  • Print the given period object
  • Pass freq =’S’ as an argument to the asfreq() function to change the frequency of the given Period object.
  • Here, it changes the frequency from H(Hourly frequency) to S(Secondly frequency).
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword.
import pandas as pd
# Pass some random frequency(here H= Hourly frequency), year, month, day,
# hour, minute, second as the arguments to the Period() function
# of the pandas module to get the Period object
# Store it in a variable.
period_obj = pd.Period(freq ='H', year = 2019, month = 3, day = 14,
                         hour = 6, minute = 15, second = 18)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()
# Pass freq ='S' as an argument to the asfreq() function
# to change the frequency of the given Period object.
# Here, it changes the frequency from H(Hourly frequency) to S(Secondly frequency)
print("Changing the frequency from Hourly(H) to Secondly(S) frequency:")
period_obj.asfreq(freq ='S')

Output:

The given period object:
2019-03-14 06:00

Changing the frequency from Hourly(H) to Secondly(S) frequency:
Period('2019-03-14 06:59:59', 'S')