Dataframe BenchMEASURED IN ONE ENVIRONMENT

    ↑↓ to select, Enter to open, Esc to close

    pandas str.contains(regex=False) is ~22.22× slower than polars str.contains(literal=True) for substring search over a text column

    Both sides are told the pattern is a literal, not a regular expression.

    Result

    Runpandas str.contains(regex=False)polars str.contains(literal=True)Ratio
    Run 159.412.821.22×
    Run 261.422.6123.51×
    Run 381.893.6922.22×

    Spread over 3 runs: pandas str.contains(regex=False) CV 18.4% / polars str.contains(literal=True) CV 18.9% / ratio CV 5.2%

    The two calls

    df[df['s'].str.contains('item-001', regex=False)]
    df.filter(pl.col('s').str.contains('item-001', literal=True))

    Conditions

    400,000 short strings of the form `item-00123-45`.

    Where this number does not apply

    A regex pattern costs more on both sides; that comparison is not on this page.

    Reproduce

    cd bench && python run.py --run 1 --out ../data/run1.json