Series.dt可用于以datetimelike的形式访问序列的值并返回几个属性。 Pandas  Series.dt.weekofyear属性返回一个numpy数组,该数组包含给定系列对象的基础数据中一年中的星期序数。
用法: Series.dt.weekofyear
参数:没有
返回:numpy数组
范例1:采用Series.dt.weekofyear属性,以在给定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)输出:

现在我们将使用Series.dt.weekofyear属性,以在给定Series对象的基础数据中返回一年中的星期几。
# return the week ordinal 
# of the year 
result = sr.dt.weekofyear 
  
# print the result 
print(result)输出:

正如我们在输出中看到的,Series.dt.weekofyear属性已成功访问并返回给定系列对象的基础数据中的年份的第几周。
范例2:采用Series.dt.weekofyear属性,以在给定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)输出:

现在我们将使用Series.dt.weekofyear属性,以在给定Series对象的基础数据中返回一年中的星期几。
# return the week ordinal 
# of the year 
result = sr.dt.weekofyear 
  
# print the result 
print(result)输出:

正如我们在输出中看到的,Series.dt.weekofyear属性已成功访问并返回给定系列对象的基础数据中的年份的第几周。
相关用法
- Python pandas.map()用法及代码示例
- Python Pandas Series.str.len()用法及代码示例
- Python Pandas.factorize()用法及代码示例
- Python Pandas TimedeltaIndex.name用法及代码示例
- Python Pandas dataframe.ne()用法及代码示例
- Python Pandas Series.between()用法及代码示例
- Python Pandas DataFrame.where()用法及代码示例
- Python Pandas Series.add()用法及代码示例
- Python Pandas.pivot_table()用法及代码示例
- Python Pandas Series.mod()用法及代码示例
- Python Pandas Dataframe.at[ ]用法及代码示例
- Python Pandas Dataframe.iat[ ]用法及代码示例
- Python Pandas.pivot()用法及代码示例
- Python Pandas dataframe.mul()用法及代码示例
- Python Pandas.melt()用法及代码示例
注:本文由纯净天空筛选整理自Shubham__Ranjan大神的英文原创作品 Python | Pandas Series.dt.weekofyear。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
