From 83addc4d710d695e1b75cbcc45d905cca02a6ce8 Mon Sep 17 00:00:00 2001 From: ilan-gold Date: Thu, 1 May 2025 11:12:05 -0400 Subject: [PATCH 1/4] (fix): don't cast to geopandas series if not needed --- xvec/accessor.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/xvec/accessor.py b/xvec/accessor.py index 189fee6..5106b99 100644 --- a/xvec/accessor.py +++ b/xvec/accessor.py @@ -960,11 +960,12 @@ def to_geodataframe( if geometry is None: geometry = df.index.name df = df.reset_index() - # ensure CRS of all columns is preserved for c in df.columns: if c in self._geom_coords_all: - df[c] = gpd.GeoSeries(df[c], crs=self._obj[c].attrs.get("crs", None)) + # As of xarray 2024.4.0, the type is preserved in the case of non-multiindex + if df[c].dtype == "object": + df[c] = gpd.GeoSeries(df[c], crs=self._obj[c].attrs.get("crs", None)) if geometry is not None: if geometry not in self._geom_coords_all: # variable geometry From 86949f3da8bc4703ce69e3c27c7f7e39841c20e6 Mon Sep 17 00:00:00 2001 From: ilan-gold Date: Thu, 1 May 2025 11:33:05 -0400 Subject: [PATCH 2/4] (chore): lint --- xvec/accessor.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/xvec/accessor.py b/xvec/accessor.py index 5106b99..76afc8a 100644 --- a/xvec/accessor.py +++ b/xvec/accessor.py @@ -965,7 +965,9 @@ def to_geodataframe( if c in self._geom_coords_all: # As of xarray 2024.4.0, the type is preserved in the case of non-multiindex if df[c].dtype == "object": - df[c] = gpd.GeoSeries(df[c], crs=self._obj[c].attrs.get("crs", None)) + df[c] = gpd.GeoSeries( + df[c], crs=self._obj[c].attrs.get("crs", None) + ) if geometry is not None: if geometry not in self._geom_coords_all: # variable geometry From 8caa6a1a56c9de163a330b851c39ae7b75fb8a09 Mon Sep 17 00:00:00 2001 From: ilan-gold Date: Thu, 1 May 2025 11:34:43 -0400 Subject: [PATCH 3/4] (fix): correct message --- xvec/accessor.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xvec/accessor.py b/xvec/accessor.py index 76afc8a..bfea535 100644 --- a/xvec/accessor.py +++ b/xvec/accessor.py @@ -963,7 +963,8 @@ def to_geodataframe( # ensure CRS of all columns is preserved for c in df.columns: if c in self._geom_coords_all: - # As of xarray 2024.4.0, the type is preserved in the case of non-multiindex + # Even though xarray supports extension arrays as coords as of 2024.4.0, it is still possible + # that people will construct objects that have a geometry column with object dtype. if df[c].dtype == "object": df[c] = gpd.GeoSeries( df[c], crs=self._obj[c].attrs.get("crs", None) From 00413266942282bed035cca82e92e184f8cecf7c Mon Sep 17 00:00:00 2001 From: Martin Fleischmann Date: Sat, 3 May 2025 14:25:01 +0200 Subject: [PATCH 4/4] ensure we always get a GeoSeries --- xvec/accessor.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/xvec/accessor.py b/xvec/accessor.py index bfea535..5357a44 100644 --- a/xvec/accessor.py +++ b/xvec/accessor.py @@ -963,12 +963,13 @@ def to_geodataframe( # ensure CRS of all columns is preserved for c in df.columns: if c in self._geom_coords_all: - # Even though xarray supports extension arrays as coords as of 2024.4.0, it is still possible - # that people will construct objects that have a geometry column with object dtype. - if df[c].dtype == "object": - df[c] = gpd.GeoSeries( - df[c], crs=self._obj[c].attrs.get("crs", None) - ) + # there is a geopandas bug that does not allow upcasting a series + # with a geometry dtype directly + df[c] = gpd.GeoSeries( + df[c].values, + crs=self._obj[c].attrs.get("crs", None), + index=df[c].index, + ) if geometry is not None: if geometry not in self._geom_coords_all: # variable geometry