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: cast elements in list of enums to string for polars (#5572)
Shahmir Varqhaff0c9fead9ef
diff --git a/marimo/_plugins/ui/_impl/tables/polars_table.py b/marimo/_plugins/ui/_impl/tables/polars_table.pyindex d8c50899be..580128a4ef 100644--- a/marimo/_plugins/ui/_impl/tables/polars_table.py+++ b/marimo/_plugins/ui/_impl/tables/polars_table.py@@ -123,15 +123,17 @@ class PolarsTableManagerFactory(TableManagerFactory):LOGGER.info("Failed to write json. Trying to convert columns to strings.")+ converted_columns = []for column in result.get_columns():dtype = column.dtypeif isinstance(dtype, pl.Object):result = self._cast_object_to_string(result, column)+ converted_columns.append(column.name)elif str(dtype) == "Int128":# Use string comparison because pl.Int128 doesn't exist on older versions- # As of writing this, Int128 is not supported by polars+ # As of writing this, Int128 to json is not supported by polarsLOGGER.warning("Column %s is of type Int128, which is not supported. Converting to string.",column.name,@@ -139,10 +141,27 @@ class PolarsTableManagerFactory(TableManagerFactory):result = result.with_columns(column.cast(pl.String))+ converted_columns.append(column.name)elif isinstance(dtype, pl.Duration):result = self._convert_time_to_string(result, column)+ converted_columns.append(column.name)+ # https://github.com/pola-rs/polars/issues/23459+ elif isinstance(dtype, pl.List) and isinstance(+ dtype.inner, (pl.Enum, pl.Categorical)+ ):+ # Convert each element in the list to a string+ result = result.with_columns(+ pl.col(column.name).cast(pl.List(pl.String))+ )+ converted_columns.append(column.name)++ if converted_columns:+ LOGGER.info(+ "Converted columns %s to string.",+ ", ".join(f"'{col}'" for col in converted_columns),+ )return sanitize_json_bigint(result.write_json())diff --git a/marimo/_smoke_tests/tables/complex_types.py b/marimo/_smoke_tests/tables/complex_types.pyindex ca85d8c713..e991c034fa 100644--- a/marimo/_smoke_tests/tables/complex_types.py+++ b/marimo/_smoke_tests/tables/complex_types.py@@ -1,6 +1,6 @@import marimo-__generated_with = "0.13.15"+__generated_with = "0.14.10"app = marimo.App(width="medium")@@ -45,6 +45,11 @@ def _():[[[1, 2]], [[3, 4]], [[5, 6]]],dtype=pl.List(pl.List(pl.Int64)),),+ "lists_with_enum": pl.Series(+ "lists_with_enum",+ [["A", "B"], ["A", "B"], ["A", "B"]],+ dtype=pl.List(pl.Enum(categories=["A", "B"])),+ ),"arrays": pl.Series("arrays",[[1, 2], [3, 4], [5, 6]],@@ -160,11 +165,11 @@ def _(mo):@app.cell-def _(pl):- wow_data = pl.scan_parquet(- "https://github.com/koaning/wow-avatar-datasets/raw/refs/heads/main/wow-full.parquet"- )- wow_data+def _():+ # wow_data = pl.scan_parquet(+ # "https://github.com/koaning/wow-avatar-datasets/raw/refs/heads/main/wow-full.parquet"+ # )+ # wow_data# wow_data.collect()returndiff --git a/marimo/_utils/requests.py b/marimo/_utils/requests.pyindex a32a3f23c7..3f1978d03d 100644--- a/marimo/_utils/requests.py+++ b/marimo/_utils/requests.py@@ -1,3 +1,4 @@+# Copyright 2025 Marimo. All rights reserved.import jsonimport urllib.errorimport urllib.parsediff --git a/tests/_plugins/ui/_impl/tables/snapshots/polars.csv b/tests/_plugins/ui/_impl/tables/snapshots/polars.csvindex 44fd6f001f..4f92db9851 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,mixed_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,"1,two"-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,"3.0,0.0"-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,2021-01-01 00:00:00.000000+strings,bool,int,large_int,float,datetime,date,struct,list,array,nulls,category,set,imaginary,time,duration,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,"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,"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,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 9450e34ce8..f5447e93d2 100644--- a/tests/_plugins/ui/_impl/tables/snapshots/polars.field_types.json+++ b/tests/_plugins/ui/_impl/tables/snapshots/polars.field_types.json@@ -1,23 +1 @@-[- ["strings", ["string", "str"]],- ["bool", ["boolean", "bool"]],- ["int", ["integer", "i64"]],- ["large_int", ["integer", "i128"]],- ["float", ["number", "f64"]],- ["datetime", ["datetime", "datetime[\u03bcs]"]],- ["date", ["date", "date"]],- ["struct", ["unknown", "struct[2]"]],- ["list", ["unknown", "list[i64]"]],- ["nested_lists", ["unknown", "list[list[i64]]"]],- ["nested_arrays", ["unknown", "array[i64, (1, 2)]"]],- ["array", ["unknown", "array[i64, 1]"]],- ["nulls", ["string", "str"]],- ["category", ["string", "cat"]],- ["set", ["unknown", "object"]],- ["imaginary", ["unknown", "object"]],- ["time", ["time", "Time"]],- ["duration", ["number", "duration[\u03bcs]"]],- ["mixed_list", ["unknown", "list[str]"]],- ["structs_with_list", ["unknown", "struct[2]"]],- ["list_with_structs", ["unknown", "list[struct[2]]"]]-]+[["strings", ["string", "str"]], ["bool", ["boolean", "bool"]], ["int", ["integer", "i64"]], ["large_int", ["integer", "i128"]], ["float", ["number", "f64"]], ["datetime", ["datetime", "datetime[\u03bcs]"]], ["date", ["date", "date"]], ["struct", ["unknown", "struct[2]"]], ["list", ["unknown", "list[i64]"]], ["nested_lists", ["unknown", "list[list[i64]]"]], ["nested_arrays", ["unknown", "array[i64, (1, 2)]"]], ["array", ["unknown", "array[i64, 1]"]], ["nulls", ["string", "str"]], ["category", ["string", "cat"]], ["set", ["unknown", "object"]], ["imaginary", ["unknown", "object"]], ["time", ["time", "Time"]], ["duration", ["number", "duration[\u03bcs]"]], ["mixed_list", ["unknown", "list[str]"]], ["structs_with_list", ["unknown", "struct[2]"]], ["list_with_structs", ["unknown", "list[struct[2]]"]], ["enum_list", ["unknown", "list[enum]"]]]\ No newline at end of filediff --git a/tests/_plugins/ui/_impl/tables/snapshots/polars.json b/tests/_plugins/ui/_impl/tables/snapshots/polars.jsonindex c8dadd231c..abbec67975 100644--- a/tests/_plugins/ui/_impl/tables/snapshots/polars.json+++ b/tests/_plugins/ui/_impl/tables/snapshots/polars.json@@ -1,71 +1 @@-[- {- "strings": "a",- "bool": true,- "int": 1,- "large_int": "18446744073709551616",- "float": 1.0,- "datetime": "2021-01-01 00:00:00",- "date": "2021-01-01",- "struct": { "a": 1, "b": 2 },- "list": [1, 2],- "nested_lists": [[1, 2]],- "nested_arrays": [[1, 2]],- "array": [1],- "nulls": null,- "category": "cat",- "set": "{1, 2}",- "imaginary": "(1+2j)",- "time": "12:30:00",- "duration": "1d",- "mixed_list": ["1", "two"],- "structs_with_list": { "a": [1, 2], "b": 2 },- "list_with_structs": [{ "a": 1, "c": null }, { "a": null, "c": 3 }]- },- {- "strings": "b",- "bool": false,- "int": 2,- "large_int": "36893488147419103233",- "float": 2.0,- "datetime": "2021-01-02 00:00:00",- "date": "2021-01-02",- "struct": { "a": 3, "b": 4 },- "list": [3, 4],- "nested_lists": [[3, 4]],- "nested_arrays": [[3, 4]],- "array": [2],- "nulls": "data",- "category": "dog",- "set": "{3, 4}",- "imaginary": "(3+4j)",- "time": "13:45:00",- "duration": "315µs",- "mixed_list": ["3.0", "0.0"],- "structs_with_list": { "a": [3, 4], "b": 4 },- "list_with_structs": [{ "a": null, "c": null }]- },- {- "strings": "c",- "bool": true,- "int": 3,- "large_int": "73786976294838206466",- "float": 3.0,- "datetime": "2021-01-03 00:00:00",- "date": "2021-01-03",- "struct": { "a": 5, "b": 6 },- "list": [5, 6],- "nested_lists": [[5, 6]],- "nested_arrays": [[5, 6]],- "array": [3],- "nulls": null,- "category": "mouse",- "set": "{5, 6}",- "imaginary": "(5+6j)",- "time": "14:15:00",- "duration": "2h 30m",- "mixed_list": [null, "2021-01-01 00:00:00.000000"],- "structs_with_list": { "a": null, "b": null },- "list_with_structs": []- }-]+[{"strings":"a","bool":true,"int":1,"large_int":"18446744073709551616","float":1.0,"datetime":"2021-01-01 00:00:00","date":"2021-01-01","struct":{"a":1,"b":2},"list":[1,2],"nested_lists":[[1,2]],"nested_arrays":[[1,2]],"array":[1],"nulls":null,"category":"cat","set":"{1, 2}","imaginary":"(1+2j)","time":"12:30:00","duration":"1d","mixed_list":["1","two"],"structs_with_list":{"a":[1,2],"b":2},"list_with_structs":[{"a":1,"c":null},{"a":null,"c":3}],"enum_list":["A","B","C"]},{"strings":"b","bool":false,"int":2,"large_int":"36893488147419103233","float":2.0,"datetime":"2021-01-02 00:00:00","date":"2021-01-02","struct":{"a":3,"b":4},"list":[3,4],"nested_lists":[[3,4]],"nested_arrays":[[3,4]],"array":[2],"nulls":"data","category":"dog","set":"{3, 4}","imaginary":"(3+4j)","time":"13:45:00","duration":"315\u00b5s","mixed_list":["3.0","0.0"],"structs_with_list":{"a":[3,4],"b":4},"list_with_structs":[{"a":null,"c":null}],"enum_list":["A","B","C"]},{"strings":"c","bool":true,"int":3,"large_int":"73786976294838206466","float":3.0,"datetime":"2021-01-03 00:00:00","date":"2021-01-03","struct":{"a":5,"b":6},"list":[5,6],"nested_lists":[[5,6]],"nested_arrays":[[5,6]],"array":[3],"nulls":null,"category":"mouse","set":"{5, 6}","imaginary":"(5+6j)","time":"14:15:00","duration":"2h 30m","mixed_list":[null,"2021-01-01 00:00:00.000000"],"structs_with_list":{"a":null,"b":null},"list_with_structs":[],"enum_list":["A","B","C"]}]\ No newline at end of filediff --git a/tests/_plugins/ui/_impl/tables/test_polars_table.py b/tests/_plugins/ui/_impl/tables/test_polars_table.pyindex b4a7796ed9..e992ba3ceb 100644--- a/tests/_plugins/ui/_impl/tables/test_polars_table.py+++ b/tests/_plugins/ui/_impl/tables/test_polars_table.py@@ -109,6 +109,10 @@ class TestPolarsTableManagerFactory(unittest.TestCase):[],],),+ "enum_list": pl.Series(+ [["A", "B", "C"], ["A", "B", "C"], ["A", "B", "C"]],+ dtype=pl.List(pl.Enum(categories=["A", "B", "C"])),+ ),},strict=False,)@@ -943,3 +947,42 @@ class TestPolarsTableManagerFactory(unittest.TestCase):# Large integers should be converted to stringsassert json_data[1]["A"] == "9007199254740992"assert json_data[1]["B"] == "-9007199254740992"++ def test_to_json_enum_list(self) -> None:+ import polars as pl++ data = {"A": [["A", "B", "C"], ["A", "B", "C"], ["A", "B", "C"]]}++ data_enum = pl.DataFrame(+ data, schema={"A": pl.List(pl.Enum(categories=["A", "B", "C"]))}+ )+ manager = self.factory.create()(data_enum)+ json_data = json.loads(manager.to_json())+ assert json_data[0]["A"] == ["A", "B", "C"]+ assert json_data[1]["A"] == ["A", "B", "C"]+ assert json_data[2]["A"] == ["A", "B", "C"]++ data_categorical = pl.DataFrame(+ data, schema={"A": pl.List(pl.Categorical())}+ )+ manager = self.factory.create()(data_categorical)+ json_data = json.loads(manager.to_json())+ assert json_data[0]["A"] == ["A", "B", "C"]+ assert json_data[1]["A"] == ["A", "B", "C"]+ assert json_data[2]["A"] == ["A", "B", "C"]++ def test_to_json_enum_list_not_supported(self) -> None:+ # When this is supported, we can remove the casting to string+ import polars as pl++ data = {"A": [["A", "B", "C"], ["A", "B", "C"], ["A", "B", "C"]]}++ data_enum = pl.DataFrame(+ data, schema={"A": pl.List(pl.Enum(categories=["A", "B", "C"]))}+ )+ with pytest.raises(pl.exceptions.PanicException):+ data_enum.write_json()++ data_list = pl.DataFrame(data, schema={"A": pl.List(pl.Categorical())})+ with pytest.raises(pl.exceptions.PanicException):+ data_list.write_json()