Description
What happened?
Encoding in data arrays are lost after applying any ufunc. There should be an argument similar to keep_attrs
in apply_ufunc
for encoding.
What did you expect to happen?
The encoding property should be inherited (or resolved if more than one data arrays are involved) when applying a ufunc.
Minimal Complete Verifiable Example
import xarray as xr
import numpy as np
my_ufunc = lambda x: x + 1
xarr1 = xr.DataArray(np.array([1,2,3]))
xarr1.encoding = {'dummy': 'baz'}
xarr2 = xr.apply_ufunc(my_ufunc, xarr1)
print(xarr1.encoding, xarr2.encoding)
# Workaround:
xarr2.encoding = xarr1.encoding.copy()
print(xarr1.encoding, xarr2.encoding)
MVCE confirmation
- Minimal example — the example is as focused as reasonably possible to demonstrate the underlying issue in xarray.
- Complete example — the example is self-contained, including all data and the text of any traceback.
- Verifiable example — the example copy & pastes into an IPython prompt or Binder notebook, returning the result.
- New issue — a search of GitHub Issues suggests this is not a duplicate.
- Recent environment — the issue occurs with the latest version of xarray and its dependencies.
Relevant log output
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.13.3 | packaged by conda-forge | (main, Apr 14 2025, 20:31:24) [MSC v.1943 64 bit (AMD64)]
python-bits: 64
OS: Windows
OS-release: 11
machine: AMD64
processor: Intel64 Family 6 Model 170 Stepping 4, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: ('English_Australia', '1252')
libhdf5: 1.14.6
libnetcdf: 4.9.2
xarray: 0.1.dev5937+g070af11
pandas: 2.2.3
numpy: 2.2.5
scipy: 1.15.2
netCDF4: 1.7.2
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: 1.6.4
nc_time_axis: None
iris: None
bottleneck: None
dask: None
distributed: None
matplotlib: None
cartopy: None
seaborn: None
numbagg: None
fsspec: None
cupy: None
pint: None
sparse: None
flox: None
numpy_groupies: None
setuptools: None
pip: 25.1.1
conda: None
pytest: None
mypy: None
IPython: 9.2.0
sphinx: None