Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing coordinate attributes after weighted mean #9317

Open
JdeJong96 opened this issue Aug 6, 2024 · 1 comment · May be fixed by #10083
Open

Missing coordinate attributes after weighted mean #9317

JdeJong96 opened this issue Aug 6, 2024 · 1 comment · May be fixed by #10083
Labels
needs triage Issue that has not been reviewed by xarray team member

Comments

@JdeJong96
Copy link

JdeJong96 commented Aug 6, 2024

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(

@JdeJong96 JdeJong96 added the needs triage Issue that has not been reviewed by xarray team member label Aug 6, 2024
Copy link

welcome bot commented Aug 6, 2024

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!

@JdeJong96 JdeJong96 changed the title Missing coordinate dimensions after weighted mean Missing coordinate attributes after weighted mean Aug 6, 2024
@JdeJong96 JdeJong96 linked a pull request Feb 27, 2025 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue that has not been reviewed by xarray team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant