Customer Analysis
Customer purchase-frequency analysis from transaction data.
PurchasesPerCustomer (distinct purchase counts), DaysBetweenPurchases (mean gap
between a customer's purchase days), and TransactionChurn (retained/churned per
purchase-day ordinal).
DaysBetweenPurchases
Computes the average number of days between purchases per customer.
"Purchases" here means purchase days: same-day transactions count once. Customers with a single purchase day are excluded.
Attributes:
| Name | Type | Description |
|---|---|---|
table |
Table
|
One row per customer with columns |
df |
DataFrame
|
Materialized view of |
Source code in openretailscience/analysis/customer.py
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 227 228 229 230 231 232 233 234 235 | |
df
cached
property
Materialized per-customer mean gaps indexed by customer_id.
__init__(df)
Initialize the DaysBetweenPurchases class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | Table
|
Transaction data containing the
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the required columns are missing, or transaction_date is timezone-aware. |
TypeError
|
If |
Source code in openretailscience/analysis/customer.py
purchases_percentile(percentile=0.5)
Return the average inter-purchase gap (in days) at the given percentile.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
percentile
|
float
|
Percentile in [0, 1]. Defaults to 0.5 (median). |
0.5
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The average gap at that percentile, using linear interpolation, or NaN if there are no customers. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in openretailscience/analysis/customer.py
PurchasesPerCustomer
Computes the number of distinct purchases per customer.
A purchase is a distinct transaction_id (counts, not rows or days), unlike the
day-based siblings.
Attributes:
| Name | Type | Description |
|---|---|---|
table |
Table
|
One row per customer with columns |
df |
DataFrame
|
Materialized view of |
Source code in openretailscience/analysis/customer.py
68 69 70 71 72 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 102 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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | |
df
cached
property
Materialized purchase counts indexed by customer_id.
__init__(df)
Initialize the PurchasesPerCustomer class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | Table
|
Transaction data containing the
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the required columns are missing. |
TypeError
|
If |
Source code in openretailscience/analysis/customer.py
find_purchase_percentile(number_of_purchases, comparison='less_than_equal_to')
Return the share of customers whose purchase count matches a comparison.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
number_of_purchases
|
float
|
Threshold to compare against. Typically an integer count,
but fractional values are compared directly against the integer purchase counts
(e.g. |
required |
comparison
|
str
|
One of |
'less_than_equal_to'
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Fraction of customers satisfying the comparison, or NaN if there are no customers. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in openretailscience/analysis/customer.py
purchases_percentile(percentile=0.5)
Return the purchase count at the given percentile across customers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
percentile
|
float
|
Percentile in [0, 1]. Defaults to 0.5 (median). |
0.5
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The purchase count at that percentile, using linear interpolation, or NaN if there are no customers. |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
Source code in openretailscience/analysis/customer.py
TransactionChurn
Computes the churn rate per purchase-day ordinal.
transaction_number N (1-based) is a customer's N-th distinct purchase day; same-day
transactions count once. Row N covers customers whose N-th purchase day is more than
churn_period days before the latest purchase day in the data; later ones have not had
churn_period days to be followed up. Of those, churned made no later purchase and retained did;
churned_pct = churned / (churned + retained), a fraction.
Construction runs one query for the customer count and latest purchase day; table
stays lazy.
Attributes:
| Name | Type | Description |
|---|---|---|
table |
Table
|
Per- |
df |
DataFrame
|
Materialized view of |
n_unique_customers |
int
|
Distinct customers in the input data. |
Source code in openretailscience/analysis/customer.py
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | |
df
cached
property
Materialized churn table indexed by transaction_number.
__init__(df, churn_period)
Initialize the TransactionChurn class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame | Table
|
Transaction data containing the
|
required |
churn_period
|
int
|
Whole number of days of inactivity after which a customer is considered churned. Must be a positive integer. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the required columns are missing, transaction_date is
timezone-aware, or |
TypeError
|
If |