Calendar.asof#

Calendar.asof(date: date, side: str = 'left', default='raise') date#

Returns the date if the date is in the calendar, or the last (first) date before (after) that.

Parameters:
  • date (datetime.date) – the lookup date

  • side ('left', 'right') – direction to search if date is not in calendar

  • default (optional) – default value if the given date is strictly before (after) the first (last) date in the calendar

Returns:

the last (first) date on or before (after) date

Return type:

datetime.date

Raises:

KeyError – if date is before (after) the first (last) date in the calendar, and no default is provided

Example

>>> import datetime

>>> calendar = Calendar([
...     datetime.date(2020, 1, 20),
...     datetime.date(2020, 4, 28)
... ])

>>> calendar.asof(datetime.date(2020, 1, 20))
datetime.date(2020, 1, 20)

>>> calendar.asof(datetime.date(2020, 2, 15))
datetime.date(2020, 1, 20)

>>> calendar.asof(datetime.date(2020, 2, 15), side="right")
datetime.date(2020, 4, 28)

>>> calendar.asof(datetime.date(2020, 1, 1))
KeyError("Out-of-range error: 2020-01-01 is before first date in the calendar")

>>> calendar.asof(datetime.date(2020, 1, 1), default=None)
None

See also

Calendar.lb

last date strictly before

Calendar.fa

first date strictly after