Pandas strftime – Python Pandas Period.strftime() Function

Pandas Period.strftime() Function:

Pandas strftime: Depending on the chosen format, Period.strftime() Function of the pandas module returns a string representation of the Period. The format must be a string that contains one or more directives.

The method supports the same directives as the standard Python distribution’s time.strftime() function, as well as the particular additional directives %f, %F, %q (formatting and documentation(docs) adapted from scikits.timeries)

Syntax:

Period.strftime(format)

Parameters:

format: It is the format that the period object must be represented.

Return Value:

A string representation of the Period object is returned by the Period.strftime() Function.

NOTE:

Here,

%b – month name

%d – day of the month

%Y – year

%A – weekday

Pandas Period.strftime() Function in Python

Example1

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
  • Print the given period object
  • Pass some random format as an argument to the strftime() function to get a string representation of the given Period object in the given format.
  • Here, %b – month name, %d – day of the month, %Y – year, %A – weekday.
  • 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
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 some random format as an argument to the strftime() function
# to get a string representation of the given Period object in the given format.
# Here, %b - month name, %d - day of the month, %Y - year, %A - weekday
print("The Period object in the given format:")
period_obj.strftime('%b. %d, %Y was a %A')

Output:

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

The Period object in the given format:
'May. 16, 2015 was a Saturday'

Example2

Approach:

  • Import pandas module using the import keyword.
  • Pass some random frequency(here D= Daily frequency), year, month, day, hour, minute, second as the arguments to the Period() function of the pandas module to get the Period object
  • Print the given period object
  • Pass some random format as an argument to the strftime() function to get a string representation of the given Period object in the given format.
  • Here, %b – month name, %d – day of the month, %Y – year, %A – weekday.
  • 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 D= Daily frequency), year, month, day,
# hour, minute, second as the arguments to the Period() function
# of the pandas module to get the Period object
period_obj = pd.Period(freq ='D', year = 2021, month = 7, day = 10,
                         hour = 15, minute = 13, second = 33)
  
# Print the given period object
print("The given period object:")
print(period_obj)
print()

# Pass some random format as an argument to the strftime() function
# to get a string representation of the given Period object in the given format.
# Here, %b - month name, %d - day of the month, %Y - year, %A - weekday
print("The Period object in the given format:")
period_obj.strftime('%d-%b-%Y')

Output:

The given period object:
2021-07-10

The Period object in the given format:
'10-Jul-2021'