pydata / xarray

N-D labeled arrays and datasets in Python
https://xarray.dev
Apache License 2.0
3.62k stars 1.08k forks source link

Missing coordinate attributes after weighted mean #9317

Open JdeJong96 opened 3 months ago

JdeJong96 commented 3 months ago

What is your issue?

After performing a weighted mean with keep_attrs=True along a specific dimension in a Dataset, no remaining coordinates have attributes left:

import numpy as np
import xarray as xr

air = xr.tutorial.open_dataset("air_temperature")
air['gw'] = ('lat', np.cos(np.deg2rad(air.lat.data)), 
             {'long_name':'grid weights'})
air_wm = air.weighted(air.gw).mean('lat', keep_attrs=True)
print('before weighted mean\n',[(c,air[c].attrs) for c in air.coords])
print('\nafter\n',[(c,air_wm[c].attrs) for c in air_wm.coords])

returns

before weighted mean [('lat', {'standard_name': 'latitude', 'long_name': 'Latitude', 'units': 'degrees_north', 'axis': 'Y'}), ('lon', {'standard_name': 'longitude', 'long_name': 'Longitude', 'units': 'degrees_east', 'axis': 'X'}), ('time', {'standard_name': 'time', 'long_name': 'Time'})]

after [('lon', {}), ('time', {})]

Likewise for a DataArray:

temp = air.air
temp_wm = temp.weighted(air.gw).mean('lat', keep_attrs=True)
print('before weighted mean\n',[(c,temp[c].attrs) for c in temp.coords])
print('\nafter\n',[(c,temp_wm[c].attrs) for c in temp_wm.coords])

returns

before weighted mean [('lat', {'standard_name': 'latitude', 'long_name': 'Latitude', 'units': 'degrees_north', 'axis': 'Y'}), ('lon', {'standard_name': 'longitude', 'long_name': 'Longitude', 'units': 'degrees_east', 'axis': 'X'}), ('time', {'standard_name': 'time', 'long_name': 'Time'})]

after [('lon', {}), ('time', {})]

I think this is the first issue regarding this problem. Have not tested any other weighted operations yet.

Output of xr.show_versions():

INSTALLED VERSIONS

commit: None python: 3.11.9 (main, Apr 19 2024, 11:44:45) [Clang 14.0.6 ] python-bits: 64 OS: Darwin OS-release: 23.5.0 machine: x86_64 processor: i386 byteorder: little LC_ALL: None LANG: nl_NL.UTF-8 LOCALE: ('nl_NL', 'UTF-8') libhdf5: 1.12.1 libnetcdf: 4.8.1

xarray: 2024.7.0 pandas: 2.1.1 numpy: 1.25.2 scipy: 1.11.3 netCDF4: 1.6.2 pydap: None h5netcdf: 1.2.0 h5py: 3.9.0 zarr: None cftime: 1.6.2 nc_time_axis: 1.4.1 iris: 3.9.0 bottleneck: 1.3.5 dask: 2024.5.0 distributed: 2024.5.0 matplotlib: 3.8.4 cartopy: 0.22.0 seaborn: None numbagg: None fsspec: 2023.9.2 cupy: None pint: None sparse: None flox: None numpy_groupies: None setuptools: 72.1.0 pip: 24.0 conda: 24.7.1 pytest: None mypy: None IPython: 8.15.0 sphinx: None /Users/jasperdejong/opt/anaconda3/envs/py311/lib/python3.11/site-packages/_distutils_hack/init.py:32: UserWarning: Setuptools is replacing distutils. Support for replacing an already imported distutils is deprecated. In the future, this condition will fail. Register concerns at https://github.com/pypa/setuptools/issues/new?template=distutils-deprecation.yml warnings.warn(

welcome[bot] commented 3 months ago

Thanks for opening your first issue here at xarray! Be sure to follow the issue template! If you have an idea for a solution, we would really welcome a Pull Request with proposed changes. See the Contributing Guide for more. It may take us a while to respond here, but we really value your contribution. Contributors like you help make xarray better. Thank you!