Repository navigation
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
Details
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
Activity
welcome commentedon Jan 7, 2025
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!
TomNicholas commentedon Jan 8, 2025
Thank you for this bug report! That definitely shouldn't be happening.
I suspect that it's going wrong in the
.variable1 = ...assignment step. Your results are consistent with the slicedDataArrayobject just being assigned as a new.variable1attribute, rather than actually properly engaging with the machinery that is supposed to recognize that the private dictionary mapping names to variables on that node is supposed to be updated.pepijnvtol commentedon Jan 8, 2025
I am not sure if this is the case, but could this indicate a possible memory error? With large datasets, it seems that the dataset is not overwritten, but both versions are stored. In situations where memory is constrained, this can be undesirable. However, I am not certain if this is truly an issue, as I have not encountered memory errors due to this. I will stick to the issue at hand, but this was just a thought.
pepijnvtol commentedon Jan 8, 2025
I just tried a similar operation with a
Dataset. In the example, it would look something like this:This results in an
AttributeError:I think the same behavior should apply to
Datatreeobjects. The suggested solution (usingds['name'] = ...) is also applicable to the reported bug.I don't have a full solution yet, but I thought it was interesting to check the
Datasetbehavior and compare it with theDatatreeto better understand what's happening.TomNicholas commentedon Jan 8, 2025
I don't think so - your example reproduces the error with a very small variable.
Yes! That's what supposed to happen here.
in https://docs.xarray.dev/en/stable/user-guide/data-structures.html#dataset-contents it says
That's the behaviour we also want for
DataTree.The bug is somewhere in the implementation of this class
https://github.com/pydata/xarray/blob/main/xarray/core/common.py#L370
@owenlittlejohns I remember we had some issues integrating the
AttrAccessMixintoDataTreewhich caused us to split it out intoTreeAttrAccessMixinlike this (in #9011). Do you have any idea what the issue might be here?keewis commentedon Jan 8, 2025
I'm not certain, but could this be caused by the presence of
__dict__onDataTree? ForDatasetthe presence of__slots__causes__dict__to be absent, which raises anAttributeErrorinAttrAccessMixin.__setitem__(which then gets modified to include more information).Edit: see #9068
TomNicholas commentedon Jan 8, 2025
Oh good catch @keewis ! So the question now is how we can get rid of
__dict__onDataTree?keewis commentedon Jan 8, 2025
Change the mixin from
TreeAttrAccessMixintoAttrAccessMixinand see what breaks? For that you'd have to go through all the base classes ofDataTreeto see whether there's any that absolutely need to have dynamic attributes.owenlittlejohns commentedon Jan 8, 2025
Looks like @keewis beat me to mentioning this issue: #9068.
Just swapping out the
TreeAttrAccessMixinforAttrAccessMixincaused problems in the instantiation due to__slots__and__dict__both being present. I think your suggestion of removing__dict__onDataTreeis the way to go. If I remember rightly, I think the issues might have also been a bit lower down in either theTreeNodeorNamedNodeclasses.[-]Datatree store dataset[/-][+]DataTree attribute-like access bug[/+]DresdenGman commentedon Nov 21, 2026
Hi, I reproduced this on xarray 2026.9.0: after
dt.variable1 = dt.variable1.sel(x=slice(1, 5)),dt.variable1has 5 elements whiledt["variable1"]still has 10 and the stored tree is unchanged. The sliced value lands in the instance__dict__instead of the dataset, matching @keewis's January 2025 diagnosis. Parent refactoring issue #9068 (consolidate ontoAttrAccessMixin) is still open with no comments.Before working on this, should the fix go through #9068's consolidation (removing
__dict__fromDataTree/TreeNode/NamedNode) or a narrower fix routing attribute assignment into the stored dataset? @owenlittlejohns noted in #9011 that the swap caused instantiation problems, so I'd rather align on the approach first.