Python | Панды DatetimeIndex.month_name ()

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

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

Pandas DatetimeIndex.month_name() function return the month names of the DateTimeIndex with specified locale. The default locale is None in which case the names are returned in English language.

Syntax: DatetimeIndex.month_name(locale=None)

Parameters :
locale : locale determining the language in which to return the month name

Return : Index of month names

Example #1: Use DatetimeIndex.month_name() function to return the month name of each entry in the DatetimeIndex object. Return the names of the month in French locale

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here "Q" represents quarterly frequency 
didx = pd.DatetimeIndex(start ="2018-11-15 09:45:10", freq ="Q", periods = 5)
  
# Print the DatetimeIndex
print(didx)

Выход :

Now we want to return the names of the month in French locale.

# return the names of the month in French
didx.month_name(locale ="French")

Output :

As we can see in the output, the function has returned an Index object containing the names of the month in French.
 
Example #2: Use DatetimeIndex.month_name() function to return the month name of each entry in the DatetimeIndex object. Return the names of the month in German locale

# importing pandas as pd
import pandas as pd
  
# Create the DatetimeIndex
# Here "M" represents monthly frequency 
didx = pd.DatetimeIndex(start ="2015-03-02", freq ="M", periods = 5)
  
# Print the DatetimeIndex
print(didx)

Выход :

Now we want to return the names of the month in German locale.

# return the names of the month in German
didx.month_name(locale ="German")

Выход :

Как видно из выходных данных, функция вернула объект Index, содержащий названия месяцев в немецком языке.

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

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