|
| 1 | +# Using Blosc2 as a pandas Engine |
| 2 | + |
| 3 | +pandas' `DataFrame.apply` and `Series.map` accept an `engine=` argument: a |
| 4 | +callable exposing a `__pandas_udf__` attribute that pandas dispatches to |
| 5 | +instead of running the Python-level per-row/per-element loop. `blosc2.jit` |
| 6 | +is such an engine. |
| 7 | + |
| 8 | +The contract is different from a plain `apply`/`map` callback: the function |
| 9 | +passed to `engine=blosc2.jit` must be **vectorized** — it is called *once* |
| 10 | +with a full NumPy array (a column, a row, or the whole array, depending on |
| 11 | +`axis`), not once per element. This is the same contract `@blosc2.jit` |
| 12 | +already has everywhere else in the library; using it as a pandas engine |
| 13 | +just changes who supplies the array. |
| 14 | + |
| 15 | +```python |
| 16 | +import numpy as np |
| 17 | +import pandas as pd |
| 18 | +import blosc2 |
| 19 | + |
| 20 | +df = pd.DataFrame( |
| 21 | + { |
| 22 | + "a": np.arange(1_000_000, dtype=np.float64), |
| 23 | + "b": np.arange(1_000_000, dtype=np.float64), |
| 24 | + } |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +@blosc2.jit |
| 29 | +def add_one(col): |
| 30 | + return col + 1 |
| 31 | + |
| 32 | + |
| 33 | +result = df.apply(add_one, engine=blosc2.jit) |
| 34 | +``` |
| 35 | + |
| 36 | +`axis=0` (the default) calls the function once per column; `axis=1` calls it |
| 37 | +once per row. **Use `axis=0`** (or restructure the computation so it works |
| 38 | +column-wise): the win comes from the Blosc2/numexpr compute engine (operator |
| 39 | +fusion, multi-threading) processing one large 1D array per call, and that |
| 40 | +only happens for columns. `axis=1` still calls the function once per row — |
| 41 | +same as plain pandas — and for a handful of columns, the overhead of |
| 42 | +wrapping each tiny row array for the compute engine outweighs any benefit, |
| 43 | +so `engine=blosc2.jit` with `axis=1` is typically *slower* than plain |
| 44 | +`apply(axis=1)`. See the benchmark below. |
| 45 | + |
| 46 | +`Series.map(func, engine=blosc2.jit)` works the same way: `func` is called |
| 47 | +once with the Series' full underlying array. |
| 48 | + |
| 49 | +## Limitations |
| 50 | + |
| 51 | +- Only numeric dtypes are supported. A non-numeric (e.g. object-dtype or |
| 52 | + string) column raises a `ValueError` naming the limitation rather than |
| 53 | + attempting the computation. |
| 54 | +- `na_action="ignore"` is not supported for `map` and raises |
| 55 | + `NotImplementedError` — the vectorized-call contract means there is no |
| 56 | + per-element step at which to skip a value. |
| 57 | +- `Series.apply(func, engine=...)` and `DataFrame.map(func, engine=...)` do |
| 58 | + not reach `blosc2.jit` at all: pandas 3's `Series.apply` does not accept |
| 59 | + an `engine` keyword for non-string functions, and `DataFrame.map` doesn't |
| 60 | + forward `engine` to a dispatch mechanism at all. These are limitations of |
| 61 | + the pandas-side API surface, not of the Blosc2 engine. The two entry |
| 62 | + points that do reach the engine are `DataFrame.apply` and `Series.map`. |
| 63 | + |
| 64 | +## Benchmark |
| 65 | + |
| 66 | +`bench/bench_pandas_engine.py` compares `df.apply(f, engine=blosc2.jit)` |
| 67 | +against plain `df.apply(f)` (`axis=0`, the default) on a 1,000,000-row, |
| 68 | +8-column frame, for a multi-operation elementwise expression |
| 69 | +(`sin(x)*cos(x) + x**2 - sqrt(|x|) + exp(-x)`). Measured on the development |
| 70 | +machine (Apple M4, conda env with pandas 3.0.3): |
| 71 | + |
| 72 | +``` |
| 73 | +rows=1000000, cols=8 |
| 74 | +plain df.apply(f): 0.1114 s |
| 75 | +df.apply(f, engine=blosc2.jit): 0.0260 s |
| 76 | +speedup: 4.3x |
| 77 | +``` |
| 78 | + |
| 79 | +Run the script for the numbers on your machine: |
| 80 | + |
| 81 | +``` |
| 82 | +python bench/bench_pandas_engine.py |
| 83 | +``` |
0 commit comments