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: table filtering by NaN in pandas string columns (#9092)
Kiran Gadhave55b1f86b2ef6
diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.pyindex 4f99a6c4b3..e05d48b057 100644--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py@@ -237,14 +237,19 @@ class NarwhalsTableManager(result = _calculate_top_k_rows(frame)value_counts: list[tuple[Any, int]] = []-- # NaNs and Infs serialize to null, which isn't distingushable from normal nulls- # so instead we set to string values+ col_dtype = self.data.collect_schema()[column]++ # NaNs and Infs serialize to null, which isn't distinguishable from+ # normal nulls, so we replace them with string tokens — but only for+ # float columns where NaN/Inf are meaningful distinct values.+ # For non-float columns (e.g. strings), NaN is just pandas' missing+ # value sentinel and should stay as None.+ is_float_col = col_dtype.is_float()for row in result.rows():value = unwrap_py_scalar(row[0])count = int(unwrap_py_scalar(row[1]))if isinstance(value, float) and math.isnan(value):- value = NAN_VALUE+ value = NAN_VALUE if is_float_col else Noneelif isinstance(value, float) and math.isinf(value) and value > 0:value = POSITIVE_INFelif isinstance(value, float) and math.isinf(value) and value < 0:@@ -278,9 +283,7 @@ class NarwhalsTableManager(return ("time", dtype_string)elif dtype == nw.Date:return ("date", dtype_string)- elif dtype == nw.Datetime:- return ("datetime", dtype_string)- elif dtype.is_temporal():+ elif dtype == nw.Datetime or dtype.is_temporal():return ("datetime", dtype_string)elif dtype.is_numeric():return ("number", dtype_string)@@ -463,35 +466,7 @@ class NarwhalsTableManager(),})- elif is_narwhals_integer_type(dtype):- exprs.update(- {- "unique": col.n_unique(),- "min": col.min(),- "max": col.max(),- "mean": col.mean(),- "std": col.std(),- "median": col.median(),- }- )- if supports_numeric_quantiles:- exprs.update(- {- "p5": col.quantile(- 0.05, interpolation=quantile_interpolation- ),- "p25": col.quantile(- 0.25, interpolation=quantile_interpolation- ),- "p75": col.quantile(- 0.75, interpolation=quantile_interpolation- ),- "p95": col.quantile(- 0.95, interpolation=quantile_interpolation- ),- }- )- elif dtype.is_numeric():+ elif is_narwhals_integer_type(dtype) or dtype.is_numeric():exprs.update({"unique": col.n_unique(),diff --git a/tests/_plugins/ui/_impl/dataframes/test_handlers.py b/tests/_plugins/ui/_impl/dataframes/test_handlers.pyindex 0312c6152b..95e2afca2e 100644--- a/tests/_plugins/ui/_impl/dataframes/test_handlers.py+++ b/tests/_plugins/ui/_impl/dataframes/test_handlers.py@@ -1921,9 +1921,6 @@ class TestTransformHandler:assert_frame_equal_with_nans(result, expected)@staticmethod- @pytest.mark.xfail(- reason="NaN filtering for object dtypes in pandas aren't implemented"- )@pytest.mark.parametrize(("df", "expected"),list(@@ -1931,9 +1928,7 @@ class TestTransformHandler:create_test_dataframes({"nulls": [1, 2, 3, None, "hello"]}, include=["pandas"]),- create_test_dataframes(- {"nulls": [float("nan")]}, include=["pandas"]- ),+ create_test_dataframes({"nulls": [None]}, include=["pandas"]),)),)@@ -1947,12 +1942,12 @@ class TestTransformHandler:Condition(column_id="nulls",operator="in",- value=[NAN_VALUE],+ value=[None],)],)result = apply(df, in_transform)- assert_frame_equal(result, expected)+ assert_frame_equal_with_nans(result, expected)@staticmethod@pytest.mark.parametrize(