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.1

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.1
File Size Uploaded Provenance Actions
superleaf-1.0.1.tar.gz 22.5 kB Without provenance Details for superleaf-1.0.1.tar.gz

Built distribution (wheel)

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

Total release size: 48.0 kB

Release files / superleaf-1.0.1.tar.gz

Download URL superleaf-1.0.1.tar.gz
Size 22.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5b1af961a926571c25acf9786b9038ca75e1ef8df8eced3ee34cacd95f488496
BLAKE2b-256 checksum
How to use checksums
52dd1cf80c767c4735295e0abe89d455df145a9dd4a6755e03c58a63f6050dbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

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

Download URL superleaf-1.0.1-py3-none-any.whl
Size 25.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7db41c252b609ad11c892c520f10aae1f190080901aa2954fad99b87f4fb73cc
BLAKE2b-256 checksum
How to use checksums
7f239d4407509df9f749027a6cc3a8318ee44d6a14246c089f84e439b10f731a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.2

Security for superleaf 1.0.1

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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