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: Serialize geopandas geometry columns as WKT strings (#7923)
Myles Scolnick36b99f2e8d2d
diff --git a/.github/workflows/dev_build.yaml b/.github/workflows/dev_build.yamlindex c5cc6e95a9..274ee57ed7 100644--- a/.github/workflows/dev_build.yaml+++ b/.github/workflows/dev_build.yaml@@ -17,9 +17,11 @@ jobs:runs-on: ${{ matrix.os }}steps:- uses: actions/checkout@v4- - uses: prefix-dev/[email protected]+ - uses: prefix-dev/[email protected]with:pixi-version: v0.62.2+ locked: false+ frozen: truecache: true- run: pixi run make check-prereqs- run: pixi run make fediff --git a/marimo/_messaging/msgspec_encoder.py b/marimo/_messaging/msgspec_encoder.pyindex 905873e640..fea3ca5719 100644--- a/marimo/_messaging/msgspec_encoder.py+++ b/marimo/_messaging/msgspec_encoder.py@@ -116,6 +116,19 @@ def enc_hook(obj: Any) -> Any:except AttributeError:pass+ # Handle shapely geometry objects from geopandas+ if DependencyManager.geopandas.imported():+ try:+ # Check if it's a shapely geometry object+ # shapely.geometry.base.BaseGeometry is the base class+ from shapely.geometry.base import BaseGeometry # type: ignore++ if isinstance(obj, BaseGeometry):+ # Convert to WKT (Well-Known Text) string representation+ return str(obj)+ except (ImportError, AttributeError):+ pass+if DependencyManager.polars.imported():import polars as pldiff --git a/marimo/_plugins/ui/_impl/tables/pandas_table.py b/marimo/_plugins/ui/_impl/tables/pandas_table.pyindex 99f06ff393..1af1624e84 100644--- a/marimo/_plugins/ui/_impl/tables/pandas_table.py+++ b/marimo/_plugins/ui/_impl/tables/pandas_table.py@@ -38,7 +38,7 @@ if TYPE_CHECKING: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():+ if DependencyManager.geopandas.imported():import geopandas as gpd # type: ignoreimport pandas as pddiff --git a/pixi.lock b/pixi.lockindex f61ae758d8..0912bcea3e 100644--- a/pixi.lock+++ b/pixi.lock@@ -1133,7 +1133,7 @@ packages:- pypi: ./name: marimoversion: 0.19.4- sha256: 0f6da2ae122ccc9a0a8b91226e8605a8094365ed84ced7d470f1b70730a22140+ sha256: 8d121008dd6aa452b389d1132e1e5876c0aa09505840255a9b1987f19700d309requires_dist:- click>=8.0,<9- jedi>=0.18.0diff --git a/pyproject.toml b/pyproject.tomlindex 89abd4f473..048274cebf 100644--- a/pyproject.toml+++ b/pyproject.toml@@ -178,8 +178,7 @@ test-optional = ["redshift-connector[full]>=2.1.7","pandas>=1.5.3","hvplot~=0.11.3",- # Causes issues installing `shapely` (v2.0.7)- # "geopandas~=1.0.1",+ "geopandas>=1.1.0","duckdb>=1.0.0","ibis-framework[duckdb]>=10.3.0",# For testing mo.imagediff --git a/tests/_plugins/ui/_impl/snapshots/parse_spec_geopandas.txt b/tests/_plugins/ui/_impl/snapshots/parse_spec_geopandas.txtindex 72751f7e71..f0f60f5191 100644--- a/tests/_plugins/ui/_impl/snapshots/parse_spec_geopandas.txt+++ b/tests/_plugins/ui/_impl/snapshots/parse_spec_geopandas.txt@@ -21,7 +21,7 @@"type": "nominal"}},- "$schema": "https://vega.github.io/schema/vega-lite/v5.20.1.json",+ "$schema": "https://vega.github.io/schema/vega-lite/v6.1.0.json","datasets": {"data-cc0a8311b413c4168b2c1fac768ce667": [{diff --git a/tests/_plugins/ui/_impl/tables/test_pandas_table.py b/tests/_plugins/ui/_impl/tables/test_pandas_table.pyindex 725881a66f..c092d50a2f 100644--- a/tests/_plugins/ui/_impl/tables/test_pandas_table.py+++ b/tests/_plugins/ui/_impl/tables/test_pandas_table.py@@ -1572,3 +1572,99 @@ class TestPandasTableManager(unittest.TestCase):assert not any(isinstance(col, str) for col in df.columns), ("Original DataFrame columns should NOT be strings")++ @pytest.mark.requires("geopandas")+ def test_geopandas_geometry_serialization(self) -> None:+ """Test that GeoDataFrame geometry columns serialize to readable strings.++ This test reproduces issue #7918 where geometry columns appear as+ empty dicts instead of proper WKT strings.+ """+ import geopandas as gpd # type: ignore+ import pandas as pd++ # Create a simple GeoDataFrame with point geometries+ gdf = gpd.GeoDataFrame(+ {+ "name": ["USA", "China", "India"],+ "pop_est": [331002651, 1439323776, 1380004385],+ "geometry": [+ gpd.points_from_xy([x], [y])[0]+ for x, y in [(-95, 37), (105, 35), (77, 20)]+ ],+ }+ )++ # Convert to pandas DataFrame (this is what marimo does internally)+ df = pd.DataFrame(gdf)+ manager = self.factory.create()(df)++ # Convert to JSON+ json_str = manager.to_json_str()+ json_data = json.loads(json_str)++ # The geometry column should contain readable WKT strings,+ # not empty dicts or object representations+ assert len(json_data) == 3++ # Check that geometry is a string (WKT format)+ for row in json_data:+ assert "geometry" in row+ geometry_value = row["geometry"]++ # Should be a string, not a dict or empty object+ assert isinstance(geometry_value, str), (+ f"Expected geometry to be string, got {type(geometry_value)}: {geometry_value}"+ )++ # Should contain "POINT" (WKT format)+ assert "POINT" in geometry_value, (+ f"Expected geometry to contain 'POINT', got: {geometry_value}"+ )++ # Verify specific geometries+ assert "POINT (-95 37)" in json_data[0]["geometry"]+ assert "POINT (105 35)" in json_data[1]["geometry"]+ assert "POINT (77 20)" in json_data[2]["geometry"]++ @pytest.mark.requires("geopandas")+ def test_geopandas_different_geometry_types(self) -> None:+ """Test serialization of different geometry types (Point, Polygon, LineString)."""+ import geopandas as gpd # type: ignore+ import pandas as pd+ from shapely.geometry import LineString, Point, Polygon++ # Create a GeoDataFrame with different geometry types+ gdf = gpd.GeoDataFrame(+ {+ "name": ["Point", "LineString", "Polygon"],+ "geometry": [+ Point(-95, 37),+ LineString([(-95, 37), (-90, 35), (-85, 38)]),+ Polygon([(-95, 37), (-90, 35), (-85, 38), (-95, 37)]),+ ],+ }+ )++ # Convert to pandas DataFrame+ df = pd.DataFrame(gdf)+ manager = self.factory.create()(df)++ # Convert to JSON+ json_str = manager.to_json_str()+ json_data = json.loads(json_str)++ # Check that all geometry types are serialized as strings+ assert len(json_data) == 3++ # Point geometry+ assert isinstance(json_data[0]["geometry"], str)+ assert "POINT" in json_data[0]["geometry"]++ # LineString geometry+ assert isinstance(json_data[1]["geometry"], str)+ assert "LINESTRING" in json_data[1]["geometry"]++ # Polygon geometry+ assert isinstance(json_data[2]["geometry"], str)+ assert "POLYGON" in json_data[2]["geometry"]diff --git a/tests/_plugins/ui/_impl/test_altair_chart.py b/tests/_plugins/ui/_impl/test_altair_chart.pyindex 7e5ba9c3b9..8349e9f591 100644--- a/tests/_plugins/ui/_impl/test_altair_chart.py+++ b/tests/_plugins/ui/_impl/test_altair_chart.py@@ -857,10 +857,7 @@ def test_parse_spec_duckdb() -> None:snapshot("parse_spec_duckdb.txt", json.dumps(spec, indent=2))- not HAS_DEPS or not DependencyManager.geopandas.has(),- reason="optional dependencies not installed",-)[email protected]("geopandas")def test_parse_spec_geopandas() -> None:import altair as altimport geopandas as gpd