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.

improvement: handle decimal in column hist (#7413)

Myles Scolnick5263b871db2c
diff --git a/marimo/_plugins/ui/_impl/tables/narwhals_table.py b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
index ba3d79ae50..6d0b5f7222 100644
--- a/marimo/_plugins/ui/_impl/tables/narwhals_table.py
+++ b/marimo/_plugins/ui/_impl/tables/narwhals_table.py
@@ -542,6 +542,12 @@ class NarwhalsTableManager(
downgraded_df = downgrade_narwhals_df_to_v1(self.as_frame())
col = downgraded_df.get_column(column)
+ # If the column is decimal, we need to convert it to float
+ if dtype.is_decimal():
+ import narwhals.stable.v1 as nw1
+
+ col = col.cast(nw1.Float64)
+
bin_start = col.min()
bin_values: list[BinValue] = []
diff --git a/marimo/_smoke_tests/tables/decimals.py b/marimo/_smoke_tests/tables/decimals.py
new file mode 100644
index 0000000000..c54776a996
--- /dev/null
+++ b/marimo/_smoke_tests/tables/decimals.py
@@ -0,0 +1,37 @@
+import marimo
+
+__generated_with = "0.18.2"
+app = marimo.App(width="medium")
+
+
+def _():
+ import marimo as mo
+ return
+
+
+def _():
+ import polars as pl
+
+ decimal_data = pl.DataFrame(
+ {"decimals": pl.Series(range(201), dtype=pl.Decimal(scale=2))}
+ )
+ decimal_data
+ return
+
+
+def _():
+ import pandas as pd
+ from decimal import Decimal
+
+ pandas_decimal_data = pd.DataFrame(
+ {"decimals": [Decimal(i) / Decimal("100") for i in range(201)]}
+ )
+ pandas_decimal_data
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/tests/_plugins/ui/_impl/tables/test_narwhals.py b/tests/_plugins/ui/_impl/tables/test_narwhals.py
index 48972b6497..ba29ddadd7 100644
--- a/tests/_plugins/ui/_impl/tables/test_narwhals.py
+++ b/tests/_plugins/ui/_impl/tables/test_narwhals.py
@@ -4,6 +4,7 @@ import datetime
import json
import time
import unittest
+from decimal import Decimal
from math import isnan
from typing import TYPE_CHECKING, Any
@@ -1102,6 +1103,45 @@ def _round_bin_values(bin_values: list[BinValue]) -> list[BinValue]:
]
[email protected](not HAS_DEPS, reason="optional dependencies not installed")
+ "df",
+ create_dataframes(
+ {"decimals": [Decimal(i) for i in range(201)]},
+ exclude=[
+ # Pandas doesn't support decimal types (instead is Object)
+ "pandas"
+ ],
+ ),
+)
+def test_get_bin_values_decimal(df: Any) -> None:
+ """Test that get_bin_values works correctly with decimal columns."""
+ dtype = nw.from_native(df).collect_schema()["decimals"]
+ assert dtype.is_decimal(), (
+ f"Decimal column not found in schema: {dtype} (type {type(df)})"
+ )
+
+ manager = NarwhalsTableManager.from_dataframe(df)
+
+ # This should not raise an error
+ bin_values = manager.get_bin_values("decimals", 5)
+
+ # Verify we got valid bin values
+ assert len(bin_values) == 5
+ assert all(isinstance(bv, BinValue) for bv in bin_values)
+ assert all(isinstance(bv.count, int) for bv in bin_values)
+ assert all(isinstance(bv.bin_start, (int, float)) for bv in bin_values)
+ assert all(isinstance(bv.bin_end, (int, float)) for bv in bin_values)
+
+ # Verify the bins cover the expected range (0 to 200)
+ assert bin_values[0].bin_start == 0.0
+ assert bin_values[-1].bin_end == 200.0
+
+ # Verify all rows are counted
+ total_count = sum(bv.count for bv in bin_values)
+ assert total_count == 201
+
+
@pytest.mark.skipif(not HAS_DEPS, reason="optional dependencies not installed")
@pytest.mark.parametrize(
"df",