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: better support for geopandas in mo.ui.dataframe and table (#5674)
Myles Scolnickf55d9cf0b3b7
diff --git a/marimo/_plugins/ui/_impl/tables/ibis_table.py b/marimo/_plugins/ui/_impl/tables/ibis_table.pyindex 34fe74c5b2..cf5a759fc8 100644--- a/marimo/_plugins/ui/_impl/tables/ibis_table.py+++ b/marimo/_plugins/ui/_impl/tables/ibis_table.py@@ -32,6 +32,7 @@ class IbisTableManagerFactory(TableManagerFactory):return "ibis"@staticmethod+ @functools.lru_cache(maxsize=1)def create() -> type[TableManager[Any]]:import ibis # type: ignorediff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.pyindex 2fbd59a23b..663b3d1d76 100644--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py@@ -1,14 +1,16 @@# Copyright 2024 Marimo. All rights reserved.from __future__ import annotations+import functoolsimport iofrom functools import cached_property-from typing import Any, Optional+from typing import TYPE_CHECKING, Any, Optionalimport narwhals.stable.v1 as nwfrom marimo import _loggersfrom marimo._data.models import ExternalDataType+from marimo._dependencies.dependencies import DependencyManagerfrom marimo._output.data.data import sanitize_json_bigintfrom marimo._plugins.ui._impl.tables.format import (FormatMapping,@@ -22,15 +24,31 @@ from marimo._plugins.ui._impl.tables.table_manager import (TableManagerFactory,)+if TYPE_CHECKING:+ import pandas as pd+LOGGER = _loggers.marimo_logger()+def _maybe_convert_geopandas_to_pandas(data: pd.DataFrame) -> pd.DataFrame:+ # Convert to pandas dataframe since geopandas will fail on+ # certain operations (like to_json(orient="records"))+ if DependencyManager.geopandas.has():+ import geopandas as gpd # type: ignore+ import pandas as pd++ if isinstance(data, gpd.GeoDataFrame):+ return pd.DataFrame(data)+ return data++class PandasTableManagerFactory(TableManagerFactory):@staticmethoddef package_name() -> str:return "pandas"@staticmethod+ @functools.lru_cache(maxsize=1)def create() -> type[TableManager[Any]]:import pandas as pd@@ -38,6 +56,7 @@ class PandasTableManagerFactory(TableManagerFactory):type = "pandas"def __init__(self, data: pd.DataFrame) -> None:+ data = _maybe_convert_geopandas_to_pandas(data)self._original_data = self._handle_multi_col_indexes(data)super().__init__(nw.from_native(self._original_data))diff --git a/marimo/_plugins/ui/_impl/tables/polars_table.py b/marimo/_plugins/ui/_impl/tables/polars_table.pyindex 580128a4ef..fe95574ef0 100644--- a/marimo/_plugins/ui/_impl/tables/polars_table.py+++ b/marimo/_plugins/ui/_impl/tables/polars_table.py@@ -1,6 +1,7 @@# Copyright 2024 Marimo. All rights reserved.from __future__ import annotations+import functoolsimport iofrom functools import cached_propertyfrom typing import Any, Optional, Union@@ -32,6 +33,7 @@ class PolarsTableManagerFactory(TableManagerFactory):return "polars"@staticmethod+ @functools.lru_cache(maxsize=1)def create() -> type[TableManager[Any]]:import polars as pldiff --git a/marimo/_smoke_tests/third_party/pandas/transform_edgecases.py b/marimo/_smoke_tests/third_party/pandas/transform_edgecases.pynew file mode 100644index 0000000000..659d397a65--- /dev/null+++ b/marimo/_smoke_tests/third_party/pandas/transform_edgecases.py@@ -0,0 +1,102 @@+# /// script+# requires-python = ">=3.12"+# dependencies = [+# "geodatasets==2024.8.0",+# "geopandas==1.1.1",+# "mapclassify==2.10.0",+# "marimo",+# "matplotlib==3.10.3",+# "pandas==2.3.1",+# "polars==1.31.0",+# ]+# ///++import marimo++__generated_with = "0.14.11"+app = marimo.App(width="medium")+++def _():+ import marimo as mo+ import pandas as pd+ return mo, pd+++def _(mo, pd):+ # https://github.com/marimo-team/marimo/issues/5445+ df = pd.read_csv(+ "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"+ )+ float64_cols = df.select_dtypes(include="float64").columns+ df[float64_cols] = df[float64_cols].astype("Float64")+ object_cols = df.select_dtypes(include=["object"]).columns+ df[object_cols] = df[object_cols].astype("string")+ df+ mo.ui.dataframe(df)+ return (df,)+++def _(df):+ df.dtypes+ return+++def _():+ import geopandas as gpd+ from geodatasets import get_path+ import polars as pl++ path_to_data = get_path("nybb")+ gdf = gpd.read_file(path_to_data)+ return gdf, pl+++def _(gdf):+ # This is ugly+ gdf+ return+++def _(gdf):+ # This interactive leaflet map does work like a charm+ gdf.explore()+ return+++def _(gdf, mo):+ # https://github.com/marimo-team/marimo/issues/5447+ mo.ui.table(gdf)+ return+++def _(gdf):+ type(gdf)+ return+++def _(gdf, mo):+ # This should not fail+ # https://github.com/marimo-team/marimo/issues/5447+ mo.ui.dataframe(gdf)+ return+++def _(gdf, mo, pl):+ pl_df = pl.DataFrame(gdf.assign(geometry=gdf.geometry.astype(str)))+ mo.ui.dataframe(pl_df)+ return+++if __name__ == "__main__":+ app.run()