Skip to content

Commit 85fedf1

Browse files
authored
fix: handle categorical and enum types in narwhals table search (#7389)
Previously, the search functionality only checked for `dtype == nw.String`, which failed to match categorical and enum columns. This caused searches on categorical columns to return no results. The fix changes the condition to use `is_narwhals_string_type(dtype)`, which properly handles String, Categorical, and Enum types.
1 parent bceab57 commit 85fedf1

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

marimo/_plugins/ui/_impl/tables/narwhals_table.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def search(self, query: str) -> TableManager[Any]:
281281
for column, dtype in self.nw_schema.items():
282282
if column == INDEX_COLUMN_NAME:
283283
continue
284-
if dtype == nw.String:
284+
if is_narwhals_string_type(dtype):
285285
expressions.append(nw.col(column).str.contains(f"(?i){query}"))
286286
elif dtype == nw.List(nw.String):
287287
# TODO: Narwhals doesn't support list.contains

tests/_plugins/ui/_impl/tables/test_narwhals.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,57 @@ def test_search(self) -> None:
529529
assert manager.search("yyy").get_num_rows() == 0
530530
assert manager.search("y").get_num_rows() == 0
531531

532+
def test_search_with_pandas_object_dtype(self) -> None:
533+
import pandas as pd
534+
535+
# Create a pandas DataFrame with object dtype columns (mimicking real-world data)
536+
df = pd.DataFrame(
537+
{
538+
"metric": ["AdrAct30dCnt", "AdrAct7dCnt", "AdrActBlobCnt"],
539+
"full_name": [
540+
"Addresses, active, monthly, count",
541+
"Addresses, active, weekly, count",
542+
"Addresses, active, blob, count",
543+
],
544+
"description": [
545+
"The sum count of unique addresses",
546+
"The sum count of unique addresses",
547+
"The sum count of unique addresses",
548+
],
549+
"product": ["Network Data", "Network Data", "Network Data"],
550+
"category": ["Addresses", "Addresses", "Transactions"],
551+
"subcategory": ["Active", "Active", "Blobs"],
552+
}
553+
)
554+
555+
manager = NarwhalsTableManager.from_dataframe(df)
556+
557+
# This should work but might fail with "Can only use .str accessor with string values!"
558+
result = manager.search("flow")
559+
assert result.get_num_rows() == 0
560+
561+
result = manager.search("active")
562+
assert (
563+
result.get_num_rows() == 3
564+
) # Should match "active" in full_name (all rows) and subcategory
565+
566+
def test_search_with_pandas_categorical(self) -> None:
567+
import pandas as pd
568+
569+
# Create a pandas DataFrame with categorical columns
570+
df = pd.DataFrame(
571+
{
572+
"category": pd.Categorical(["cat1", "cat2", "cat3"]),
573+
"value": [1, 2, 3],
574+
}
575+
)
576+
577+
manager = NarwhalsTableManager.from_dataframe(df)
578+
579+
# Search for "cat" should match all rows
580+
result = manager.search("cat")
581+
assert result.get_num_rows() == 3
582+
532583
def test_apply_formatting_does_not_modify_original_data(self) -> None:
533584
original_data = self.data.clone()
534585
format_mapping: FormatMapping = {

0 commit comments

Comments
 (0)