lilio.utils

Commonly used utility functions for Lilio.

Module Contents

lilio.utils.MONTH_LENGTH = 30[source]
lilio.utils.check_timeseries(data: pandas.Series | pandas.DataFrame | xarray.DataArray | xarray.Dataset) None[source]

Check if input data contains valid time data.

Checks if:
  • Input data is pd.Dataframe/pd.Series/xr.Dataset/xr.DataArray.

  • Input data has a time index (pd), or a dim named time containing datetime values

lilio.utils.is_dask_array(data: xarray.DataArray | xarray.Dataset) bool[source]

Check if the xarray dataset/array has any dask arrays.

lilio.utils.check_time_dim_xarray(data) None[source]

Check if an xarray data has a time dimensions with time data.

lilio.utils.check_time_dim_pandas(data) None[source]

Check if pandas data has an index with time data.

lilio.utils.check_empty_intervals(indices_list: list[numpy.ndarray]) None[source]

Check for empty intervals in the resampling data.

Parameters:

indices_list – A list, where each item is an array with indices corresponding to the to-be-resampled data’s time axis.

Raises:

UserWarning – If the data is insufficient.

Returns:

None

lilio.utils.infer_input_data_freq(data: pandas.Series | pandas.DataFrame | xarray.DataArray | xarray.Dataset) pandas.Timedelta[source]

Infer the frequency of the input data, for comparison with the calendar freq.

Parameters:

data – Pandas or xarray data to infer the frequency of.

Returns:

a pd.Timedelta

lilio.utils.replace_month_length(length: str) str[source]

Replace month lengths with an equivalent length in days.

lilio.utils.get_smallest_calendar_freq(calendar: lilio.Calendar) pandas.Timedelta[source]

Return the smallest length of the calendar’s intervals as a Timedelta.

lilio.utils.check_input_frequency(calendar: lilio.Calendar, data: pandas.Series | pandas.DataFrame | xarray.DataArray | xarray.Dataset)[source]

Compare the frequency of (input) data to the frequency of the calendar.

Note: Pandas and xarray have the builtin function infer_freq, but this function is not robust enough for our purpose, so we have to manually infer the frequency if the builtin one fails.

lilio.utils.convert_interval_to_bounds(data: xarray.Dataset) xarray.Dataset[source]

Convert pandas intervals to bounds in a xarray Dataset.

pd.Interval objects cannot be written to netCDF. To allow writing the calendar-resampled data to netCDF these intervals have to be converted to bounds. This function adds a ‘bounds’ dimension, with ‘left’ and ‘right’ coordinates, and converts the ‘interval’ coordinates to this system.

Parameters:

data – Input data with intervals as pd.Interval objects.

Returns:

Input data with the intervals converted to bounds.

lilio.utils.check_reserved_names(input_data: pandas.Series | pandas.DataFrame | xarray.DataArray | xarray.Dataset) None[source]

Check if reserved names are already in the input data. E.g. “anchor_year”.

lilio.utils.assert_bokeh_available()[source]

Util that attempts to load the optional module bokeh.

lilio.utils.get_month_names() dict[source]

Generate a dictionary with English lowercase month names and abbreviations.

Returns:

Dictionary containing the English names of the months, including their

abbreviations, linked to the number of each month. E.g. {‘december’: 12, ‘jan’: 1}

lilio.utils.check_month_day(month: int, day: int = 1)[source]

Check if the input day/month combination is valid.

Months must be between 1 and 12, and days must be within 1 and 28/30/31 (depending on the month).

Parameters:
  • month – Month number

  • day – Day number. Defaults to 1.

lilio.utils.check_week_day(week: int, day: int = 1)[source]

Validate the week and day numbers.

lilio.utils.parse_freqstr_to_dateoffset(time_str)[source]

Parse the user-input time strings.

Parameters:

time_str – Time length string in the right formatting.

Returns:

Dictionary as keyword argument for Pandas DateOffset.