diff --git a/pyproject.toml b/pyproject.toml index 9eab908..e7b6ed0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,7 +26,6 @@ sphinx = "^7.2" [tool.pyright] typeCheckingMode = "strict" -reportIncompatibleMethodOverride = "warning" reportMissingTypeStubs = "warning" reportUnnecessaryIsInstance = "warning" diff --git a/xarray_units/quantity.py b/xarray_units/quantity.py index 698304d..6013989 100644 --- a/xarray_units/quantity.py +++ b/xarray_units/quantity.py @@ -117,6 +117,7 @@ def format( da: TDataArray, format: str, /, + coords: bool = True, **kwargs: Any, ) -> TDataArray: """Format units of a DataArray. @@ -124,6 +125,7 @@ def format( Args: da: Input DataArray with units. format: Format of units (e.g. ``"console"``, ``"latex"``). + coords: Whether to also format the units of the coordinates. **kwargs: Keyword arguments of the formatting. Returns: @@ -139,7 +141,19 @@ def format( """ units = unitsof(da, format=format, strict=True, **kwargs) - return set(da, units, overwrite=True) + da = set(da, units, overwrite=True) + + if not coords: + return da + + for name, coord in da.coords.items(): # type: ignore + if (units := unitsof(coord, format=format, **kwargs)) is None: + continue + + coord = set(coord, units, overwrite=True) # type: ignore + da = da.assign_coords({name: coord}) # type: ignore + + return da def like(