Skip to content

Commit d60f273

Browse files
authored
feat: enhance DataWrapper to support dims and coords properties for custom data types (#228)
1 parent 1237487 commit d60f273

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/ndv/models/_data_wrapper.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,17 @@ def supports(cls, obj: Any) -> TypeGuard[Any]:
129129
@property
130130
def dims(self) -> tuple[Hashable, ...]:
131131
"""Return the dimension labels for the data."""
132+
if hasattr(self._data, "dims"):
133+
return tuple(self._data.dims)
132134
# type ignore is asserted in the __init__ method
133135
return tuple(range(len(self._data.shape))) # type: ignore [attr-defined]
134136

135137
@property
136138
def coords(self) -> Mapping[Hashable, Sequence]:
137139
"""Return the coordinates for the data."""
140+
if hasattr(self._data, "coords") and isinstance(self._data.coords, Mapping):
141+
return self._data.coords
142+
138143
dims = self.dims
139144
# type ignore is asserted in the __init__ method
140145
return {i: range(s) for i, s in zip(dims, self._data.shape)} # type: ignore [attr-defined]
@@ -399,16 +404,6 @@ def _asarray(self, data: cl_array.Array) -> np.ndarray:
399404
class XarrayWrapper(DataWrapper["xr.DataArray"]):
400405
"""Wrapper for xarray DataArray objects."""
401406

402-
@property
403-
def dims(self) -> tuple[Hashable, ...]:
404-
"""Return the dimension labels for the data."""
405-
return tuple(self._data.dims)
406-
407-
@property
408-
def coords(self) -> Mapping[Hashable, Sequence]:
409-
"""Return the coordinates for the data."""
410-
return self.data.coords # type: ignore [no-any-return]
411-
412407
@classmethod
413408
def supports(cls, obj: Any) -> TypeGuard[xr.DataArray]:
414409
if (xr := sys.modules.get("xarray")) and isinstance(obj, xr.DataArray):

0 commit comments

Comments
 (0)