home / github / issues

Menu
  • Search all tables
  • GraphQL API

issues: 1617939324

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
1617939324 I_kwDOAMm_X85gb8t8 7602 Inconsistent coordinate attributes handling in apply_ufunc 20629530 open 0     0 2023-03-09T20:19:25Z 2023-03-15T17:04:35Z   CONTRIBUTOR      

What happened?

When calling apply_ufunc with keep_attrs=False, the coordinate attributes are dropped only if there is more than one argument to the call.

What did you expect to happen?

I expected the behaviour to be the same, no matter the number of arguments.

I also expected the coordinate attributes to be preserved if that coordinate was appearing on only one argument.

Minimal Complete Verifiable Example

```Python import xarray as xr

def wrapper(ar1, ar2=None): return ar1.mean(axis=-1)

ds = xr.tutorial.open_dataset("air_temperature")

o1 = xr.apply_ufunc( wrapper, ds.air, ds.time, input_core_dims=[['time'], ['time']], keep_attrs=False ) print(o1.lat.attrs) # {}

o2 = xr.apply_ufunc( wrapper, ds.air, input_core_dims=[['time']], keep_attrs=False ) print(o2.lat.attrs) # {'standard_name': ... } ```

MVCE confirmation

  • [X] Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
  • [X] Complete example — the example is self-contained, including all data and the text of any traceback.
  • [X] Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
  • [X] New issue — a search of GitHub Issues suggests this is not a duplicate.

Relevant log output

No response

Anything else we need to know?

The behaviour stems from this if/else:

https://github.com/pydata/xarray/blob/6d771fc82228bdaf8a4b77d0ceec1cc444ebd090/xarray/core/computation.py#L252-L260

The upper part (1 arg) doesn't touch the attributes, but in the else (more than 1 arg) , two levels deeper in merge_coordinates_without_align , we have:

https://github.com/pydata/xarray/blob/6d771fc82228bdaf8a4b77d0ceec1cc444ebd090/xarray/core/merge.py#L283-L286

When apply_ufunc is called with keep_attrs=False, the combine_attrs above is "drop". In merge_attrs even though there is only one attribute dict passed for lat, it returns an empty dict.

My preference would be for keep_attrs to only refer to the data attributes and that coordinate attributes would be preserved, or even merged if needed. This was my expectation here, as this is the behaviour in many other places of xarray. For example : python3 with xr.set_options(keep_attrs=False): o = ds.air.mean('time') This drops attributes of air, but preserves those of lat and lon.

I see no easy way out here, except by handling it explicitly somewhere in apply_ufunc ? If the decision is that "untouched" coordinate attribute preservation is not ensured by xarray, I think it would be worth noting somewhere (but I don't know where). And I would change my codes to "manually" preserve those where appropriate.

Environment

INSTALLED VERSIONS ------------------ commit: 6d771fc82228bdaf8a4b77d0ceec1cc444ebd090 python: 3.10.9 | packaged by conda-forge | (main, Feb 2 2023, 20:20:04) [GCC 11.3.0] python-bits: 64 OS: Linux OS-release: 6.1.11-100.fc36.x86_64 machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: fr_CA.UTF-8 LOCALE: ('fr_CA', 'UTF-8') libhdf5: 1.12.2 libnetcdf: 4.9.1 xarray: 2023.2.0 pandas: 1.5.3 numpy: 1.23.5 scipy: 1.10.1 netCDF4: 1.6.3 pydap: installed h5netcdf: 1.1.0 h5py: 3.8.0 Nio: None zarr: 2.13.6 cftime: 1.6.2 nc_time_axis: 1.4.1 PseudoNetCDF: 3.2.2 rasterio: 1.3.6 cfgrib: 0.9.10.3 iris: 3.4.1 bottleneck: 1.3.7 dask: 2023.3.0 distributed: 2023.3.0 matplotlib: 3.7.1 cartopy: 0.21.1 seaborn: 0.12.2 numbagg: 0.2.2 fsspec: 2023.3.0 cupy: None pint: 0.20.1 sparse: 0.14.0 flox: 0.6.8 numpy_groupies: 0.9.20 setuptools: 67.6.0 pip: 23.0.1 conda: None pytest: 7.2.2 mypy: None IPython: 8.11.0 sphinx: None
{
    "url": "https://api.github.com/repos/pydata/xarray/issues/7602/reactions",
    "total_count": 0,
    "+1": 0,
    "-1": 0,
    "laugh": 0,
    "hooray": 0,
    "confused": 0,
    "heart": 0,
    "rocket": 0,
    "eyes": 0
}
    13221727 issue

Links from other tables

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