Python Pandas Series.dt.dayofyear

Python Pandas Series.dt.dayofyear

Series.dt可以用来访问系列的数据时间值,并返回几个属性。Pandas Series.dt.dayofyear属性返回给定系列对象中基于数据时间的底层数据中的年的序数日。

语法: Series.dt.dayofyear

参数:

返回 : numpy数组

示例#1:使用Series.dt.dayofyear属性来返回给定Series对象的底层数据中的年的序号。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(['2012-10-21 09:30', '2019-7-18 12:30', '2008-02-2 10:30',
                '2010-4-22 09:25', '2019-11-8 02:22'])
  
# 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.dayofyear

现在我们将使用Series.dt.dayofyear属性来返回给定系列对象中基于日期时间的数据中的年的序号。

# return the day of year
result = sr.dt.dayofyear
  
# print the result
print(result)

输出 :
Python Pandas Series.dt.dayofyear

正如我们在输出中看到的,Series.dt.dayofyear属性已经成功访问并返回了给定系列对象的底层数据中的年月日。

示例#2 :使用Series.dt.dayofyear属性来返回给定Series对象的底层数据中的年的序号。

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series(pd.date_range('2012-12-12 12:12', 
                       periods = 5, freq = 'M'))
  
# 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.dayofyear

现在我们将使用Series.dt.dayofyear属性来返回给定系列对象中基于日期时间的数据中的年的序号。

# return the day of year
result = sr.dt.dayofyear
  
# print the result
print(result)

输出 :
Python Pandas Series.dt.dayofyear
正如我们在输出中看到的,Series.dt.dayofyear属性已经成功访问并返回了给定系列对象的底层数据中的年月日。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程