Python Pandas tseries.offsets.CustomBusinessHour.rollforward
Dateoffsets是一种标准的日期增量,用于Pandas中的日期范围。在我们传入的关键字args方面,它的工作方式与relativedelta完全一样。DateOffsets的工作原理如下,每个偏移量指定一组符合DateOffset的日期。例如,Bday定义这个集合为工作日(M-F)的日期集合。DateOffsets可以被创建,以将日期向前移动一个给定的有效日期数量。例如,Bday(2)可以被添加到一个日期,使其向前移动两个工作日。如果日期不是从一个有效的日期开始,首先它被移到一个有效的日期,然后创建偏移。Pandas tseries.offsets.CustomBusinessHour.rollforward()函数用于将提供的日期向前滚动到下一个偏移量,如果不在偏移量上的话。
语法: pandas.tseries.offsets.CustomBusinessHour.rollforward(dt)
参数:dt : 日期
返回: rollforward
例子#1:使用pandas.tseries.offsets.CustomBusinessHour.rollforward()函数将提供的日期向前滚动,如果它不在偏移量上。
# 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)
输出 :
现在我们将把偏移量添加到给定的时间戳对象中,以增加数据时间的值。我们还将检查给定的日期是否在偏移量上。如果不是,我们将把给定的日期向前滚动到下一个偏移量。
# Adding the offset to the given timestamp
new_timestamp = ts + cbh
# Print the updated timestamp
print(new_timestamp)
# roll forward if not on offset
result = cbh.rollforward(pd.Timestamp('2019-4-28 11:15:00'))
# print the result
print(result)
输出 :
我们可以在输出中看到,我们已经成功创建了一个偏移量,并将其添加到给定的时间戳。我们还将日期向前滚动到下一个偏移量。
示例#2:使用pandas.tseries.offsets.CustomBusinessHour.rollforward()函数将提供的日期向前滚动,如果它不在偏移量上。
# 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)
输出 :
现在我们将在给定的时间戳对象上添加偏移量来增加数据时间的值。我们还将检查给定的日期是否在偏移量上。如果不是,我们将把给定的日期向前滚动到下一个偏移量。
# Adding the offset to the given timestamp
new_timestamp = ts + cbh
# Print the updated timestamp
print(new_timestamp)
# roll forward if not on offset
result = cbh.rollforward(pd.Timestamp('2019-4-28 11:15:00'))
# print the result
print(result)
输出 :
我们可以在输出中看到,我们已经成功创建了一个偏移量,并将其添加到给定的时间戳。我们还将日期向前滚动到下一个偏移量。