raffalib-python documentation
raffalib-python is a small library of helper functions for data wrangling.
Its main purpose is to enrich pandas and
polars with STATA-like logging
and .docx export capabilities, but it also bundles assorted utilities for
logging, pickling, progress bars, Selenium, SQLAlchemy, and bibliometrics.
Highlights
STATA-like logging — wrap a pandas/polars pipeline in
startlog()/endlog()and get a log line describing how many rows, columns, or cell values changed (and optionally how long it took).Logging joins — a
join()wrapper (for both backends) that logs where the output rows came from: left only, right only, both, dropped rows, and duplicate keys.Frequency & cross tables —
freq()andcrosstab()accessors for quick tabulations.Word export — write any DataFrame straight to a
.docxtable withto_docx().
A taste
import pandas as pd
import numpy as np
import raffalib
import raffalib.pandas # registers the `.raffa` accessor
raffalib.create_logger(rich=False, fmt="{message}")
df = pd.DataFrame(
{
"species": ["Adelie", "Adelie", "Adelie", "Gentoo", "Gentoo", "Chinstrap"],
"bill_depth_mm": [18.7, np.nan, 18.0, np.nan, 16.3, 17.9],
"body_mass_g": [3750.0, 3800.0, np.nan, 4500.0, 5700.0, 3500.0],
}
)
df = df.raffa.startlog().dropna(subset=["bill_depth_mm"]).raffa.endlog()
# -> Removed 2/6 (33.33%) rows. New shape: (4, 3).
# -> Took: 0.01 seconds
New here? Start with Quickstart, then browse the Examples for the full tour. The complete API reference is generated from the source docstrings under API Reference in the sidebar.
Note
This project is under active development.