Skip to content

Encoded cube to zarr fails with TypeError: Cannot interpret '<geopandas.array.GeometryDtype object... #126

Closed
xarray-contrib/cf-xarray
#573

Description

@jakenotjay

Using example code from "Reading and writing files".

Code

import xarray as xr
import geopandas as gpd
import xvec
from geodatasets import get_path

counties = gpd.read_file(get_path("geoda.natregimes"))

cube = xr.Dataset(
    data_vars=dict(
        population=(["county", "year"], counties[["PO60", "PO70", "PO80", "PO90"]]),
        unemployment=(["county", "year"], counties[["UE60", "UE70", "UE80", "UE90"]]),
        divorce=(["county", "year"], counties[["DV60", "DV70", "DV80", "DV90"]]),
        age=(["county", "year"], counties[["MA60", "MA70", "MA80", "MA90"]]),
    ),
    coords=dict(county=counties.geometry, year=[1960, 1970, 1980, 1990]),
).xvec.set_geom_indexes("county", crs=counties.crs)

encoded_cube = cube.xvec.encode_cf()

encoded_cube.to_zarr("stac_cube.zarr", mode="w")

Traceback

Traceback (most recent call last):
  File "/Users/redacted/stac_icechunk.py", line 81, in <module>
    encoded_cube.to_zarr("stac_cube.zarr", mode="w")
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/redacted/venv/lib/python3.13/site-packages/xarray/core/dataset.py", line 2292, in to_zarr
    return to_zarr(  # type: ignore[call-overload,misc]
        self,
    ...<15 lines>...
        chunkmanager_store_kwargs=chunkmanager_store_kwargs,
    )
  File "/Users/redacted/venv/lib/python3.13/site-packages/xarray/backends/api.py", line 2245, in to_zarr
    dump_to_store(dataset, zstore, writer, encoding=encoding)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/redacted/venv/lib/python3.13/site-packages/xarray/backends/api.py", line 1975, in dump_to_store
    store.store(variables, attrs, check_encoding, writer, unlimited_dims=unlimited_dims)
    ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/redacted/venv/lib/python3.13/site-packages/xarray/backends/zarr.py", line 935, in store
    variables_encoded, attributes = self.encode(
                                    ~~~~~~~~~~~^
        {vn: variables[vn] for vn in new_variable_names}, attributes
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/Users/redacted/venv/lib/python3.13/site-packages/xarray/backends/common.py", line 392, in encode
    variables = {k: self.encode_variable(v) for k, v in variables.items()}
                    ~~~~~~~~~~~~~~~~~~~~^^^
  File "/Users/redacted/venv/lib/python3.13/site-packages/xarray/backends/zarr.py", line 859, in encode_variable
    variable = encode_zarr_variable(variable)
  File "/Users/redacted/venv/lib/python3.13/site-packages/xarray/backends/zarr.py", line 458, in encode_zarr_variable
    var = conventions.encode_cf_variable(var, name=name)
  File "/Users/redacted/venv/lib/python3.13/site-packages/xarray/conventions.py", line 102, in encode_cf_variable
    var = coder.encode(var, name=name)
  File "/Users/redacted/venv/lib/python3.13/site-packages/xarray/coding/times.py", line 1361, in encode
    if np.issubdtype(
       ~~~~~~~~~~~~~^
        variable.data.dtype, np.datetime64
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ) or contains_cftime_datetimes(variable):
    ^
  File "/Users/redacted/venv/lib/python3.13/site-packages/numpy/_core/numerictypes.py", line 530, in issubdtype
    arg1 = dtype(arg1).type
           ~~~~~^^^^^^
TypeError: Cannot interpret '<geopandas.array.GeometryDtype object at 0x10bd21400>' as a data type

Versioning

python==3.13.3
xarray==2025.6.1
xvec==0.4.0
zarr==3.0.8
geopandas==1.1.0
geodatasets==2024.8.0

Activity

  1. jakenotjay commented on Jun 19, 2025

    @jakenotjay
    ContributorAuthor
  2. martinfleis commented on Jun 19, 2025

    @martinfleis
    Member

    @dcherian is it possible that this check in cf_xarray is failing now, returning the dataset as is? Because it seems that encoded_cube in the snippet above still has the geometry.

    https://github.com/xarray-contrib/cf-xarray/blob/4be96ff2e40e7a7fc7ff6d57744c4a42b6cbaa94/cf_xarray/geometry.py#L298-L304

  3. dcherian commented on Jun 20, 2025

    @dcherian
    Contributor

    Just released cf-xarray v0.10.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions