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: narwhals.pyarrow does not support mean/quantile (#4860)

Myles Scolnick646536c0364a
diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
index 2fb91f96d9..e29111b527 100644
--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py
+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
@@ -307,7 +307,7 @@ class NarwhalsTableManager(
data = data.collect()
col = data[column]
- total = len(col)
+ total = col.shape[0]
if is_narwhals_string_type(col.dtype):
return ColumnSummary(
total=total,
@@ -322,6 +322,15 @@ class NarwhalsTableManager(
false=cast(int, total - col.sum()),
)
if (col.dtype == nw.Date) or is_narwhals_time_type(col.dtype):
+ # Arrow does not support mean or quantile
+ if self.data.implementation.is_pyarrow():
+ return ColumnSummary(
+ total=total,
+ nulls=col.null_count(),
+ min=col.min(),
+ max=col.max(),
+ )
+
return ColumnSummary(
total=total,
nulls=col.null_count(),
@@ -348,6 +357,14 @@ class NarwhalsTableManager(
mean=str(res.mean()) + unit,
)
if is_narwhals_temporal_type(col.dtype):
+ # Arrow does not support mean or quantile
+ if self.data.implementation.is_pyarrow():
+ return ColumnSummary(
+ total=total,
+ nulls=col.null_count(),
+ min=col.min(),
+ max=col.max(),
+ )
return ColumnSummary(
total=total,
nulls=col.null_count(),
diff --git a/tests/_plugins/ui/_impl/tables/test_narwhals.py b/tests/_plugins/ui/_impl/tables/test_narwhals.py
index 3ff7ab49bc..6005bb97a6 100644
--- a/tests/_plugins/ui/_impl/tables/test_narwhals.py
+++ b/tests/_plugins/ui/_impl/tables/test_narwhals.py
@@ -860,6 +860,55 @@ def test_dataframe_with_mixed_types(df: Any) -> None:
assert manager.get_field_type("A") == ("string", "String")
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+def test_get_summary_all_types() -> None:
+ dfs = create_dataframes(
+ {
+ "integer": [1, 2, 3],
+ "float": [1.1, 2.2, 3.3],
+ "string": ["a", "b", "c"],
+ "boolean": [True, False, True],
+ "datetime": [
+ datetime.datetime(2021, 1, 1),
+ datetime.datetime(2021, 1, 2),
+ datetime.datetime(2021, 1, 3),
+ ],
+ "date": [
+ datetime.date(2021, 1, 1),
+ datetime.date(2021, 1, 2),
+ datetime.date(2021, 1, 3),
+ ],
+ "list": [["a", "b"], ["c", "d"], ["e", "f"]],
+ "null": [None, None, None],
+ "duration": [
+ datetime.timedelta(days=1),
+ datetime.timedelta(hours=2),
+ datetime.timedelta(minutes=3),
+ ],
+ },
+ exclude=["duckdb", "ibis"],
+ )
+
+ error_count = 0
+ for df in dfs:
+ manager: NarwhalsTableManager[Any] = (
+ NarwhalsTableManager.from_dataframe(df)
+ )
+
+ for column in manager.get_column_names():
+ try:
+ summary = manager._get_summary_internal(column)
+ assert isinstance(summary, ColumnSummary)
+ assert summary.total == 3
+ except Exception as e:
+ error_count += 1
+ print(f"Error getting summary for column {column}: {e}")
+
+ assert error_count == 0, (
+ f"Got {error_count} errors when getting column summaries"
+ )
+
+
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
@pytest.mark.parametrize(
"df",