Skip to content

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;
  • due is 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.

_WEEKDAYS module-attribute

_WEEKDAYS = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']

_WEEKDAY_NAMES module-attribute

_WEEKDAY_NAMES = {'MONDAY': 0, 'TUESDAY': 1, 'WEDNESDAY': 2, 'THURSDAY': 3, 'FRIDAY': 4, 'SATURDAY': 5, 'SUNDAY': 6}

_UNITS module-attribute

_UNITS = {'s': 1, 'sec': 1, 'min': 60, 'h': 3600, 'd': 86400, 'w': 604800}

_OFFSET_RE module-attribute

_OFFSET_RE = compile('^([+-])(\\d{2}):?(\\d{2})$')

CAL_INFERABLE module-attribute

CAL_INFERABLE = 'calendar_rule_inferable'

CAL_IRREGULAR module-attribute

CAL_IRREGULAR = 'calendar_irregular'

CAL_OFF_RULE module-attribute

CAL_OFF_RULE = 'calendar_off_rule_dates'

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.

kind instance-attribute

kind = kind

day instance-attribute

day = day

weekday instance-attribute

weekday = weekday

at instance-attribute

at = _parse_at(at)

interval instance-attribute

interval = interval

alias instance-attribute

alias = alias

anchor instance-attribute

anchor = None if anchor is None else Timestamp(anchor)

due instance-attribute

due = _parse_due(due)

tz instance-attribute

tz = tz if isinstance(tz, str) else str(tz)

dtype instance-attribute

dtype = dtype

__init__

__init__(kind, *, day=None, weekday=None, at='00:00', interval=None, alias=None, anchor=None, due=None, tz='UTC', dtype=None)

monthly classmethod

monthly(day=1, at='00:00', *, due=None, tz='UTC', dtype=None)

One step a month on day (1..31, clamped to the month length like ADD_MONTHS) or 'last'.

weekly classmethod

weekly(weekday='MON', at='00:00', *, due=None, tz='UTC', dtype=None)

daily classmethod

daily(at='00:00', *, due=None, tz='UTC', dtype=None)

every classmethod

every(interval, *, anchor=None, due=None, tz='UTC', dtype=None)

A fixed interval ('15min', '1h', '2d' or a timedelta), anchored on the start date (or on anchor).

from_pandas classmethod

from_pandas(alias, *, due=None, tz='UTC', dtype=None)

Any pandas offset alias ('W-FRI', 'MS', 'ME' ...).

steps

steps(start, until) -> List[pd.Timestamp]

Business dates in [start, until] (both included), tz-aware in the rule's zone.

next_after

next_after(date) -> pd.Timestamp

The first business date strictly after date.

previous_before

previous_before(date) -> pd.Timestamp

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

step() -> Tuple[str, int]

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

to_code() -> str

Python text rebuilding this rule: tdfs4ds.Calendar.monthly(day='last', ...).

due_time

due_time(business_date) -> pd.Timestamp

When the data of business_date is available (the date itself when no due).

fits

fits(date) -> bool

Whether date is one of the rule's business dates (a naive date is read in the rule's zone).

describe

describe() -> str

to_json

to_json() -> str

from_json classmethod

from_json(text) -> Calendar

__eq__

__eq__(other)

__hash__

__hash__()

__repr__

__repr__()

_parse_at

_parse_at(at) -> datetime.time

_at_text

_at_text(t: time) -> str

_parse_weekday

_parse_weekday(w) -> int

_seconds_text

_seconds_text(seconds: int) -> str

_parse_interval

_parse_interval(interval) -> datetime.timedelta

_parse_due

_parse_due(due) -> Optional[str]

Normalised due text: '+3d 06:00' (days later, at a time of day) or '+2h'; None = at the date.

_resolve_tz

_resolve_tz(tz)

infer_calendar

infer_calendar(dates) -> Tuple[Optional[Calendar], str]

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.