当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Pandas Series.dt.days_in_month用法及代码示例


Series.dt可用于以datetimelike的形式访问序列的值并返回几个属性。 Pandas Series.dt.days_in_month属性返回给定系列对象在月份中的天数。

用法: Series.dt.days_in_month

参数:没有


返回:numpy数组

范例1:采用Series.dt.days_in_month属性以查找系列对象中给定日期的月份中的天数。

# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series(['2012-12-31', '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)

输出:

现在我们将使用Series.dt.days_in_month属性以查找给定日期的月份中的天数。

# find the number of  
# days in the month 
result = sr.dt.days_in_month 
  
# print the result 
print(result)

输出:

正如我们在输出中看到的,Series.dt.days_in_month属性已成功访问并返回给定日期的月份中的天数。

范例2:采用Series.dt.days_in_month属性以查找系列对象中给定日期的月份中的天数。

# importing pandas as pd 
import pandas as pd 
  
# Creating the Series 
sr = pd.Series(pd.date_range('2012-12-31 00:00', periods = 5, freq = 'D')) 
  
# 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.days_in_month属性以查找给定日期的月份中的天数。

# find the number of  
# days in the month 
result = sr.dt.days_in_month 
  
# print the result 
print(result)

输出:

正如我们在输出中看到的,Series.dt.days_in_month属性已成功访问并返回给定日期的月份中的天数。



相关用法


注:本文由纯净天空筛选整理自Shubham__Ranjan大神的英文原创作品 Python | Pandas Series.dt.days_in_month。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。