Utilities
Utility functions used internally for date manipulation and numerical rounding in financial calculations.
actual_days(start, end)
Returns the absolute number of days between two dates (inclusive of start, exclusive of end), regardless of date order. Times and timezones are ignored by normalizing to calendar dates. Args: start: The start date (pd.Timestamp). end: The end date (pd.Timestamp). Returns: int: The absolute number of days between start and end. Raises: TypeError: If start or end is not a pd.Timestamp.
Source code in curo/utils.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | |
roll_month(date, months, day)
Rolls a date by the specified number of months, adjusting to a preferred day.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date
|
Timestamp
|
The input date to roll (pd.Timestamp). |
required |
months
|
int
|
Number of months to roll; positive for forward, negative for backward (int). |
required |
day
|
int
|
Preferred day of the month for the resulting date; capped at month-end if invalid (int). |
required |
Returns:
| Type | Description |
|---|---|
Timestamp
|
pd.Timestamp: The rolled date, preserving the input timezone and normalized to midnight. |
Source code in curo/utils.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
roll_day(date, days)
Rolls a date by the specified number of days.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date
|
Timestamp
|
The input date to roll (pd.Timestamp). |
required |
days
|
int
|
Number of days to roll; positive for forward, negative for backward (int). |
required |
Returns:
| Type | Description |
|---|---|
Timestamp
|
pd.Timestamp: The rolled date, preserving the input timezone and normalized to midnight. |
Source code in curo/utils.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
roll_date(date, frequency, day)
Rolls a date forward by the frequency implicit period, adjusting to a preferred day.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date
|
Timestamp
|
The input date to roll (pd.Timestamp). |
required |
frequency
|
Frequency
|
The implicit interval to roll date forward (Frequency). |
required |
day
|
int
|
Preferred day of the month for the resulting date; capped at month-end if invalid (int). Note: ignored when frequency WEEKLY or FORTNIGHTLY. |
required |
Returns:
| Type | Description |
|---|---|
Timestamp
|
pd.Timestamp: The rolled date, preserving the input timezone and normalized to midnight. |
Source code in curo/utils.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
to_timestamp(dt)
Converts a date input to a pd.Timestamp normalized to midnight UTC.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dt
|
Optional[Union[Timestamp, datetime, date]]
|
A pd.Timestamp, datetime.datetime, or datetime.date. If None, returns None. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Timestamp]
|
pd.Timestamp: A UTC timestamp with time set to midnight (00:00:00), or None. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If dt is not a supported type. |
Source code in curo/utils.py
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
has_month_end_day(date)
Checks if a date is the last day of its month.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
date
|
Timestamp
|
The date to check (pd.Timestamp). |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if the date is the last day of the month, False otherwise. |
Source code in curo/utils.py
129 130 131 132 133 134 135 136 137 138 139 140 | |
gauss_round(num, precision=0)
Performs Gaussian (bankers') rounding to the nearest even number to avoid statistical bias.
Unlike standard rounding, which biases upward, Gaussian rounding selects the nearest even number when a value is exactly halfway between two numbers (e.g., 2.5 rounds to 2, 3.5 to 4).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num
|
float
|
The number to round (float). |
required |
precision
|
int
|
Number of decimal places; can be positive or negative (int, optional). Defaults to 0. |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The number rounded to the specified precision. |
Source code in curo/utils.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |