home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 1046614317

This data as json

id node_id number title user state locked assignee milestone comments created_at updated_at closed_at author_association active_lock_reason draft pull_request body reactions performed_via_github_app state_reason repo type
1046614317 PR_kwDOAMm_X84uL503 5948 Fix plot.line crash for data of shape (1, N) in _title_for_slice on format_item 9513634 closed 0     2 2021-11-06T22:56:59Z 2021-11-08T18:40:08Z 2021-11-08T17:27:45Z CONTRIBUTOR   0 pydata/xarray/pulls/5948

Affected xarray versions 0.20.0+

When dependabot recently made a PR to update xarray to 0.20.0 our integration test CI failed with .... File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/xarray/plot/plot.py", line 447, in line ax.set_title(darray._title_for_slice()) File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/xarray/core/dataarray.py", line 3140, in _title_for_slice v=format_item(coord.values), File "/opt/hostedtoolcache/Python/3.8.12/x64/lib/python3.8/site-packages/xarray/core/formatting.py", line 146, in format_item return f"{x:.4}" TypeError: unsupported format string passed to numpy.ndarray.__format__

We use xarray.Dataarray.plot.line(x="<dim_name>") to plot data of shape (N, M) where N and M are >=1.

The crash is caused because the following line

https://github.com/pydata/xarray/blob/402a9b398868bd9bd385210138d6f8ea610c8c40/xarray/core/dataarray.py#L3136

allows arrays of shape () and (1,) to be passed into format_item, where both pass this check

https://github.com/pydata/xarray/blob/402a9b398868bd9bd385210138d6f8ea610c8c40/xarray/core/formatting.py#L145-L146

But f"{x:.4}" only works for arrays of shape () and crashes on arrays of shape (1,)

Before the check was https://github.com/pydata/xarray/blob/a78c1e0115d38cb4461fd1aba93334d440cff49c/xarray/core/formatting.py#L145-L146

Which didn't allow any arrays and caused the float formatting issue.

Code showing the differences ```python In [1]: import numpy as np In [2]: x = np.array(0.1) In [3]: x.shape Out[3]: () In [4]: np.issubdtype(type(x), np.floating) Out[4]: False In [5]: hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating) Out[5]: True In [6]: f"{x:.4}" Out[6]: '0.1' In [7]: f"{x.item():.4}" Out[7]: '0.1' In [8]: x = np.array([0.1]) In [9]: x.shape Out[9]: (1,) In [10]: np.issubdtype(type(x), np.floating) Out[10]: False In [11]: hasattr(x, "dtype") and np.issubdtype(x.dtype, np.floating) Out[11]: True In [12]: f"{x:.4}" --------------------------------------------------------------------------- TypeError Traceback (most recent call last) ~\AppData\Local\Temp/ipykernel_32644/2490801713.py in <module> ----> 1 f"{x:.4}" TypeError: unsupported format string passed to numpy.ndarray.__format__ In [13]: f"{x.item():.4}" Out[13]: '0.1' ```

I hope TestPlot1D was the correct place to put the test since the plot itself is 1D but the data which are plotted have a second dimension with size 1 (naming thing and where to put things... 😅).

  • [x] Tests added
  • [x] Passes pre-commit run --all-files
  • [x] User visible changes (including notable bug fixes) are documented in whats-new.rst

PS: Thanks for the awesome package ❤️

{
    "url": "https://api.github.com/repos/pydata/xarray/issues/5948/reactions",
    "total_count": 1,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 1,
    "rocket": 0,
    "eyes": 0
}
    13221727 pull

Links from other tables

  • 1 row from issues_id in issues_labels
  • 2 rows from issue in issue_comments
Powered by Datasette · Queries took 79.764ms · About: xarray-datasette