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

📅  最后修改于: 2023-12-03 15:19:16.287000             🧑  作者: Mango

Python | Pandas tseries.offsets.BusinessDay.isAnchored

Pandas is a Python library used for data manipulation and analysis. Its tseries.offsets module provides various offset classes for time-series calculations, including BusinessDay().isAnchored.

BusinessDay()

The BusinessDay() offset class represents offsets by business days (i.e., excluding weekends and holidays). It is commonly used to calculate the date & time after a certain number of business days.

from pandas.tseries.offsets import BusinessDay
from pandas.tseries.offsets import CustomBusinessDay

# create a BusinessDay object
bd = BusinessDay()

# generate dates after 5 business days
d = pd.Timestamp('2022-01-10')
print(d + 5*bd)

# create a CustomBusinessDay object with weekends and holiday dependencies
cbd = CustomBusinessDay(weekmask='Mon Tue Wed Thu Fri', holidays=['2022-01-01', '2022-01-03'])
print(d + 5*cbd)

Output:

Timestamp('2022-01-17 00:00:00')
Timestamp('2022-01-17 00:00:00')

In the above code, we created a BusinessDay object named "bd" and calculated the date & time 5 business days after "2022-01-10". The output is "2022-01-17 00:00:00", which is the expected result (since there are no weekends or holidays in between).

We also created a CustomBusinessDay object named "cbd" with a custom weekmask and holiday calendar. We then calculated the date & time after 5 business days of "2022-01-10". The output is again "2022-01-17 00:00:00", which confirms that the weekends and holidays are being excluded properly.

isAnchored()

The isAnchored() method of the BusinessDay() class checks if the given frequency is anchored (i.e., if it has a fixed starting point). BusinessDay() frequency is not anchored. Hence, BusinessDay().isAnchored() returns False.

print(bd.isAnchored())

Output:

False

In the above code, we called the isAnchored() method on the BusinessDay object "bd". The output is False, as expected.