📌  相关文章
📜  Python|熊猫 tseries.offsets.CustomBusinessHour.rollback

📅  最后修改于: 2022-05-13 01:54:40.003000             🧑  作者: Mango

Python|熊猫 tseries.offsets.CustomBusinessHour.rollback

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

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

Pandas tseries.offsets.CustomBusinessHour.rollback()函数用于将提供的日期向后滚动到下一个偏移量,前提是不在偏移量上。

示例 #1:使用pandas.tseries.offsets.CustomBusinessHour.rollback()函数将提供的日期向后滚动,如果它不是偏移量。

# 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 backward if not on offset
result = cbh.rollback(pd.Timestamp('2019-4-28 11:15:00'))
  
# print the result
print(result)

输出 :

正如我们在输出中看到的,我们已经成功创建了一个偏移量并将其添加到给定的时间戳中。我们还将日期向后滚动到下一个偏移量。

示例 #2:使用pandas.tseries.offsets.CustomBusinessHour.rollback()函数将提供的日期向后滚动,如果它不是偏移量。

# 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 backward if not on offset
result = cbh.rollback(pd.Timestamp('2019-4-28 11:15:00'))
  
# print the result
print(result)

输出 :

正如我们在输出中看到的,我们已经成功创建了一个偏移量并将其添加到给定的时间戳中。我们还将日期向后滚动到下一个偏移量。