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
cast list of enums to string preemptively for polars (#7058)
Shahmir Varqha02f58aaad592
diff --git a/marimo/_plugins/ui/_impl/tables/polars_table.py b/marimo/_plugins/ui/_impl/tables/polars_table.pyindex 3c59580e44..7fae7c7990 100644--- a/marimo/_plugins/ui/_impl/tables/polars_table.py+++ b/marimo/_plugins/ui/_impl/tables/polars_table.py@@ -118,6 +118,15 @@ class PolarsTableManagerFactory(TableManagerFactory):result = self._convert_time_to_string(result, column)+ # https://github.com/marimo-team/marimo/issues/7032+ # Polars issue with ordering and write_json for enums, so we convert to strings+ 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))+ )return sanitize_json_bigint(result.write_json())except (BaseException@@ -154,16 +163,6 @@ class PolarsTableManagerFactory(TableManagerFactory):result, column)converted_columns.append(column.name)- # https://github.com/marimo-team/marimo/issues/5562- 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 safe values.",diff --git a/tests/_plugins/ui/_impl/tables/test_polars_table.py b/tests/_plugins/ui/_impl/tables/test_polars_table.pyindex 1f80452b33..0e1862b74d 100644--- a/tests/_plugins/ui/_impl/tables/test_polars_table.py+++ b/tests/_plugins/ui/_impl/tables/test_polars_table.py@@ -3,6 +3,7 @@ from __future__ import annotationsimport datetimeimport jsonimport unittest+from enum import Enumfrom math import isnanfrom typing import Any@@ -995,3 +996,35 @@ class TestPolarsTableManagerFactory(unittest.TestCase):data_list = pl.DataFrame(data, schema={"A": pl.List(pl.Categorical())})data_list.write_json()++ @pytest.mark.xfail(+ reason="Polars does not properly order sliced data to json when enums in list"+ )+ def test_failing_enums_list(self) -> None:+ import polars as pl++ class MyEnum(Enum):+ A = 1+ B = 2+ C = 3+ D = 4++ # Create 10 rows cycling through enum values B, C, D, A...+ enum_names = [e.name for e in MyEnum]+ rows = [+ {"value": [enum_names[i % len(enum_names)]]} for i in range(1, 11)+ ]++ expected_first_five = '[{"value":["B"]},{"value":["C"]},{"value":["D"]},{"value":["A"]},{"value":["B"]}]'+ expected_second_five = '[{"value":["C"]},{"value":["D"]},{"value":["A"]},{"value":["B"]},{"value":["C"]}]'++ # Test without schema - works fine+ df = pl.DataFrame(rows)+ assert df[0:5].write_json() == expected_first_five+ assert df[5:10].write_json() == expected_second_five++ # Test with schema - second slice fails+ schema = {"value": pl.List(pl.Enum(enum_names))}+ df_schema = pl.DataFrame(rows, schema=schema)+ assert df_schema[0:5].write_json() == expected_first_five+ assert df_schema[5:10].write_json() == expected_second_five # failsdiff --git a/tests/_plugins/ui/_impl/test_table.py b/tests/_plugins/ui/_impl/test_table.pyindex b79a839cc9..408feacc55 100644--- a/tests/_plugins/ui/_impl/test_table.py+++ b/tests/_plugins/ui/_impl/test_table.py@@ -3,6 +3,7 @@ from __future__ import annotationsimport jsonfrom datetime import date+from enum import Enumfrom typing import TYPE_CHECKING, Anyfrom unittest.mock import patch@@ -2399,3 +2400,41 @@ def test_cell_search_df_hover_texts_sorted(df: Any):"4": {"column_0": "hover:carrots"},"5": {"column_0": "hover:carrots"},}+++ not DependencyManager.polars.has(), reason="Polars not installed"+)+def test_polars_enums_in_list():+ import polars as pl++ class MyEnum(Enum):+ A = 1+ B = 2+ C = 3+ D = 4++ # Create 10 rows cycling through enum values B, C, D, A...+ enum_names = [e.name for e in MyEnum]+ rows = [{"value": [enum_names[i % len(enum_names)]]} for i in range(1, 11)]++ schema = {"value": pl.List(pl.Enum(enum_names))}+ df = pl.DataFrame(rows, schema=schema)++ table = ui.table(df, selection=None)++ # First page+ response = table._search(SearchTableArgs(page_size=5, page_number=0))+ assert (+ response.data+ == '[{"value":["B"]},{"value":["C"]},{"value":["D"]},{"value":["A"]},{"value":["B"]}]'+ )++ # Second page+ response_next_page = table._search(+ SearchTableArgs(page_size=5, page_number=1)+ )+ assert (+ response_next_page.data+ == '[{"value":["C"]},{"value":["D"]},{"value":["A"]},{"value":["B"]},{"value":["C"]}]'+ )