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


Python Pandas tseries.offsets.CustomBusinessHour用法及代码示例

Dateoffsets是用于Pandas中日期范围的标准日期增量。就我们传入的关键字args而言,它的工作方式与relativedelta完全相同。DateOffets的工作方式如下,每个偏移量指定一组符合DateOffset的日期。例如,“ Bday”将此集合定义为工作日的日期集合(M-F)。

可以创建DateOffsets以将日期向前移动给定数量的有效日期。例如,可以将Bday(2)添加到日期以将其向前移动两个工作日。如果日期不是从有效日期开始,则首先将其移动到有效日期,然后创建偏移量。

Pandas tseries.offsets.CustomBusinessHour()函数用于创建您自己的自定义营业时间。 DateOffset子类,可能表示n个自定义工作日。


用法: pandas.tseries.offsets.CustomBusinessHour()

参数:
n:整型
normalize:将开始/结束日期标准化为午夜,然后再生成日期范围
weekmask:有效工作日的周掩码,已传递给numpy.busdaycalendar
holidays:没有
calendar:没有
start:营业时间开始
end:营业时间结束
offset:datetime.timedelta(0)

返回:抵销

范例1:采用pandas.tseries.offsets.CustomBusinessHour()函数来创建您自己的自定义营业时间偏移量。

# importing pandas as pd 
import pandas as pd 
  
# Creating Timestamp 
ts = pd.Timestamp('2019-4-23 11:15:00') 
  
# Create an offset 
cbh = pd.tseries.offsets.CustomBusinessHour(n = 2, weekmask = 'Mon Tue Wed Thu', 
                                           start ='12:00') 
  
# Print the Timestamp 
print(ts) 
  
# Print the Offset 
print(cbh)

输出:

现在,我们将偏移量添加到给定的timestamp对象中,以增加datetime值。

# Adding the offset to the given timestamp 
new_timestamp = ts + cbh 
  
# Print the updated timestamp 
print(new_timestamp)

输出:


从输出中可以看到,我们已经成功创建了一个偏移并将其添加到给定的时间戳中。

范例2:采用pandas.tseries.offsets.CustomBusinessHour()函数来创建您自己的自定义营业时间偏移量。

# importing pandas as pd 
import pandas as pd 
  
# Creating Timestamp 
ts = pd.Timestamp('2019-4-23 11:15:00') 
  
# Create an offset 
cbh = pd.tseries.offsets.CustomBusinessHour(start ='07:00', end ='14:00') 
  
# Print the Timestamp 
print(ts) 
  
# Print the Offset 
print(cbh)

输出:

现在,我们将偏移量添加到给定的timestamp对象中,以增加datetime值。

# Adding the offset to the given timestamp 
new_timestamp = ts + cbh 
  
# Print the updated timestamp 
print(new_timestamp)

输出:

从输出中可以看到,我们已经成功创建了一个偏移并将其添加到给定的时间戳中。



相关用法


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