MCPcopy Index your code
hub / github.com/pydata/xarray / validate_dataarray_coords

Function validate_dataarray_coords

xarray/core/coordinates.py:1306–1350  ·  view source on GitHub ↗

Validate coordinates ``coords`` to include in a DataArray defined by ``shape`` and dimensions ``dim``. If a coordinate is associated with an index, the validation is performed by the index. By default the coordinate dimensions must match (a subset of) the array dimensions (in any or

(
    shape: tuple[int, ...],
    coords: Coordinates | Mapping[Hashable, Variable],
    dim: tuple[Hashable, ...],
)

Source from the content-addressed store, hash-verified

1304
1305
1306def validate_dataarray_coords(
1307 shape: tuple[int, ...],
1308 coords: Coordinates | Mapping[Hashable, Variable],
1309 dim: tuple[Hashable, ...],
1310):
1311 """Validate coordinates ``coords`` to include in a DataArray defined by
1312 ``shape`` and dimensions ``dim``.
1313
1314 If a coordinate is associated with an index, the validation is performed by
1315 the index. By default the coordinate dimensions must match (a subset of) the
1316 array dimensions (in any order) to conform to the DataArray model. The index
1317 may override this behavior with other validation rules, though.
1318
1319 Non-index coordinates must all conform to the DataArray model. Scalar
1320 coordinates are always valid.
1321 """
1322 sizes = dict(zip(dim, shape, strict=True))
1323 dim_set = set(dim)
1324
1325 indexes: Mapping[Hashable, Index]
1326 if isinstance(coords, Coordinates):
1327 indexes = coords.xindexes
1328 else:
1329 indexes = {}
1330
1331 for k, v in coords.items():
1332 if k in indexes:
1333 invalid = not indexes[k].should_add_coord_to_array(k, v, dim_set)
1334 else:
1335 invalid = any(d not in dim for d in v.dims)
1336
1337 if invalid:
1338 raise CoordinateValidationError(
1339 f"coordinate {k} has dimensions {v.dims}, but these "
1340 "are not a subset of the DataArray "
1341 f"dimensions {dim}"
1342 )
1343
1344 for d, s in v.sizes.items():
1345 if d in sizes and s != sizes[d]:
1346 raise CoordinateValidationError(
1347 f"conflicting sizes for dimension {d!r}: "
1348 f"length {sizes[d]} on the data but length {s} on "
1349 f"coordinate {k!r}"
1350 )
1351
1352
1353def coordinates_from_variable(variable: Variable) -> Coordinates:

Callers 2

_infer_coords_and_dimsFunction · 0.90
_update_coordsMethod · 0.85

Calls 3

itemsMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…