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: polars category filtering (#3744)

Myles Scolnick459a15d5c1f3
diff --git a/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py b/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
index ebd38b7929..3032cc49da 100644
--- a/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
+++ b/marimo/_plugins/ui/_impl/dataframes/transforms/handlers.py
@@ -271,6 +271,7 @@ class PolarsTransformHandler(TransformHandler["pl.DataFrame"]):
column = col(str(condition.column_id))
dtype = df.schema[str(condition.column_id)]
value = condition.value
+ value_str = str(value)
# If columns type is a Datetime, we need to convert the value to a datetime
if dtype == pl.Datetime and isinstance(value, str):
@@ -280,6 +281,10 @@ class PolarsTransformHandler(TransformHandler["pl.DataFrame"]):
elif dtype == pl.Time and isinstance(value, str):
value = datetime.time.fromisoformat(value)
+ # If columns type is a Categorical, we need to cast the value to a string
+ if dtype == pl.Categorical:
+ column = column.cast(pl.String)
+
# Build the expression based on the operator
if condition.operator == "==":
condition_expr = column == value
@@ -306,15 +311,13 @@ class PolarsTransformHandler(TransformHandler["pl.DataFrame"]):
elif condition.operator == "does_not_equal":
condition_expr = column != value
elif condition.operator == "contains":
- condition_expr = column.str.contains(value or "", literal=True)
+ condition_expr = column.str.contains(value_str, literal=True)
elif condition.operator == "regex":
- condition_expr = column.str.contains(
- value or "", literal=False
- )
+ condition_expr = column.str.contains(value_str, literal=False)
elif condition.operator == "starts_with":
- condition_expr = column.str.starts_with(value or "")
+ condition_expr = column.str.starts_with(value_str)
elif condition.operator == "ends_with":
- condition_expr = column.str.ends_with(value or "")
+ condition_expr = column.str.ends_with(value_str)
elif condition.operator == "in":
condition_expr = column.is_in(value or [])
else:
diff --git a/tests/_plugins/ui/_impl/dataframes/test_handlers.py b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
index 6d12f366fb..d2ac2c3f57 100644
--- a/tests/_plugins/ui/_impl/dataframes/test_handlers.py
+++ b/tests/_plugins/ui/_impl/dataframes/test_handlers.py
@@ -602,6 +602,103 @@ class TestTransformHandler:
result = apply(df, transform)
assert_frame_equal(result, expected)
+ @staticmethod
+ @pytest.mark.parametrize(
+ "df",
+ [
+ (
+ pd.DataFrame({"column_a": ["alpha", "beta", "gamma"]}).astype(
+ {"column_a": "category"}
+ )
+ ),
+ (
+ pl.DataFrame(
+ {"column_a": ["alpha", "beta", "gamma"]},
+ schema_overrides={"column_a": pl.Categorical},
+ )
+ ),
+ (ibis.memtable({"column_a": ["alpha", "beta", "gamma"]})),
+ ],
+ )
+ def test_handle_filter_rows_categorical(df: DataFrameType) -> None:
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=[
+ Condition(
+ column_id="column_a",
+ operator="equals",
+ value="alpha",
+ )
+ ],
+ )
+ result = apply(df, transform)
+ assert df_size(result) == 1
+
+ transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=[
+ Condition(
+ column_id="column_a",
+ operator="does_not_equal",
+ value="alpha",
+ )
+ ],
+ )
+ result = apply(df, transform)
+ assert df_size(result) == 2
+
+ ends_with_transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=[
+ Condition(
+ column_id="column_a", operator="ends_with", value="mma"
+ )
+ ],
+ )
+ result = apply(df, ends_with_transform)
+ assert df_size(result) == 1
+
+ contains_transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=[
+ Condition(
+ column_id="column_a", operator="contains", value="mma"
+ )
+ ],
+ )
+ result = apply(df, contains_transform)
+ assert df_size(result) == 1
+
+ does_not_contain_transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="remove_rows",
+ where=[
+ Condition(
+ column_id="column_a",
+ operator="contains",
+ value="mma",
+ )
+ ],
+ )
+ result = apply(df, does_not_contain_transform)
+ assert df_size(result) == 2
+
+ starts_with_transform = FilterRowsTransform(
+ type=TransformType.FILTER_ROWS,
+ operation="keep_rows",
+ where=[
+ Condition(
+ column_id="column_a", operator="starts_with", value="alp"
+ )
+ ],
+ )
+ result = apply(df, starts_with_transform)
+ assert df_size(result) == 1
+
@staticmethod
@pytest.mark.parametrize(
("df", "expected"),