lilio.calendar_shorthandsο
Shorthands for calendars, to make generating commonly used calendars a one-liner.
Module Contentsο
- class lilio.calendar_shorthands.Calendar(anchor: str, allow_overlap: bool = False, mapping: None | _MappingYears | _MappingData | _MappingDataGreedy = None, intervals: None | list[Interval] = None)[source]ο
Build a calendar from scratch with basic construction elements.
Instantiate a basic container for building calendar using basic blocks.
This is a highly flexible calendar which allows the user to build their own calendar with the basic building blocks of target and precursor periods.
Users have the freedom to create calendar with customized intervals, gap between intervals, and even overlapped intervals. They need to manage the calendar themselves.
Some shorthand calendars, such as a daily_calendar, weekly_calendar and monthly_calendar are available in lilio.calendar_shorthands. These can be used to easily construct basic calendars with only a few parameters, but do not have the flexibility that this calendar builder module provides.
- Parameters:
anchor β
String denoting the anchor date. The following inputs are valid: - βMM-DDβ for a month and day. E.g. β12-31β. - βMMβ for only a month, e.g. β4β for March. - English names and abbreviations of months. E.g. βDecemberβ or
βjanβ.
- βWwwβ for a week number, e.g. βW05β for the fifth week of the
year.
- βWww-Dβ for a week number plus day of week. E.g. βW01-4β for the
first thursday of the year.
allow_overlap β If overlapping intervals between years is allowed or not. Default behaviour is False, which means that anchor years will be skipped to avoid data being shared between anchor years.
mapping β Calendar mapping. Input in the form: (βyearsβ, 2000, 2020) or (βdataβ, pd.Timestamp(β2000-01-01β), pd.Timestamp(β2020-01-01β)). The calendar mapping is usually set with the map_years or map_to_data methods.
intervals β A list of Interval objects that should be appended to the calendar when it is initialized.
Example
Instantiate a custom calendar and appending target/precursor periods.
>>> import lilio >>> calendar = lilio.Calendar(anchor="12-31") >>> calendar Calendar( anchor='12-31', allow_overlap=False, mapping=None, intervals=None )
- property anchorο
- Return the anchor.
- property allow_overlapο
- if overlapping intervals are allowed or not.
- Type:
Returns the allow_overlap
- property mapping: None | Literal['years', 'data', 'data-greedy']ο
Return the mapping of the calendar. Either None, βyearsβ, or βdataβ.
- add_intervals(role: Literal['target', 'precursor'], length: str, gap: str = '0d', n: int = 1) None [source]ο
Add one or more intervals to the calendar.
The interval can be a target or a precursor, and can be defined by its length, a possible gap between this interval and the preceding interval.
- Parameters:
role β Either a βtargetβ or βprecursorβ interval(s).
length β The length of the interval(s), in a format of β5dβ for five days, β2Wβ for two weeks, or β1Mβ for one month.
gap β The gap between this interval and the preceding target/precursor interval. Same format as the length argument.
n β The number of intervals which should be added to the calendar. Defaults to 1.
- map_years(start: int, end: int)[source]ο
Add a start and end year mapping to the calendar.
If the start and end years are the same, the intervals for only that single year are returned by calendar.get_intervals().
- Parameters:
start β The first year for which the calendar will be realized
end β The last year for which the calendar will be realized
- Returns:
The calendar mapped to the input start and end year.
- map_to_data(input_data: pandas.Series | pandas.DataFrame | xarray.Dataset | xarray.DataArray, safe: bool = True)[source]ο
Map the calendar to input data period.
Stores the first and last intervals of the input data to the calendar, so that the intervals can cover the data to the greatest extent.
- Parameters:
input_data β Input data for datetime mapping. Its index must be either pandas.DatetimeIndex, or an xarray time coordinate with datetime data.
safe β bool describing if data should be mapped in safe (makes sure intervals are data-filled) or greedy mode (interval created if there is any data), safe is default.
- Returns:
The calendar mapped to the input data period.
- get_intervals() pandas.DataFrame [source]ο
Retrieve updated intervals from the Calendar object.
- show() pandas.DataFrame [source]ο
Display the intervals the Calendar will generate for the current setup.
- Returns:
Dataframe containing the calendar intervals.
- Return type:
pd.Dataframe
- visualize(n_years: int = 3, interactive: bool = False, relative_dates: bool = False, show_length: bool = False, add_legend: bool = True, ax=None, **bokeh_kwargs) None [source]ο
Plot a visualization of the current calendar setup, to aid in user setup.
Note: The interactive visualization requires the bokeh package to be installed in the active Python environment.
- Parameters:
n_years β Sets the maximum number of anchor years that should be shown. By default only the most recent 3 are visualized, to ensure that they fit within the plot.
interactive β If False, matplotlib will be used for the visualization. If True, bokeh will be used.
relative_dates β Toggles if the intervals should be displayed relative to the anchor date, or as absolute dates.
show_length β Toggles if the frequency of the intervals should be displayed. Defaults to False (Matplotlib plotter only).
add_legend β Toggles if a legend should be added to the plot (Matplotlib only)
ax β Matplotlib axis object to plot the visualization into.
**bokeh_kwargs β Keyword arguments to pass to Bokehβs plotting.figure. See https://docs.bokeh.org/en/latest/docs/reference/plotting/figure.html for a list of possible keyword arguments.
- property flat: pandas.DataFrameο
Returns the flattened intervals.
- lilio.calendar_shorthands.daily_calendar(anchor: str, length: str = '1d', n_targets: int = 1, n_precursors: int = 0, allow_overlap: bool = False) lilio.calendar.Calendar [source]ο
Instantiate a basic daily calendar with minimal configuration.
Set up a quick calendar revolving around intervals with day-based lengths. The intervals will extend back in time with as many intervals as fit within the cycle time of one year.
- Parameters:
anchor β String in the form β12-31β for December 31st. The first target interval will contain the anchor, while the precursor intervals are built back in time starting at this date.
length β The length of every target and precursor period.
n_targets β integer specifying the number of target intervals in a period.
n_precursors β Sets the maximum number of precursors of the Calendar. If 0, the amount will be determined by how many fit in each anchor year. If a value is provided, the intervals can either only cover part of the year, or extend over multiple years. In case of a large max_lag number where the intervals extend over multiple years, anchor years will be skipped to avoid overlapping intervals. To allow overlapping intervals, use the allow_overlap kwarg.
allow_overlap β Allows intervals to overlap between anchor years, if the max_lag is set to a high enough number that intervals extend over multiple years. False by default, to avoid train/test information leakage.
- Returns:
An instantiated Calendar built according to the input kwarg specifications
Example
Instantiate a calendar counting towards Christmas in 3-days steps.
>>> import lilio >>> calendar = lilio.daily_calendar(anchor='12-25', length="3d", n_precursors=3) >>> calendar Calendar( anchor='12-25', allow_overlap=False, mapping=None, intervals=[ Interval(role='target', length='3d', gap='0d'), Interval(role='precursor', length='3d', gap='0d'), Interval(role='precursor', length='3d', gap='0d'), Interval(role='precursor', length='3d', gap='0d') ] )
- lilio.calendar_shorthands.weekly_calendar(anchor: str, length: str = '1W', n_targets: int = 1, n_precursors: int = 0, allow_overlap: bool = False) lilio.calendar.Calendar [source]ο
Instantiate a basic monthly calendar with minimal configuration.
Set up a quick calendar revolving around intervals with week-based lengths. The precursor intervals will extend back in time with as many intervals as fit within the cycle time of one year (i.e. 52 - n_targets).
Note that the difference between this calendar and the daily_calendar revolves around the use of calendar weeks (Monday - Sunday), instead of 7-day periods.
- Parameters:
anchor β Str in the form of β40Wβ, denoting the week number. The first target interval will contain the anchor, while the precursor intervals are built back in time starting from this week.
length β The length of every precursor and target interval, e.g. β2Wβ.
n_targets β integer specifying the number of target intervals in a period.
n_precursors β Sets the maximum number of precursors of the Calendar. If 0, the amount will be determined by how many fit in each anchor year. If a value is provided, the intervals can either only cover part of the year, or extend over multiple years. In case of a large max_lag number where the intervals extend over multiple years, anchor years will be skipped to avoid overlapping intervals. To allow overlapping intervals, use the allow_overlap kwarg.
allow_overlap β Allows intervals to overlap between anchor years, if the max_lag is set to a high enough number that intervals extend over multiple years. False by default, to avoid train/test information leakage.
- Returns:
An instantiated Calendar built according to the input kwarg specifications
Example
Instantiate a calendar counting down the quarters (3 month periods) from december.
>>> import lilio >>> calendar = lilio.weekly_calendar(anchor="W40", length="1W", n_precursors=2) >>> calendar Calendar( anchor='W40-1', allow_overlap=False, mapping=None, intervals=[ Interval(role='target', length='1W', gap='0d'), Interval(role='precursor', length='1W', gap='0d'), Interval(role='precursor', length='1W', gap='0d') ] )
- lilio.calendar_shorthands.monthly_calendar(anchor: str, length: str = '1M', n_targets: int = 1, n_precursors: int = 0, allow_overlap: bool = False) lilio.calendar.Calendar [source]ο
Instantiate a basic monthly calendar with minimal configuration.
Set up a quick calendar revolving around intervals with month-based lengths. The intervals will extend back in time with as many intervals as fit within the cycle time of one year.
- Parameters:
anchor β Str in the form βJanuaryβ or βJanβ. he first target interval will contain the anchor, while the precursor intervals are built back in time starting at this Month.
length β The length of every target and precursor period, in the form β1Mβ, β2Mβ, etc.
n_targets β integer specifying the number of target intervals in a period.
n_precursors β Sets the maximum number of precursors of the Calendar. If 0, the amount will be determined by how many fit in each anchor year. If a value is provided, the intervals can either only cover part of the year, or extend over multiple years. In case of a large max_lag number where the intervals extend over multiple years, anchor years will be skipped to avoid overlapping intervals. To allow overlapping intervals, use the allow_overlap kwarg.
allow_overlap β Allows intervals to overlap between anchor years, if the max_lag is set to a high enough number that intervals extend over multiple years. False by default, to avoid train/test information leakage.
- Returns:
An instantiated Calendar built according to the input kwarg specifications
Example
Instantiate a calendar counting down the quarters (3 month periods) from december.
>>> import lilio >>> calendar = lilio.monthly_calendar(anchor='Dec', length="3M") >>> calendar Calendar( anchor='12', allow_overlap=False, mapping=None, intervals=[ Interval(role='target', length='3M', gap='0d'), Interval(role='precursor', length='3M', gap='0d'), Interval(role='precursor', length='3M', gap='0d'), Interval(role='precursor', length='3M', gap='0d') ] )