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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: handle categorical and enum types in narwhals table search (#7389)
Myles Scolnick85fedf1b22e6
diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.pyindex 515c9eaebb..3743bd9a17 100644--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py@@ -281,7 +281,7 @@ class NarwhalsTableManager(for column, dtype in self.nw_schema.items():if column == INDEX_COLUMN_NAME:continue- if dtype == nw.String:+ if is_narwhals_string_type(dtype):expressions.append(nw.col(column).str.contains(f"(?i){query}"))elif dtype == nw.List(nw.String):# TODO: Narwhals doesn't support list.containsdiff --git a/tests/_plugins/ui/_impl/tables/test_narwhals.py b/tests/_plugins/ui/_impl/tables/test_narwhals.pyindex 47bf6168a1..fd69e59855 100644--- a/tests/_plugins/ui/_impl/tables/test_narwhals.py+++ b/tests/_plugins/ui/_impl/tables/test_narwhals.py@@ -529,6 +529,57 @@ class TestNarwhalsTableManagerFactory(unittest.TestCase):assert manager.search("yyy").get_num_rows() == 0assert manager.search("y").get_num_rows() == 0+ def test_search_with_pandas_object_dtype(self) -> None:+ import pandas as pd++ # Create a pandas DataFrame with object dtype columns (mimicking real-world data)+ df = pd.DataFrame(+ {+ "metric": ["AdrAct30dCnt", "AdrAct7dCnt", "AdrActBlobCnt"],+ "full_name": [+ "Addresses, active, monthly, count",+ "Addresses, active, weekly, count",+ "Addresses, active, blob, count",+ ],+ "description": [+ "The sum count of unique addresses",+ "The sum count of unique addresses",+ "The sum count of unique addresses",+ ],+ "product": ["Network Data", "Network Data", "Network Data"],+ "category": ["Addresses", "Addresses", "Transactions"],+ "subcategory": ["Active", "Active", "Blobs"],+ }+ )++ manager = NarwhalsTableManager.from_dataframe(df)++ # This should work but might fail with "Can only use .str accessor with string values!"+ result = manager.search("flow")+ assert result.get_num_rows() == 0++ result = manager.search("active")+ assert (+ result.get_num_rows() == 3+ ) # Should match "active" in full_name (all rows) and subcategory++ def test_search_with_pandas_categorical(self) -> None:+ import pandas as pd++ # Create a pandas DataFrame with categorical columns+ df = pd.DataFrame(+ {+ "category": pd.Categorical(["cat1", "cat2", "cat3"]),+ "value": [1, 2, 3],+ }+ )++ manager = NarwhalsTableManager.from_dataframe(df)++ # Search for "cat" should match all rows+ result = manager.search("cat")+ assert result.get_num_rows() == 3+def test_apply_formatting_does_not_modify_original_data(self) -> None:original_data = self.data.clone()format_mapping: FormatMapping = {