Skip to content

Commit

Permalink
Fix plot.line crash for data of shape (1, N) in _title_for_slice on f…
Browse files Browse the repository at this point in the history
…ormat_item (pydata#5948)

* 🧪 Add failing test case show casing the error

* 🩹 Fix format_item crash when value array of shape (1, )

* 📚 Added change to whats-new.rst
  • Loading branch information
s-weigand authored and snowman2 committed Feb 9, 2022
1 parent fac2c24 commit 1f7b3be
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
3 changes: 2 additions & 1 deletion doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ Deprecations

Bug fixes
~~~~~~~~~

- Fix plot.line crash for data of shape ``(1, N)`` in _title_for_slice on format_item (:pull:`5948`).
By `Sebastian Weigand <https://github.com/s-weigand>`_.

Documentation
~~~~~~~~~~~~~
Expand Down
2 changes: 1 addition & 1 deletion xarray/core/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def format_item(x, timedelta_format=None, quote_strings=True):
elif isinstance(x, (str, bytes)):
return repr(x) if quote_strings else x
elif hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating):
return f"{x:.4}"
return f"{x.item():.4}"
else:
return str(x)

Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,6 +754,13 @@ def test_slice_in_title(self):
title = plt.gca().get_title()
assert "d = 10.01" == title

def test_slice_in_title_single_item_array(self):
"""Edge case for data of shape (1, N) or (N, 1)."""
darray = self.darray.expand_dims({"d": np.array([10.009])})
darray.plot.line(x="period")
title = plt.gca().get_title()
assert "d = 10.01" == title


class TestPlotStep(PlotTestCase):
@pytest.fixture(autouse=True)
Expand Down

0 comments on commit 1f7b3be

Please sign in to comment.