Skip to main content

superleaf

Readable, composable filters for pandas and plain Python data.

pip install superleaf           # numpy + pandas only
pip install "superleaf[fuzzy]"  # adds F.fuzzy_match (rapidfuzz)

superleaf 1.x requires pandas 3 and Python 3.11+. For pandas 2, use superleaf<1 (0.9.x has the same API without pd.col support).

Filters as data

A filter is a dict mapping columns to conditions. It can be built in one place, stored, combined and passed to dfilter:

import pandas as pd
from superleaf import F, dfilter, partition

df = pd.DataFrame({
    "type": ["T4a", "T4b", "Mi1", None, "Tm3"],
    "side": ["L", "R", "L", "R", "L"],
    "size": [10.0, None, 3.5, 7.0, -1.0],
})

left_t4 = {"type": F.startswith("T4"), "side": "L"}
dfilter(df, **left_t4)

dfilter(df, size=F.isna | F.lt(0))
dfilter(df, type=F.isin(["Mi1", "Tm3"]), columns=["type", "size"])
kept, dropped = partition(df, type=F.notna)

Keyword values are matched by equality, F.* predicates and other callables are applied to each value, and columns= selects columns by name or predicate. For conditions across columns, pass a pd.col expression, or where with any function of the DataFrame:

from superleaf import where

is_t4_or_unsized = pd.col("type").str.startswith("T4") | pd.col("size").isna()
dfilter(df, is_t4_or_unsized, side="L")
dfilter(df, where(lambda d: d["size"] > d["size"].median()))

Predicates work on values and columns

F predicates combine with |, & and ~, and evaluate either a single value or a whole Series with the same result:

is_valid = F.gt(0) | F.eq("n/a")
is_valid(5)                        # True
is_valid(None)                     # False: missing values only satisfy ne
is_valid.on_series(df["size"])     # boolean Series; vectorized where possible

& and | short-circuit like and and or, so a type check on the left guards the right.

The same predicate filters lists:

from superleaf.sequences import filtered, partitioned

filtered(F.startswith("T4"), ["T4a", "Mi1", "T4b"])   # ["T4a", "T4b"]

Available predicates: eq, ne, lt, le, gt, ge, isin, contains, contains_all, contains_any, startswith, endswith, startswith_one_of, endswith_one_of, matches_regex, fuzzy_match, isna, notna, isinf, notinf. Pass fallback= (and optionally exceptions=) to return a value instead of raising, e.g. F.lt(0, fallback=False)("a") is False.

Custom predicates and pipelines:

from superleaf.operators import attr_getter, bool_operator, str_op

is_even = bool_operator(lambda x: x % 2 == 0)
lower_starts_e = str_op("lower") >> F.startswith("e")   # pipe with >>
get_score = attr_getter("score") >> F.ge(0.5)

Compared with alternatives

Reusable as data Works on scalars and lists Missing values / mixed types
df.query("a > 0 and b == 'x'") as strings no follows pandas
df.loc[lambda d: (d.a > 0) & (d.b == "x")] as functions no follows pandas
df.loc[(pd.col("a") > 0) & (pd.col("b") == "x")] as expressions no follows pandas
polars expressions yes no (polars frames only) follows polars
dfilter(df, a=F.gt(0), b="x") as dicts yes same result per value as plain Python, with fallback=

Other utilities

  • superleaf.sequences: mapped, filtered, flatten, flatmap, groupby, partitioned
  • superleaf.dataframe: reorder_columns, expand_dict_to_cols
  • superleaf.collections: OrderedSet, SummableDict

See the API reference and CHANGELOG.

Metadata

Release files for superleaf 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for superleaf 1.0.0
File Size Uploaded
superleaf-1.0.0.tar.gz 21.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for superleaf 1.0.0
File Interpreter ABI Platform
superleaf-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 45.7 kB

Release files / superleaf-1.0.0.tar.gz

Download URL superleaf-1.0.0.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
500c791f7bf7b8e35755fa878ff7b0250b68b55fdc9eda611e86cadb11f66e73
BLAKE2b-256 checksum
How to use checksums
f43a5c4f24c1b27016ef4f766fcdc859b89cd7f2be8e9043d3e40855a6086e28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release files / superleaf-1.0.0-py3-none-any.whl

Download URL superleaf-1.0.0-py3-none-any.whl
Size 24.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f4d7a177af321145d9bc7acd5f57192b5e45b96c974e51bcb84f00c2490a59d6
BLAKE2b-256 checksum
How to use checksums
dbaec6736008eef16e060eac39ba6f20a50c7ca1fb5dd8cd0adce01de83a2750
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 17, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.9.0

2 release files

0.2.10

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.4

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page