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
convert decimals to string (#7341)
Shahmir Varqha9cdd8f122028
diff --git a/marimo/_messaging/msgspec_encoder.py b/marimo/_messaging/msgspec_encoder.pyindex cb8f03037e..6661dd9f4a 100644--- a/marimo/_messaging/msgspec_encoder.py+++ b/marimo/_messaging/msgspec_encoder.py@@ -5,6 +5,7 @@ from __future__ import annotationsimport collectionsimport datetime+import decimalimport fractionsimport uuidfrom math import isnan@@ -34,7 +35,10 @@ def enc_hook(obj: Any) -> Any:if isinstance(obj, range):return list(obj)- if isinstance(obj, (complex, fractions.Fraction, PurePath, uuid.UUID)):+ if isinstance(+ obj,+ (complex, fractions.Fraction, decimal.Decimal, PurePath, uuid.UUID),+ ):return str(obj)if DependencyManager.numpy.imported():diff --git a/marimo/_smoke_tests/tables/complex_types.py b/marimo/_smoke_tests/tables/complex_types.pyindex 3c0b157ac6..2f0a74728e 100644--- a/marimo/_smoke_tests/tables/complex_types.py+++ b/marimo/_smoke_tests/tables/complex_types.py@@ -111,7 +111,7 @@ def _(df):return[email protected](hide_code=True)def _(mo, pd, pl):complex = [1 + 2j, 2 + 3j]@@ -131,7 +131,38 @@ def _(mo, pd, pl):"large_floats": [[125339796295248046.9], [-12533979629524804.69]],})- mo.vstack([additional_types_pd, additional_types_pl])+ mo.vstack([mo.md("#### BigInts"), additional_types_pd, additional_types_pl])+ return++[email protected](hide_code=True)+def _(mo, pd):+ from decimal import Decimal++ _data = {+ "decimals": [+ Decimal("23.546"),+ Decimal("12.431"),+ Decimal("-12.3232"),+ Decimal("NaN"),+ Decimal("Infinity"),+ Decimal("-0"),+ Decimal("-Infinity"),+ ]+ }++ mo.vstack(+ [+ mo.md("#### Decimals"),+ mo.hstack(+ [+ mo.plain(pd.DataFrame(_data)),+ pd.DataFrame(_data),+ ],+ justify="start",+ ),+ ]+ )return@@ -148,7 +179,7 @@ def _(df, mo):return[email protected](hide_code=True)def _(mo, pd, pl):import uuid@@ -164,7 +195,7 @@ def _(mo, pd, pl):uuid_df = pd.DataFrame(uuid_data)uuid_polars = pl.DataFrame(uuid_data)- mo.vstack([mo.plain(uuid_df), uuid_df, uuid_polars])+ mo.vstack([mo.md("#### UUIDs"), mo.plain(uuid_df), uuid_df, uuid_polars])returndiff --git a/tests/_messaging/test_enc_hook.py b/tests/_messaging/test_enc_hook.pyindex 43b26d2bfb..9ecdad4850 100644--- a/tests/_messaging/test_enc_hook.py+++ b/tests/_messaging/test_enc_hook.py@@ -1,5 +1,6 @@# Copyright 2025 Marimo. All rights reserved.+import decimalimport jsonimport pytest@@ -63,3 +64,20 @@ def test_serialize_matplotlib_figure() -> None:image_data = json.loads(result["data"])assert "image/png" in image_dataassert image_data["image/png"].startswith("data:image/png;base64,")+++def test_serialize_decimal() -> None:+ decimal_obj = decimal.Decimal("123.45")+ result = enc_hook(decimal_obj)+ assert result == "123.45"++ # Nans, infinities, and -0+ decimal_obj = decimal.Decimal("NaN")+ result = enc_hook(decimal_obj)+ assert result == "NaN"+ decimal_obj = decimal.Decimal("Infinity")+ result = enc_hook(decimal_obj)+ assert result == "Infinity"+ decimal_obj = decimal.Decimal("-0")+ result = enc_hook(decimal_obj)+ assert result == "-0"diff --git a/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv b/tests/_plugins/ui/_impl/tables/snapshots/pandas.csvindex 2e800ed469..99ee234d2f 100644--- a/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv+++ b/tests/_plugins/ui/_impl/tables/snapshots/pandas.csv@@ -1,4 +1,4 @@-strings,bool,int,float,datetime,date,nanosecond,datetime_index,period_range,interval_range,delta,none,infs,struct,list,nested_lists,objects,nested_objects,nulls,category,set,imaginary,time,duration,mixed_list,bytes-a,True,1,1.0,2021-01-01,2021-01-01,2017-03-22 15:16:45,"DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'], dtype='datetime64[ns]', freq=None)",2017Q1,"(2017-01-01 00:00:00, 2017-01-02 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030'], dtype='timedelta64[ns]', freq=None)",,inf,"{'a': 1, 'b': 2}","[1, 2]","[[1, 2]]","{'a': 1, 'b': 2}","[{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]",,cat,"{1, 2}",(1+2j),12:30:00,1 days,"[1, 'two']",b'\x00\x00\x00\x00\x01\xc0U\xe8\xb1n1\xc0T@D\xf1?Bc\x95\x83'-b,False,2,2.0,2021-01-02,2021-01-02,2017-07-14 02:40:00,"DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None)",2017Q2,"(2017-01-02 00:00:00, 2017-01-03 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500'], dtype='timedelta64[ns]', freq=None)",,inf,"{'a': 3, 'b': 4}","[3, 4]","[[3, 4]]","{'a': 3, 'b': 4}","[{'a': 5, 'b': 6}, {'a': 7, 'b': 8}]",data,dog,"{3, 4}",(3+4j),13:45:00,2 days,"[3.0, False]",b'world'-c,True,3,3.0,2021-01-03,2021-01-03,2017-11-06 20:26:40,"DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None)",2017Q3,"(2017-01-03 00:00:00, 2017-01-04 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500', NaT], dtype='timedelta64[ns]', freq=None)",,-inf,"{'a': 5, 'b': 6}","[5, 6]","[[5, 6]]","{'a': 5, 'b': 6}","[{'a': 9, 'b': 10}, {'a': 11, 'b': 12}]",,mouse,"{5, 6}",(5+6j),14:15:00,3 days,"[None, datetime.datetime(2021, 1, 1, 0, 0)]",b'bytes'+strings,bool,int,float,decimals,datetime,date,nanosecond,datetime_index,period_range,interval_range,delta,none,infs,struct,list,nested_lists,objects,nested_objects,nulls,category,set,imaginary,time,duration,mixed_list,bytes+a,True,1,1.0,-1.23,2021-01-01,2021-01-01,2017-03-22 15:16:45,"DatetimeIndex(['1960-01-02', '1960-01-03', '1960-01-04'], dtype='datetime64[ns]', freq=None)",2017Q1,"(2017-01-01 00:00:00, 2017-01-02 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030'], dtype='timedelta64[ns]', freq=None)",,inf,"{'a': 1, 'b': 2}","[1, 2]","[[1, 2]]","{'a': 1, 'b': 2}","[{'a': 1, 'b': 2}, {'a': 3, 'b': 4}]",,cat,"{1, 2}",(1+2j),12:30:00,1 days,"[1, 'two']",b'\x00\x00\x00\x00\x01\xc0U\xe8\xb1n1\xc0T@D\xf1?Bc\x95\x83'+b,False,2,2.0,,2021-01-02,2021-01-02,2017-07-14 02:40:00,"DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None)",2017Q2,"(2017-01-02 00:00:00, 2017-01-03 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500'], dtype='timedelta64[ns]', freq=None)",,inf,"{'a': 3, 'b': 4}","[3, 4]","[[3, 4]]","{'a': 3, 'b': 4}","[{'a': 5, 'b': 6}, {'a': 7, 'b': 8}]",data,dog,"{3, 4}",(3+4j),13:45:00,2 days,"[3.0, False]",b'world'+c,True,3,3.0,Infinity,2021-01-03,2021-01-03,2017-11-06 20:26:40,"DatetimeIndex(['1970-01-02', '1970-01-03', '1970-01-04'], dtype='datetime64[ns]', freq=None)",2017Q3,"(2017-01-03 00:00:00, 2017-01-04 00:00:00]","TimedeltaIndex(['1 days 06:05:01.000030', '0 days 00:00:00.000015500', NaT], dtype='timedelta64[ns]', freq=None)",,-inf,"{'a': 5, 'b': 6}","[5, 6]","[[5, 6]]","{'a': 5, 'b': 6}","[{'a': 9, 'b': 10}, {'a': 11, 'b': 12}]",,mouse,"{5, 6}",(5+6j),14:15:00,3 days,"[None, datetime.datetime(2021, 1, 1, 0, 0)]",b'bytes'diff --git a/tests/_plugins/ui/_impl/tables/snapshots/pandas.field_types.json b/tests/_plugins/ui/_impl/tables/snapshots/pandas.field_types.jsonindex 5c6e16b1a5..abd7e98adf 100644--- a/tests/_plugins/ui/_impl/tables/snapshots/pandas.field_types.json+++ b/tests/_plugins/ui/_impl/tables/snapshots/pandas.field_types.json@@ -27,6 +27,13 @@"float64"]],+ [+ "decimals",+ [+ "string",+ "object"+ ]+ ],["datetime",[diff --git a/tests/_plugins/ui/_impl/tables/snapshots/pandas.json b/tests/_plugins/ui/_impl/tables/snapshots/pandas.jsonindex bfd4a96c76..03c5f9ad5e 100644--- a/tests/_plugins/ui/_impl/tables/snapshots/pandas.json+++ b/tests/_plugins/ui/_impl/tables/snapshots/pandas.json@@ -4,6 +4,7 @@"bool": true,"int": 1,"float": 1.0,+ "decimals": "-1.23","datetime": "2021-01-01 00:00:00","date": "2021-01-01","nanosecond": "2017-03-22 15:16:45",@@ -67,6 +68,7 @@"bool": false,"int": 2,"float": 2.0,+ "decimals": "NaN","datetime": "2021-01-02 00:00:00","date": "2021-01-02","nanosecond": "2017-07-14 02:40:00",@@ -131,6 +133,7 @@"bool": true,"int": 3,"float": 3.0,+ "decimals": "Infinity","datetime": "2021-01-03 00:00:00","date": "2021-01-03","nanosecond": "2017-11-06 20:26:40",diff --git a/tests/_plugins/ui/_impl/tables/snapshots/polars.csv b/tests/_plugins/ui/_impl/tables/snapshots/polars.csvindex 197de2eb7f..10578f3ebe 100644--- a/tests/_plugins/ui/_impl/tables/snapshots/polars.csv+++ b/tests/_plugins/ui/_impl/tables/snapshots/polars.csv@@ -1,4 +1,4 @@-strings,bool,int,large_int,float,datetime,date,struct,list,array,nulls,category,set,imaginary,time,duration,nans,infs,mixed_list,enum_list-a,true,1,18446744073709551616,1.0,2021-01-01T00:00:00.000000,2021-01-01,"{""a"":1,""b"":2}","1,2",1,,cat,"{1, 2}",(1+2j),12:30:00.000000000,1d,NaN,inf,"1,two","A,B,C"-b,false,2,36893488147419103233,2.0,2021-01-02T00:00:00.000000,2021-01-02,"{""a"":3,""b"":4}","3,4",2,data,dog,"{3, 4}",(3+4j),13:45:00.000000000,315µs,NaN,-inf,"3.0,0.0","A,B,C"-c,true,3,73786976294838206466,3.0,2021-01-03T00:00:00.000000,2021-01-03,"{""a"":5,""b"":6}","5,6",3,,mouse,"{5, 6}",(5+6j),14:15:00.000000000,2h 30m,NaN,inf,2021-01-01 00:00:00.000000,"A,B,C"+strings,bool,int,large_int,float,decimals,datetime,date,struct,list,array,nulls,category,set,imaginary,time,duration,nans,infs,mixed_list,enum_list+a,true,1,18446744073709551616,1.0,1.23,2021-01-01T00:00:00.000000,2021-01-01,"{""a"":1,""b"":2}","1,2",1,,cat,"{1, 2}",(1+2j),12:30:00.000000000,1d,NaN,inf,"1,two","A,B,C"+b,false,2,36893488147419103233,2.0,4.56,2021-01-02T00:00:00.000000,2021-01-02,"{""a"":3,""b"":4}","3,4",2,data,dog,"{3, 4}",(3+4j),13:45:00.000000000,315µs,NaN,-inf,"3.0,0.0","A,B,C"+c,true,3,73786976294838206466,3.0,7.89,2021-01-03T00:00:00.000000,2021-01-03,"{""a"":5,""b"":6}","5,6",3,,mouse,"{5, 6}",(5+6j),14:15:00.000000000,2h 30m,NaN,inf,2021-01-01 00:00:00.000000,"A,B,C"diff --git a/tests/_plugins/ui/_impl/tables/snapshots/polars.field_types.json b/tests/_plugins/ui/_impl/tables/snapshots/polars.field_types.jsonindex 80400d29d6..1d288f334e 100644--- a/tests/_plugins/ui/_impl/tables/snapshots/polars.field_types.json+++ b/tests/_plugins/ui/_impl/tables/snapshots/polars.field_types.json@@ -34,6 +34,13 @@"f64"]],+ [+ "decimals",+ [+ "number",+ "decimal[38,2]"+ ]+ ],["datetime",[diff --git a/tests/_plugins/ui/_impl/tables/snapshots/polars.json b/tests/_plugins/ui/_impl/tables/snapshots/polars.jsonindex a877834dbf..3dff8680b0 100644--- a/tests/_plugins/ui/_impl/tables/snapshots/polars.json+++ b/tests/_plugins/ui/_impl/tables/snapshots/polars.json@@ -7,6 +7,7 @@"$bigint": "18446744073709551616"},"float": 1.0,+ "decimals": "1.23","datetime": "2021-01-01 00:00:00","date": "2021-01-01","struct": {@@ -79,6 +80,7 @@"$bigint": "36893488147419103233"},"float": 2.0,+ "decimals": "4.56","datetime": "2021-01-02 00:00:00","date": "2021-01-02","struct": {@@ -147,6 +149,7 @@"$bigint": "73786976294838206466"},"float": 3.0,+ "decimals": "7.89","datetime": "2021-01-03 00:00:00","date": "2021-01-03","struct": {diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.pyindex 91d70d50eb..7c83754c9b 100644--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py@@ -1,6 +1,7 @@from __future__ import annotationsimport datetime+import decimalimport jsonimport unittestfrom math import isnan, nan@@ -72,6 +73,11 @@ class TestPandasTableManager(unittest.TestCase):"bool": [True, False, True],"int": [1, 2, 3],"float": [1.0, 2.0, 3.0],+ "decimals": [+ decimal.Decimal("-1.23"),+ decimal.Decimal("NaN"),+ decimal.Decimal("Infinity"),+ ],"datetime": [datetime.datetime(2021, 1, 1),datetime.datetime(2021, 1, 2),diff --git a/tests/_plugins/ui/_impl/tables/test_polars_table.py b/tests/_plugins/ui/_impl/tables/test_polars_table.pyindex d1377ed507..e53717127d 100644--- a/tests/_plugins/ui/_impl/tables/test_polars_table.py+++ b/tests/_plugins/ui/_impl/tables/test_polars_table.py@@ -1,6 +1,7 @@from __future__ import annotationsimport datetime+import decimalimport jsonimport mathimport unittest@@ -51,6 +52,11 @@ class TestPolarsTableManagerFactory(unittest.TestCase):"int": [1, 2, 3],"large_int": [2**64, 2**65 + 1, 2**66 + 2],"float": [1.0, 2.0, 3.0],+ "decimals": [+ decimal.Decimal("1.23"),+ decimal.Decimal("4.56"),+ decimal.Decimal("7.89"),+ ],"datetime": [datetime.datetime(2021, 1, 1),datetime.datetime(2021, 1, 2),