getdate¶
Calendar.getdate(expr, ref) resolves date expressions against month, year, or date references. It returns numpy.datetime64 for scalar calls and numpy.ndarray for vectorized calls.
The expr argument has three forms:
<position> <day>, such asfirst day,15th day,last bizday,next wed<position> <day> <before|after> <position> <day>, such asfirst day before 15th dayor1st bizday before 2nd friprevious <weekday>, such asprevious mon
Positions are next, first/1st, second/2nd, third/3rd, <n> with the right English suffix (4th, 11th, 21st, 22nd) and last. Days are day, bizday, or a weekday as a 3-letter or full English name (mon/monday … sun/sunday). Words are case-insensitive; any other word raises ValueError.
Reference rules:
year or month refs: the n-th day, business day or weekday inside the period;
lastis the last one andnextmeans the first. Composite expressions work with period refs only.date refs:
<n> dayis the date plus n days,<n> bizdayisoffset(date, n), and<n> <weekday>is the n-th such weekday strictly after the date.last(orprevious, for weekdays) gives the one strictly before.all refs in one call must be of one kind (year, month or date).
[1]:
from bizdays import Calendar
cal = Calendar.load(name="ANBIMA")
Month references use YYYY-MM strings.
[2]:
cal.getdate("15th day", "2002-05")
[2]:
np.datetime64('2002-05-15')
[3]:
cal.getdate("first day before 15th day", "2002-01")
[3]:
np.datetime64('2002-01-14')
[4]:
cal.getdate(["last day", "last bizday"], "2006")
[4]:
array(['2006-12-31', '2006-12-29'], dtype='datetime64[D]')
The last business day of a month. All refs in one call must be of one kind: mixing month and date refs raises ValueError.
[5]:
cal.getdate("last bizday", "2024-02")
[5]:
np.datetime64('2024-02-29')
Year references can be integers or year strings.
[6]:
cal.getdate("first bizday", 2002)
[6]:
np.datetime64('2002-01-02')
Date references resolve the expression relative to that day.
[7]:
cal.getdate("next wed", "2021-02-10")
[7]:
np.datetime64('2021-02-17')
[8]:
cal.getdate("previous mon", "2021-02-10")
[8]:
np.datetime64('2021-02-08')
[9]:
cal.getdate("second fri", "2026-04-20")
[9]:
np.datetime64('2026-05-01')
[10]:
cal.getdate(["next bizday", "last bizday"], "2022-06-18")
[10]:
array(['2022-06-20', '2022-06-17'], dtype='datetime64[D]')
[11]:
cal.getdate("last mon", "2022-06-20")
[11]:
np.datetime64('2022-06-13')
Both the expression and the reference can be vectorized, with refs of one kind per call.
[12]:
cal.getdate(["15th day", "16th day"], ["2002-01", "2001-01"])
[12]:
array(['2002-01-15', '2001-01-16'], dtype='datetime64[D]')
[13]:
cal.getdate(["next wed", "previous mon"], ["2021-02-10", "2021-02-10"])
[13]:
array(['2021-02-17', '2021-02-08'], dtype='datetime64[D]')