lair.clock.regular_times_to_intervals#

lair.clock.regular_times_to_intervals(times, time_step='monthly', closed='left')[source]#

Convert an array of regular times to intervals of the specified length.

Parameters:
  • times (np.array) – A numpy array of datetime64 values indicating the start of each interval.

  • time_step (str, optional) – The interval step (‘hourly’, ‘daily’, ‘monthly’, ‘annual’). Determines the length of each interval. Default is ‘monthly’.

  • closed (str, optional) – Defines if the interval is closed on the ‘left’, ‘right’, ‘both’, or ‘neither’. Default is ‘left’.

Returns:

pd.IntervalIndex – An index with intervals covering the specified time range.

Raises:

ValueError – If time_step is not one of ‘hourly’, ‘daily’, ‘monthly’, or ‘annual’.

Return type:

IntervalIndex

See also

pandas.interval_range

Intervals of one length between a start and an end.

Examples

>>> import pandas as pd
>>> from lair.clock import regular_times_to_intervals
>>> months = pd.date_range("2024-01-01", periods=2, freq="MS")
>>> intervals = regular_times_to_intervals(months)
>>> [str(interval) for interval in intervals]
['[2024-01-01 00:00:00, 2024-02-01 00:00:00)', '[2024-02-01 00:00:00, 2024-03-01 00:00:00)']