Date Utils
Utility functions for time-related operations in retail analysis.
filter_and_label_by_periods(transactions, period_ranges, period_col='period_name')
Filters transactions to specified time periods and adds period labels.
This function filters transactions based on specified time periods and adds a new column indicating the period name. It is useful for analyzing transactions within specific date ranges and comparing KPIs between them.
Example
transactions = ibis.table("transactions") period_ranges = { "Q1": ("2023-01-01", "2023-03-31"), "Q2": ("2023-04-01", "2023-06-30"), } filtered_transactions = filter_and_label_by_periods(transactions, period_ranges)
filtered_transactions will only contain transactions from the date ranges specified in Q1 and Q2 and a new
column 'period_name' will be in the table defining the period for each transaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transactions |
Table
|
An ibis table with a transaction_date column. |
required |
period_ranges |
dict[str, tuple[datetime, datetime] | tuple[str, str]]
|
Dict where keys are period names and values are(start_date, end_date) tuples. |
required |
period_col |
str
|
Name of the column to create for period labels. Defaults to "period_name". |
'period_name'
|
Returns:
| Type | Description |
|---|---|
Table
|
An ibis table with filtered transactions and added period label column. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any value in period_ranges is not a tuple of length 2. |
ValueError
|
If first date > second date for any period. |
ValueError
|
If periods overlap with each other. |
Source code in openretailscience/utils/date.py
find_overlapping_periods(start_date, end_date, return_str=True)
Find overlapping time periods within the given date range, split by year.
This function generates overlapping periods between a given start date and end date. The first period will start from the given start date, and each subsequent period will start on the same month and day for the following years, ending each period on the same month and day of the end date but in the subsequent year, except for the last period, which ends at the provided end date.
Note
This function does not adjust for leap years. If the start or end date is February 29, it may cause an issue in non-leap years.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date |
datetime | str
|
The starting date of the range, either as a datetime object or 'YYYY-MM-DD' string. |
required |
end_date |
datetime | str
|
The ending date of the range, either as a datetime object or 'YYYY-MM-DD' string. |
required |
return_str |
bool
|
If True, returns dates as ISO-formatted strings ('YYYY-MM-DD'). If False, returns datetime objects. Defaults to True. |
True
|
Returns:
| Type | Description |
|---|---|
list[tuple[str | datetime, str | datetime]]
|
list[tuple[str | datetime, str | datetime]]: |
list[tuple[str | datetime, str | datetime]]
|
A list of tuples where each tuple contains the start and end dates of an overlapping period, |
list[tuple[str | datetime, str | datetime]]
|
either as strings (ISO format) or datetime objects. Returned datetimes preserve the |
list[tuple[str | datetime, str | datetime]]
|
timezone-awareness of the input (naive in → naive out, aware in → aware out). |
list[tuple[str | datetime, str | datetime]]
|
String inputs produce naive datetime outputs. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If start_date and end_date have mismatched timezone awareness (one naive or string and one timezone-aware, or vice versa). |
ValueError
|
If the start date is after the end date. |
Source code in openretailscience/utils/date.py
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | |