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 search for tables for pandas (#7387)

Shahmir Varqha8a48aa41d946
diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
index 3743bd9a17..4d67d219a5 100644
--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py
+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
@@ -282,7 +282,10 @@ class NarwhalsTableManager(
if column == INDEX_COLUMN_NAME:
continue
if is_narwhals_string_type(dtype):
- expressions.append(nw.col(column).str.contains(f"(?i){query}"))
+ # Cast to string as pandas may fail for certain values
+ expressions.append(
+ nw.col(column).cast(nw.String).str.contains(f"(?i){query}")
+ )
elif dtype == nw.List(nw.String):
# TODO: Narwhals doesn't support list.contains
# expressions.append(
diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
index 7c83754c9b..1ab8851a6a 100644
--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py
+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py
@@ -888,6 +888,20 @@ class TestPandasTableManager(unittest.TestCase):
assert manager.search("yyy").get_num_rows() == 0
assert manager.search("y").get_num_rows() == 0
+ def test_search_string_column_with_nulls(self) -> None:
+ """
+ Test that string columns with null values can be searched after casting.
+ """
+ df = pd.DataFrame(
+ {"name": [float("nan")] * 4 + ["Alice"]},
+ )
+ manager = self.factory.create()(df)
+
+ # Search without raising errors
+ assert manager.search("alice").get_num_rows() == 1
+ assert manager.search("bob").get_num_rows() == 0
+ assert manager.search("nan").get_num_rows() == 4
+
def test_apply_formatting_does_not_modify_original_data(self) -> None:
original_data = self.data.copy()
format_mapping = {