Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

fix(dataframes): generated filter code should match the widget for the literal `contains` operator (#10617)

Madan kumar9fc1796c166a
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py b/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py
index 872f551b35..d452437e36 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/print_code.py
@@ -49,7 +49,7 @@ def python_print_pandas(
elif operator in [">", ">=", "<", "<="]:
result = f"{col} {operator} {_as_literal(value)}"
elif operator == "contains":
- result = f"{col}.str.contains({_as_literal(value)})"
+ result = f"{col}.str.contains({_as_literal(value)}, regex=False)"
elif operator == "regex":
result = f"{col}.str.contains({_as_literal(value)}, regex=True)"
elif operator == "starts_with":
@@ -317,7 +317,7 @@ def python_print_polars(
elif operator in [">", ">=", "<", "<="]:
result = f"{col} {operator} {_as_literal(value)}"
elif operator == "contains":
- result = f"{col}.str.contains({_as_literal(value)})"
+ result = f"{col}.str.contains({_as_literal(value)}, literal=True)"
elif operator == "regex":
result = f"{col}.str.contains({_as_literal(value)}, literal=False)"
elif operator == "starts_with":
diff --git a/tests/_plugins/ui/_impl/dataframes/test_print_code.py b/tests/_plugins/ui/_impl/dataframes/test_print_code.py
index fba1d04420..ccaa1c3ba3 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_print_code.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_print_code.py
@@ -615,6 +615,69 @@ def test_print_code_expand_dict_nan_pandas() -> None:
)
+# The values here are valid strings that are also regexes: "a.c" matches
+# "abc" when interpreted as a regex, and "(" is not a valid regex at all. If
+# `contains` is emitted without a literal flag, the generated pandas code
+# diverges from the widget (or raises), so this test fails.
[email protected]("value", ["a.c", "("])
+ not DependencyManager.pandas.has(), reason="pandas not installed"
+)
+def test_print_code_contains_is_literal_pandas(value: str) -> None:
+ import pandas as pd
+
+ my_df = pd.DataFrame({"strings": ["abc", "a.c", "f(x)", "zzz"]})
+
+ transformations = Transformations(
+ [
+ FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="strings",
+ operator="contains",
+ value=value,
+ )
+ ],
+ ),
+ )
+ ]
+ )
+
+ pandas_code = python_print_transforms(
+ "my_df",
+ list(my_df.columns),
+ transformations.transforms,
+ python_print_pandas,
+ )
+
+ loc = {"pd": pd, "my_df": my_df.copy()}
+ exec(pandas_code, {}, loc)
+ code_result = loc["my_df_next"]
+
+ nw_df = nw.from_native(my_df.copy(), eager_only=True).lazy()
+ real_result = (
+ _apply_transforms(
+ nw_df,
+ NarwhalsTransformHandler(),
+ transformations,
+ )
+ .collect()
+ .to_native()
+ .reset_index(drop=True)
+ )
+
+ pd.testing.assert_frame_equal(
+ code_result.reset_index(drop=True),
+ real_result,
+ )
+
+
@given(
transform=create_transform_strategy(
defined_column_id,
@@ -906,6 +969,66 @@ def test_print_code_expand_dict_nested_dict_polars() -> None:
assert code_result.columns == ["a", "nested", "other"]
+# The values here are valid strings that are also regexes: "a.c" matches
+# "abc" when interpreted as a regex, and "(" is not a valid regex at all. If
+# `contains` is emitted without a literal flag, the generated polars code
+# diverges from the widget (or raises), so this test fails.
[email protected]("value", ["a.c", "("])
+ not DependencyManager.polars.has(), reason="polars not installed"
+)
+def test_print_code_contains_is_literal_polars(value: str) -> None:
+ import polars as pl
+ import polars.testing as pl_testing
+
+ my_df = pl.DataFrame({"strings": ["abc", "a.c", "f(x)", "zzz"]})
+
+ transformations = Transformations(
+ [
+ FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=FilterGroup(
+ type="group",
+ operator="and",
+ children=[
+ FilterCondition(
+ type="condition",
+ column_id="strings",
+ operator="contains",
+ value=value,
+ )
+ ],
+ ),
+ )
+ ]
+ )
+
+ polars_code = python_print_transforms(
+ "my_df",
+ my_df.columns,
+ transformations.transforms,
+ python_print_polars,
+ )
+
+ loc = {"pl": pl, "my_df": my_df.clone()}
+ exec(polars_code, globals(), loc)
+ code_result = loc["my_df_next"]
+
+ nw_df = nw.from_native(my_df.clone(), eager_only=True).lazy()
+ real_result = (
+ _apply_transforms(
+ nw_df,
+ NarwhalsTransformHandler(),
+ transformations,
+ )
+ .collect()
+ .to_native()
+ )
+
+ pl_testing.assert_frame_equal(code_result, real_result)
+
+
@given(
transform=create_transform_strategy(
defined_column_id,