Date Utils
Utility functions for time-related operations in retail analysis.
filter_and_label_by_periods(transactions, period_ranges, period_col='period_name')
Filter transactions to named time periods and label each row with its period.
Rows outside every period are dropped. The date column is read from the
column.transaction_date option.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transactions
|
Table
|
Table containing the transaction date column. |
required |
period_ranges
|
Mapping[str, tuple[datetime | date | str, datetime | date | str]]
|
Period
names mapped to inclusive |
required |
period_col
|
str
|
Name of the label column to add. Defaults to |
'period_name'
|
Returns:
| Type | Description |
|---|---|
Table
|
ibis.Table: Filtered table with the |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a period value is not a |
Source code in openretailscience/utils/date.py
find_overlapping_periods(start_date, end_date, return_str=True)
Split a date range into year-aligned overlapping periods.
The first period starts at start_date; each subsequent period starts on the same
month and day one year later. Each period ends on end_date's month and day in the
following year, so the last period ends exactly at end_date. Both dates in the
same year yield an empty list.
Note
No leap-year adjustment: a Feb 29 start or end date raises ValueError in a
non-leap year.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
datetime | str
|
Range start, a datetime or |
required |
end_date
|
datetime | str
|
Range end, a datetime or |
required |
return_str
|
bool
|
If True, dates are returned as ISO-formatted strings; if False, as datetime objects. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
list[tuple[str | datetime, str | datetime]]
|
list[tuple[str | datetime, str | datetime]]: |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If the start date is after the end date. |