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: handle mixed-type column sorting in data table (#9102)
Kiran Gadhaveaf1e820c56ff
diff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.pyindex 17914cbdbf..47568a8e13 100644--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py@@ -29,6 +29,8 @@ from marimo._plugins.ui._impl.tables.table_manager import (if TYPE_CHECKING:import pandas as pd+ from marimo._plugins.ui._impl.table import SortArgs+LOGGER = _loggers.marimo_logger()if TYPE_CHECKING:@@ -107,6 +109,51 @@ class PandasTableManagerFactory(TableManagerFactory):def schema(self) -> pd.Series[Any]:return self._original_data.dtypes # type: ignore+ def sort_values(self, by: list[SortArgs]) -> TableManager[Any]:+ if not by:+ return self++ columns = [sort_arg.by for sort_arg in by]+ descending = [sort_arg.descending for sort_arg in by]++ # Object-dtype columns may contain mixed Python types+ # (e.g. int + str) that can't be compared directly.+ # Cast those to string via temp columns before sorting.+ dtypes = self._original_data.dtypes+ mixed_cols = [+ col for col in columns if dtypes[col] == "object"+ ]++ if not mixed_cols:+ return super().sort_values(by)++ df = self.data+ temp_cols: list[str] = []+ sort_cols: list[str] = []+ for col in columns:+ if col in mixed_cols:+ temp = f"__sort_{col}"+ # Preserve nulls so nulls_last=True works.+ # On pandas <3.0, cast(String) turns None+ # into the string "None" instead of null.+ df = df.with_columns(+ nw.when(nw.col(col).is_null())+ .then(None)+ .otherwise(nw.col(col).cast(nw.String))+ .alias(temp)+ )+ temp_cols.append(temp)+ sort_cols.append(temp)+ else:+ sort_cols.append(col)++ df = df.sort(+ sort_cols,+ descending=descending,+ nulls_last=True,+ ).drop(temp_cols)+ return self.with_new_data(df)+# We override narwhals's to_csv_str to handle pandas# headersdef to_csv_str(diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.pyindex 00311a9008..a11412b79e 100644--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py@@ -1652,6 +1652,46 @@ class TestPandasTableManager(unittest.TestCase):last = sorted_manager.data["A"][-1]assert last is None or isnan(last)+ def test_sort_values_with_mixed_types(self) -> None:+ """Sorting a column with mixed types (int, str, float, bool, None)+ should not raise, falling back to string comparison."""+ df = pd.DataFrame(+ {+ "mixed": [42, "hello", 3.14, True, None, "world", 7],+ "normal": [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0],+ }+ )+ manager = self.factory.create()(df)++ # descending+ sorted_manager = manager.sort_values(+ [SortArgs(by="mixed", descending=True)]+ )+ assert sorted_manager.get_num_rows() == 7+ values = sorted_manager.data["mixed"].to_list()+ assert values[-1] is None or (+ isinstance(values[-1], float) and isnan(values[-1])+ )++ # ascending+ sorted_manager = manager.sort_values(+ [SortArgs(by="mixed", descending=False)]+ )+ assert sorted_manager.get_num_rows() == 7+ values = sorted_manager.data["mixed"].to_list()+ assert values[-1] is None or (+ isinstance(values[-1], float) and isnan(values[-1])+ )++ # multi-column sort with one mixed column+ sorted_manager = manager.sort_values(+ [+ SortArgs(by="mixed", descending=False),+ SortArgs(by="normal", descending=False),+ ]+ )+ assert sorted_manager.get_num_rows() == 7+def test_dataframe_with_multiindex(self) -> None:df = pd.DataFrame({"A": [1, 2, 3, 4], "B": [5, 6, 7, 8]},