Metadata-Version: 2.5
Name: rfmkit
Version: 0.1.0
Summary: Recency, frequency and monetary (RFM) analysis in pure pandas and matplotlib.
Project-URL: Homepage, https://rfmkit.rsquaredacademy.com
Project-URL: Documentation, https://rfmkit.rsquaredacademy.com
Project-URL: Repository, https://github.com/rsquaredacademy/rfmkit
Project-URL: Issues, https://github.com/rsquaredacademy/rfmkit/issues
Author-email: Rsquared Academy <aravindhebbali@rsquaredacademy.com>
License-Expression: MIT
License-File: LICENSE
Keywords: customer,frequency,marketing,monetary,recency,rfm,segmentation
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Requires-Python: >=3.11
Requires-Dist: matplotlib>=3.8
Requires-Dist: numpy>=1.26
Requires-Dist: pandas>=2.2
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: coverage[toml]>=7.5; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: twine>=5.0; extra == 'dev'
Provides-Extra: docs
Requires-Dist: black>=24; extra == 'docs'
Requires-Dist: mkdocs-material>=9.6; extra == 'docs'
Requires-Dist: mkdocstrings[python]>=0.29; extra == 'docs'
Description-Content-Type: text/markdown

# rfmkit

rfmkit scores customers by recency, frequency and monetary value and turns
those scores into segments, for marketing analysts and R learners who want the
same numbers as the R [`rfm`](https://rfm.rsquaredacademy.com) package in pure
pandas and matplotlib (no compiled code, so it also runs in Pyodide).

## Install

```bash
pip install rfmkit
```

Python 3.11+ with pandas 2.2+, numpy and matplotlib.

## Example

```python
import pandas as pd
from rfmkit import load_orders, rfm_table_order

orders = load_orders()
table = rfm_table_order(
    orders,
    customer_id="customer_id",
    order_date="order_date",
    revenue="revenue",
    analysis_date=pd.Timestamp("2026-01-01"),
)

print(table.rfm.head(3).to_string(index=False))
```

```text
customer_id  recency_days  transaction_count  amount  recency_score  frequency_score  monetary_score  rfm_score
      C0001         293.0                  5  304.19              2                3               4        234
      C0002         220.0                  3  138.87              3                2               2        322
      C0003         261.0                  2  161.62              2                1               3        213
```

Segment the scored customers and summarise the result:

```python
from rfmkit import rfm_segment, rfm_segment_summary

segments = rfm_segment(
    table,
    segment_names=["Champions"],
    recency_lower=[5], recency_upper=[5],
    frequency_lower=[5], frequency_upper=[5],
    monetary_lower=[5], monetary_upper=[5],
)
print(rfm_segment_summary(segments))
```

`rfmkit.datasets.load_orders()` ships a synthetic two-year order history for
1,000 customers and 5,000 orders.

## Differences from R

rfmkit reproduces the numbers of R `rfm` 0.5.1, including its tie handling and
binning quirks. These are the deliberate differences:

- **Whole-number bin counts.** `recency_bins=5.5` raises `ValueError`; R accepts
  it and produces non-quintile cuts.
- **Date-time columns.** A date-time `order_date` or recency column raises
  `TypeError` naming the column; R silently computes a huge negative recency.
- **Constant extra columns with `NA`.** An extra order column is kept when its
  non-missing values are constant, even if one value is `NA`; R drops it.
- **Empty input.** Returns a zero-row table; R fails inside `findInterval`.
- **Fractional `n_transactions`.** Rejected up front with `ValueError`; R fails
  at the very end.
- **Counts above `2**31 - 1`.** Kept as int64; R coerces values above the int32
  range to `NA`.
- **One-item bin lists.** `frequency_bins=[5]` raises `ValueError`; it cannot
  clearly mean a bin count or a break point.
- **`rfm_segment` bound lengths.** Every `*_lower`/`*_upper` argument must have
  the same length as `segment_names`; a mismatch raises `ValueError` naming the
  argument. R recycles length-1 bounds, so later rules can silently receive
  `NA` and never match.

## Documentation

<https://rfmkit.rsquaredacademy.com>

## Licence

MIT.
