Repository navigation
Closed
xarray-contrib/cf-xarray
#573Description
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 typeVersioning
python==3.13.3
xarray==2025.6.1
xvec==0.4.0
zarr==3.0.8
geopandas==1.1.0
geodatasets==2024.8.0
Metadata
Metadata
Assignees
Labels
No labels
Activity
jakenotjay commentedon Jun 19, 2025
Appears that downgrading to xarray-2025.3.1 fixes this - https://colab.research.google.com/drive/1SG-R6fN54gUQVx2FVvPKbNoviH_mB9Nx?usp=sharing
martinfleis commentedon Jun 19, 2025
@dcherian is it possible that this check in
cf_xarrayis failing now, returning the dataset as is? Because it seems thatencoded_cubein the snippet above still has the geometry.https://github.com/xarray-contrib/cf-xarray/blob/4be96ff2e40e7a7fc7ff6d57744c4a42b6cbaa94/cf_xarray/geometry.py#L298-L304
dcherian commentedon Jun 20, 2025
Just released cf-xarray v0.10.6