Open klindsay28 opened 4 years ago
I don't know too much about that area, but I believe that's because directly after reading a NetCDF file, you don't work with numpy
arrays but with something different. Only once you call some form of np.array
(such as np.asarray
or np.asanyarray
) on it, it becomes a numpy.ndarray
.
I'm not sure if the behaviour you encountered is a bug (it probably is), but to fix your example for now, I think you should explicitly load()
the dataset after opening it:
with xr.open_dataset('ds.nc').load() as ds:
ds2 = ds.copy()
print(f'ds.equals = {ds.equals(ds2)}')
ds2[varname][0] = 11.0
print(f'ds.equals = {ds.equals(ds2)}')
MCVE Code Sample
Expected Output
I expect the code to write out
However, when I run it, the last line is
Problem Description
The code above writes a small
xr.Dataset
to a netCDF file. There are 2 context managers opening the netCDF file asds
. Both context manager blocks start by settingds2
to a shallow copy ofds
.In the first context manager block, a value in
ds2
is modified, andds2
is compared tods
. The Datasets are still equal, confirming that the copy is shallow.The second context manager block is the same as the first, except that
ds2
is compared tods
prior changing the value the value inds2
. When this is done, the Datasets are no longer equal, indicating thatds2
is no longer a shallow copy ofds
.I don't understand how evaluating
ds.equals(ds2)
, prior to changing a value inds2
, could decoupleds2
fromds
.I only observe this behavior when
ds
is set viaxr.open_dataset
. I don't see it when I createds
directly usingxr.Dataset
.I'm rather perplexed by this.
Output of
xr.show_versions()