📌  相关文章
📜  Python| Pandas tseries.offsets.BusinessDay.isAnchored

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

Python| Pandas tseries.offsets.BusinessDay.isAnchored

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

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

Pandas tseries.offsets.BusinessDay.isAnchored()函数返回一个布尔值。如果给定的工作日偏移量是锚定的,则返回True ,否则返回False

示例 #1:使用pandas.tseries.offsets.BusinessDay.isAnchored()函数来检查给定的工作日偏移对象是否被锚定。

# importing pandas as pd
import pandas as pd
  
# Creating Timestamp
ts = pd.Timestamp('2019-10-10 07:15:11')
  
# Create an offset of 5 Business days
bd = pd.tseries.offsets.BusinessDay(n = 5)
  
# Print the Timestamp
print(ts)
  
# Print the DateOffset
print(bd)

输出 :

现在我们将工作日偏移量添加到给定的时间戳对象以增加日期时间值。我们还将检查给定的工作日偏移对象是否已锚定。

# Adding the Business day offset to the given timestamp
new_timestamp = ts + bd
  
# Print the updated timestamp
print(new_timestamp)
  
# check if the offset is 
# anchored or not
result = bd.isAnchored()
  
# print the result
print(result)

输出 :

正如我们在输出中看到的,我们成功创建了 5 个工作日的偏移量并将其添加到给定的时间戳中。给定函数返回False表示给定偏移对象未锚定。

示例 #2:使用pandas.tseries.offsets.BusinessDay.isAnchored()函数来检查给定的工作日偏移对象是否被锚定。

# importing pandas as pd
import pandas as pd
  
# Creating Timestamp
ts = pd.Timestamp('2019-10-10 07:15:11')
  
# Create an offset of 10 Business days and 10 hours
bd = pd.tseries.offsets.BusinessDay(offset = datetime.timedelta(days = 10, hours = 10))
  
# Print the Timestamp
print(ts)
  
# Print the DateOffset
print(bd)

输出 :

现在我们将工作日偏移量添加到给定的时间戳对象以增加日期时间值。我们还将检查给定的工作日偏移对象是否已锚定。

# Adding the Business day offset to the given timestamp
new_timestamp = ts + bd
  
# Print the updated timestamp
print(new_timestamp)
  
# check if the offset is 
# anchored or not
result = bd.isAnchored()
  
# print the result
print(result)

输出 :

正如我们在输出中看到的,我们成功创建了 10 个工作日和 10 小时的偏移量,并将其添加到给定的时间戳中。给定函数返回True表示给定偏移对象已锚定。