Skip to content

Commit 2b90358

Browse files
committed
coords.dims: only include dims found in variables
Exclude the dimension(s) present in the wrapped Dataset / DataArray / DataTree object that do not have any coordinate variable.
1 parent 40119bf commit 2b90358

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

xarray/core/coordinates.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from xarray.core.types import DataVars, Self, T_DataArray, T_Xarray
2525
from xarray.core.utils import (
2626
Frozen,
27+
FrozenMappingWarningOnValuesAccess,
2728
ReprObject,
2829
either_dict_or_kwargs,
2930
emit_user_level_warning,
@@ -741,8 +742,8 @@ def _names(self) -> set[Hashable]:
741742

742743
@property
743744
def dims(self) -> Frozen[Hashable, int]:
744-
# deliberately display all dims, not just those on coordinate variables - see https://github.com/pydata/xarray/issues/9466
745-
return self._data.dims
745+
dims = calculate_dimensions(self.variables)
746+
return FrozenMappingWarningOnValuesAccess(dims)
746747

747748
@property
748749
def dtypes(self) -> Frozen[Hashable, np.dtype]:
@@ -851,8 +852,8 @@ def _names(self) -> set[Hashable]:
851852

852853
@property
853854
def dims(self) -> Frozen[Hashable, int]:
854-
# deliberately display all dims, not just those on coordinate variables - see https://github.com/pydata/xarray/issues/9466
855-
return Frozen(self._data.dims)
855+
dims = calculate_dimensions(self.variables)
856+
return FrozenMappingWarningOnValuesAccess(dims)
856857

857858
@property
858859
def dtypes(self) -> Frozen[Hashable, np.dtype]:
@@ -942,7 +943,8 @@ def __init__(self, dataarray: T_DataArray) -> None:
942943

943944
@property
944945
def dims(self) -> tuple[Hashable, ...]:
945-
return self._data.dims
946+
dims = calculate_dimensions(self._data._coords)
947+
return tuple(dims)
946948

947949
@property
948950
def dtypes(self) -> Frozen[Hashable, np.dtype]:
@@ -967,7 +969,9 @@ def _update_coords(
967969
self, coords: dict[Hashable, Variable], indexes: dict[Hashable, Index]
968970
) -> None:
969971
validate_dataarray_coords(
970-
self._data.shape, Coordinates._construct_direct(coords, indexes), self.dims
972+
self._data.shape,
973+
Coordinates._construct_direct(coords, indexes),
974+
self._data.dims,
971975
)
972976

973977
self._data._coords = coords

0 commit comments

Comments
 (0)