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: suppress RuntimeWarning for all-NaN DataFrame columns (#8217)

Ahmad Yassere31a152cd580
diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
index b8b9656fe4..d2c7ff8952 100644
--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py
+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
@@ -517,8 +517,16 @@ class NarwhalsTableManager(
}
)
+ import warnings
+
stats = frame.select(**exprs)
- stats_dict = stats.collect().rows(named=True)[0]
+ with warnings.catch_warnings():
+ warnings.filterwarnings(
+ "ignore",
+ message="Mean of empty slice|Degrees of freedom",
+ category=RuntimeWarning,
+ )
+ stats_dict = stats.collect().rows(named=True)[0]
# Maybe add units to the stats
for key, value in stats_dict.items():
diff --git a/tests/_plugins/ui/_impl/tables/test_narwhals.py b/tests/_plugins/ui/_impl/tables/test_narwhals.py
index 6b3196f879..6facf27cb7 100644
--- a/tests/_plugins/ui/_impl/tables/test_narwhals.py
+++ b/tests/_plugins/ui/_impl/tables/test_narwhals.py
@@ -491,6 +491,19 @@ class TestNarwhalsTableManagerFactory(unittest.TestCase):
assert isinstance(bool_stats.true, int)
assert isinstance(bool_stats.false, int)
+ def test_summary_all_nan_column_no_warning(self) -> None:
+ import warnings
+
+ import polars as pl
+
+ data = pl.DataFrame({"A": [float("nan"), float("nan"), float("nan")]})
+ manager = NarwhalsTableManager.from_dataframe(data)
+ with warnings.catch_warnings():
+ warnings.simplefilter("error")
+ stats = manager.get_stats("A")
+ assert stats is not None
+ assert stats.total == 3
+
def test_sort_values(self) -> None:
sorted_df = self.manager.sort_values(
[SortArgs(by="A", descending=True)]