Skip to content

Commit

Permalink
#19 Merge pull request from astropenguin/astropenguin/issue14
Browse files Browse the repository at this point in the history
Add option to format units of coordinates
  • Loading branch information
astropenguin authored Feb 3, 2024
2 parents dfb5df9 + 46cc556 commit 28b96db
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ sphinx = "^7.2"

[tool.pyright]
typeCheckingMode = "strict"
reportIncompatibleMethodOverride = "warning"
reportMissingTypeStubs = "warning"
reportUnnecessaryIsInstance = "warning"

Expand Down
16 changes: 15 additions & 1 deletion xarray_units/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ def format(
da: TDataArray,
format: str,
/,
coords: bool = True,
**kwargs: Any,
) -> TDataArray:
"""Format units of a DataArray.
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:
Expand All @@ -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(
Expand Down

0 comments on commit 28b96db

Please sign in to comment.