tdfs4ds.utils.calendar¶
tdfs4ds.utils.calendar
¶
Calendar rules for a TimeManager (0.3.7.31). Pure: no database access.
A :class:Calendar is the rule that produced the dates of a TimeManager (monthly on the last
day, every Friday, every 15 minutes, any pandas offset ...). With a rule the dates can be
generated when the manager is created and appended later, up to now.
Two notions are kept apart:
- the business date (the step value, the valid time of the features) is what the rule produces; it is never shifted;
dueis the delay after the business date when the data is available. It only decides when a step may be added (due_time); it does not move the date.
_WEEKDAY_NAMES
module-attribute
¶
_WEEKDAY_NAMES = {'MONDAY': 0, 'TUESDAY': 1, 'WEDNESDAY': 2, 'THURSDAY': 3, 'FRIDAY': 4, 'SATURDAY': 5, 'SUNDAY': 6}
_UNITS
module-attribute
¶
Calendar
¶
A rule giving the business dates of a TimeManager. Build it with :meth:monthly,
:meth:weekly, :meth:daily, :meth:every or :meth:from_pandas.
__init__
¶
__init__(kind, *, day=None, weekday=None, at='00:00', interval=None, alias=None, anchor=None, due=None, tz='UTC', dtype=None)
monthly
classmethod
¶
One step a month on day (1..31, clamped to the month length like ADD_MONTHS) or 'last'.
every
classmethod
¶
A fixed interval ('15min', '1h', '2d' or a timedelta), anchored on the start date (or on anchor).
from_pandas
classmethod
¶
Any pandas offset alias ('W-FRI', 'MS', 'ME' ...).
steps
¶
Business dates in [start, until] (both included), tz-aware in the rule's zone.
previous_before
¶
The business date one step BEFORE date (0.3.7.31): the first date a previous-date view
returns for the first stored date. Monthly: the same rule in the month before (31 Jan ->
31 Dec for 'last'); weekly: 7 days earlier; daily: 1 day; every: one interval; pandas
alias: one offset back. date need not be a step of the rule for every / pandas.
step
¶
The rule's step as (unit, n): ('months', 1), ('days', 7), ('days', 1) or
('seconds', n) for every; ('unknown', 0) for a pandas alias. Pure.
to_code
¶
Python text rebuilding this rule: tdfs4ds.Calendar.monthly(day='last', ...).
due_time
¶
When the data of business_date is available (the date itself when no due).
fits
¶
Whether date is one of the rule's business dates (a naive date is read in the rule's zone).
_parse_due
¶
Normalised due text: '+3d 06:00' (days later, at a time of day) or '+2h'; None = at the date.
infer_calendar
¶
Infer ONE rule from existing dates: (Calendar, reason) or (None, reason).
A rule is returned only when every date fits it and only one rule fits: a fixed interval,
weekly on one weekday, monthly on one day, monthly on the last day (31 Jan, 29 Feb, 31 Mar
read as 'last'). Fewer than 2 dates, irregular dates or an ambiguity give None and
the reason. The time of day and the zone of the dates are kept; naive dates read as UTC.
calendar_state
¶
calendar_state(dates, stored=None, view: str = '<view>', database: str = '<db>', max_listed: int = 5) -> dict
The calendar state of one table of dates. Pure.
dates: the stored dates (any order); stored: the JSON of the stored rule, or None.
Returns {state, rule, code, kind, detail, fix, off_rule, count} with state one of
stored (a rule is stored and every date fits), stored_off_rule (a rule is stored and some dates
do not fit), inferable (no rule stored; infer_calendar finds one), irregular (no rule stored,
none can be inferred), unknown (fewer than 2 dates, or an unreadable stored rule). kind is the
finding the state calls for (calendar_rule_inferable info, calendar_irregular info,
calendar_off_rule_dates warning) or None.