Python Pandas Series.dt.day_name

Python Pandas Series.dt.day_name

Series.dt可以用来访问系列的数据值,并返回几个属性。Pandas Series.dt.day_name()函数返回指定地区的DateTimeIndex的日名。

语法: Series.dt.day_name(*args, **kwargs)

参数:
locale : 语言决定了返回日期名称的语言。默认为英语。

返回:日名索引。

示例#1:使用Series.dt.day_name()函数返回给定系列对象中基础日期时间数据的日期名称。返回英文的日期名称。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['2012-12-31 08:45', '2019-1-1 12:30', '2008-02-2 10:30',
                '2010-1-1 09:25', '2019-12-31 00:00'])
  
# Creating the index
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
  
# set the index
sr.index = idx
  
# Convert the underlying data to datetime 
sr = pd.to_datetime(sr)
  
# Print the series
print(sr)

输出 :

Python Pandas Series.dt.day_name

现在我们将使用Series.dt.day_name()函数来返回给定系列对象中每个时间戳的日期名称。

# return day name in english
result = sr.dt.day_name(locale = 'English')
  
# print the result
print(result)

输出 :

Python Pandas Series.dt.day_name

正如我们在输出中看到的,Series.dt.day_name()函数已经成功地返回了指定语言中的日期名称。

示例#2 :使用Series.dt.day_name()函数来返回给定系列对象中底层日期时间数据的日期名称。返回法语中的日名。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(pd.date_range('2012-12-31 09:45', periods = 5, freq = 'Q',
                            tz = 'Europe / Berlin'))
  
# Creating the index
idx = ['Day 1', 'Day 2', 'Day 3', 'Day 4', 'Day 5']
  
# set the index
sr.index = idx
  
# Print the series
print(sr)

输出 :

Python Pandas Series.dt.day_name

现在我们将使用Series.dt.day_name()函数来返回给定系列对象中每个时间戳的日期名称。

# return day name in french
result = sr.dt.day_name(locale = 'French')
  
# print the result
print(result)

输出 :

Python Pandas Series.dt.day_name

正如我们在输出中看到的,Series.dt.day_name()函数已经成功地返回了指定语言中的日期名称。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程