Description
What happened?
I have a datatree with a dataset in a group. I tried to filter this dataset and store in the datatree. The dataset showed the filtered dataset and the unfiltered dataset, depending on the method to get the group. dt.variable1
or dt['variable1']
.
What did you expect to happen?
I expect that both datasets are equal.
Minimal Complete Verifiable Example
Note the difference when `dt.variable1` and `dt['variable1']` are printed. When the datatree is exported the value of `dt['variable1']` is used.
import numpy as np
import xarray as xr
ds1 = xr.Dataset(
dict(
variable1=xr.DataArray(
np.random.randn(10),
dims="x",
coords=dict(x=np.linspace(1, 10, num=10)),
)
)
)
dt = xr.DataTree(ds1)
print("Datatree with 1 dataset (variable1)")
print(dt)
# filter on x
dt.variable1 = dt.variable1.sel(x=slice(1, 5))
print("\nPrint sliced dataset variable 1 using 'dt.variable1'")
print(dt.variable1)
print("\nPrint sliced dataset variable 1 using 'dt['variable1']'")
print(dt["variable1"])
print(
"\nThe whole datatree how it is stored (see variable1, which is not sliced)"
)
print(dt)
Datatree with 1 dataset (variable1)
<xarray.DataTree>
Group: /
Dimensions: (x: 10)
Coordinates:
* x (x) float64 80B 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
Data variables:
variable1 (x) float64 80B -0.03606 -0.5738 -1.331 ... -0.3654 1.669 0.9354
Print sliced dataset variable 1 using 'dt.variable1'
<xarray.DataArray 'variable1' (x: 5)> Size: 40B
array([-0.03605657, -0.57384656, -1.3312692 , 1.0832746 , -0.07725834])
Coordinates:
* x (x) float64 40B 1.0 2.0 3.0 4.0 5.0
Print sliced dataset variable 1 using 'dt['variable1']'
<xarray.DataArray 'variable1' (x: 10)> Size: 80B
array([-0.03605657, -0.57384656, -1.3312692 , 1.0832746 , -0.07725834,
-0.45822326, -1.32865998, -0.36541352, 1.66870598, 0.93536491])
Coordinates:
* x (x) float64 80B 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
The whole datatree how it is stored (see variable1, which is not sliced)
<xarray.DataTree>
Group: /
Dimensions: (x: 10)
Coordinates:
* x (x) float64 80B 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0
Data variables:
variable1 (x) float64 80B -0.03606 -0.5738 -1.331 ... -0.3654 1.669 0.9354
>>> import xarray
>>> xarray.__version__
'2025.1.0'
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
No response
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None
python: 3.13.1 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 17:02:46) [MSC v.1929 64 bit (AMD64)]
python-bits: 64
OS: Windows
OS-release: 11
machine: AMD64
processor: Intel64 Family 6 Model 186 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8
LOCALE: ('English_United States', '1252')
libhdf5: None
libnetcdf: None
xarray: 2025.1.0
pandas: 2.2.3
numpy: 2.2.1
scipy: None
netCDF4: None
pydap: None
h5netcdf: None
h5py: None
zarr: None
cftime: None
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: 75.1.0
pip: 24.2
conda: None
pytest: None
mypy: None
IPython: None
sphinx: None