marimo icon indicating copy to clipboard operation
marimo copied to clipboard

mo.ui.dataframe: generated code for "not in", "in" and "is empty" filters handles null rows differently from the widget

Open Assertive-xxh opened this issue 2 weeks ago • 0 comments

Describe the bug

In mo.ui.dataframe, the code in the "Python Code" tab can keep different rows than the widget for the not in, in and is empty filters when the column has nulls.

The widget (NarwhalsTransformHandler._evaluate_condition in transforms/handlers.py) handles nulls explicitly:

  • not in [...] keeps null rows, and in [...] matches them only when the selected values include a null. The value picker offers a null when the column has one.
  • is empty does not match null rows, so "remove rows where is empty" keeps them.

The code printers in transforms/print_code.py emit a bare isin / is_in and a bare == "", so the result depends on how each backend handles nulls:

Filter Backends where the code differs from the widget
keep rows, not in ["apple"] Polars, Ibis: the code drops the null rows
remove rows, is empty Polars, Ibis, pandas string dtype: the code drops the null rows
keep rows, in ["apple", null] Polars, Ibis, pandas object dtype: the code drops the null rows
keep rows, not in ["apple", null] pandas object dtype: the code keeps the null rows. Ibis: the code keeps no rows

With pandas 3's default str dtype, the code matches the widget in all four cases.

#11036 fixed the same kind of mismatch for contains, regex, starts_with and ends_with. in, not in and is empty were not part of that change.

Code to reproduce

The script sets the widget value the same way the frontend does, then runs the code that the "Python Code" tab shows:

# /// script
# requires-python = ">=3.11"
# dependencies = ["marimo", "polars"]
# ///
import polars as pl

import marimo as mo
from marimo._runtime.functions import EmptyArgs

df = pl.DataFrame({"fruit": ["apple", None, "", "pear"]})


def compare(label, filter_rows):
    widget = mo.ui.dataframe(df)
    widget._update({"transforms": [{"type": "filter_rows", **filter_rows}]})
    code = widget._get_dataframe(EmptyArgs()).python_code
    namespace = {"pl": pl, "df": df}
    exec(code, namespace)
    print(f"--- {label}")
    print(code.splitlines()[-1])
    print("widget keeps:", widget.value["fruit"].to_list())
    print("code keeps:  ", namespace["df_next"]["fruit"].to_list())


compare(
    "keep rows where fruit is not in ['apple']",
    {
        "operation": "keep_rows",
        "where": [{"column_id": "fruit", "operator": "not_in", "value": ["apple"]}],
    },
)
compare(
    "remove rows where fruit is empty",
    {
        "operation": "remove_rows",
        "where": [{"column_id": "fruit", "operator": "is_empty"}],
    },
)

Output with marimo 0.25.2 and polars 2.1.0:

--- keep rows where fruit is not in ['apple']
df_next = df_next.filter(~pl.col("fruit").is_in(('apple',)))
widget keeps: [None, '', 'pear']
code keeps:   ['', 'pear']
--- remove rows where fruit is empty
df_next = df_next.filter(~(((pl.col("fruit") == ""))))
widget keeps: ['apple', None, 'pear']
code keeps:   ['apple', 'pear']

Expected

The code in the "Python Code" tab keeps the same rows as the widget: [None, '', 'pear'] in the first case and ['apple', None, 'pear'] in the second.

Environment

marimo 0.25.2 from PyPI, and main at 9bdfb76. Python 3.12, Linux (WSL2). polars 2.1.0, pandas 3.1.1, ibis-framework 12.0.0 with DuckDB 1.5.6.

marimo env (from the main checkout)
{
  "marimo": "0.25.2",
  "editable": true,
  "location": "<repo>/marimo",
  "OS": "Linux",
  "OS Version": "6.6.114.1-microsoft-standard-WSL2",
  "Processor": "x86_64",
  "Python Version": "3.12.3",
  "Locale": "C",
  "Binaries": {
    "Browser": "--",
    "Node": "--",
    "uv": "0.13.8 (x86_64-unknown-linux-gnu)"
  },
  "Dependencies": {
    "click": "8.5.1",
    "docutils": "0.23",
    "itsdangerous": "2.2.0",
    "jedi": "0.20.1",
    "markdown": "3.11.3",
    "narwhals": "2.28.0",
    "packaging": "26.3",
    "psutil": "7.2.2",
    "pygments": "2.21.0",
    "pymdown-extensions": "11.0.2",
    "pyyaml": "6.0.3",
    "starlette": "1.10.0",
    "tomlkit": "0.15.2",
    "typing-extensions": "4.16.0",
    "uvicorn": "0.54.0",
    "websockets": "17.4"
  },
  "Optional Dependencies": {
    "duckdb": "1.5.6",
    "ibis-framework": "12.0.0",
    "openai": "3.46.0",
    "pandas": "3.1.1",
    "polars": "2.1.0",
    "pyarrow": "26.0.1",
    "pytest": "9.1.1",
    "ruff": "0.17.4",
    "sqlglot": "30.30.0"
  },
  "Experimental Flags": {
    "cache_panel": true,
    "multi_column": true,
    "isolate_apps": true,
    "debugger": false,
    "line_timing": false,
    "execution_type": "relaxed"
  }
}

Will you submit a PR?

  • [x] Yes

I have a fix ready that applies the widget's null rules in the three printers, with tests that run the generated code against the widget for pandas, Polars and Ibis, in the style of #11036.

This issue was drafted with the help of an AI assistant (Claude Code), which also ran the reproduction. I reviewed it before posting.

Assertive-xxh avatar Nov 14 '26 09:11 Assertive-xxh