Skip to content

Commit 5a45051

Browse files
Convert name to string in label_from_attrs (#6832)
* Convert name to string in label_from_attrs * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 2e02ca7 commit 5a45051

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

xarray/plot/utils.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ def _maybe_gca(**kwargs):
463463
return plt.axes(**kwargs)
464464

465465

466-
def _get_units_from_attrs(da):
466+
def _get_units_from_attrs(da) -> str:
467467
"""Extracts and formats the unit/units from a attributes."""
468468
pint_array_type = DuckArrayModule("pint").type
469469
units = " [{}]"
@@ -478,16 +478,16 @@ def _get_units_from_attrs(da):
478478
return units
479479

480480

481-
def label_from_attrs(da, extra=""):
481+
def label_from_attrs(da, extra: str = "") -> str:
482482
"""Makes informative labels if variable metadata (attrs) follows
483483
CF conventions."""
484-
484+
name: str = "{}"
485485
if da.attrs.get("long_name"):
486-
name = da.attrs["long_name"]
486+
name = name.format(da.attrs["long_name"])
487487
elif da.attrs.get("standard_name"):
488-
name = da.attrs["standard_name"]
488+
name = name.format(da.attrs["standard_name"])
489489
elif da.name is not None:
490-
name = da.name
490+
name = name.format(da.name)
491491
else:
492492
name = ""
493493

xarray/tests/test_plot.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ def test_label_from_attrs(self):
169169
da = self.darray.copy()
170170
assert "" == label_from_attrs(da)
171171

172+
da.name = 0
173+
assert "0" == label_from_attrs(da)
174+
172175
da.name = "a"
173176
da.attrs["units"] = "a_units"
174177
da.attrs["long_name"] = "a_long_name"

0 commit comments

Comments
 (0)