Python | Панды Period.strftime

Опубликовано: 28 Марта, 2022

Python - отличный язык для анализа данных, в первую очередь из-за фантастической экосистемы пакетов Python, ориентированных на данные. Pandas - один из таких пакетов, который значительно упрощает импорт и анализ данных.

Pandas Period.strftime() function returns the string representation of the Period, depending on the selected format. format must be a string containing one or several directives. The method recognizes the same directives as the time.strftime() function of the standard Python distribution, as well as the specific additional directives %f, %F, %q. (formatting & docs originally from scikits.timeries)

Syntax : Period.strftime()

Parameters : format

Return : string

Example #1: Use Period.strftime() function to return the value of the given period in a specified format.

# importing pandas as pd
import pandas as pd
  
# Create the Period object
prd = pd.Period(freq ="S", year = 2000, month = 2, day = 22
                        hour = 8, minute = 21, second = 24)
  
# Print the Period object
print(prd)

Выход :

Now we will use the Period.strftime() function to return the given period in (‘%b. %d, %Y was a %A’) format.

Note : "%b" is month name, %d is day of the month, %Y is year and %A is weekday name.

# return the period in specified format.
prd.strftime("% b. % d, % Y was a % A")

Выход :

As we can see in the output, the Period.strftime() function has returned the value of the given period in the specified format.
 
Example #2: Use Period.strftime() function to return the value of the given period in a specified format.

Выход :

Now we will use the Period.strftime() function to return the given period in (‘%b-%Y’) format.

Note : "%b" is month name, %Y is year

# return the period in specified format.
prd.strftime("% b-% Y")

Выход :

As we can see in the output, the Period.strftime() function has returned the value of the given period in the specified format.

Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.

Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.