-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
.coarsen() method for the xarray.Dataset removes its attributes. #3376
Comments
Have you tried The behaviour for Datasets and DataArrays should be consistent in any case. Would you like to try and fix it? |
Hello, Yes, even with the option Inputsimport xarray as xr
import numpy as np
xr.set_options(keep_attrs=True)
import xarray as xr
import numpy as np
var1 = np.linspace(10, 15, 100)
var2 = np.linspace(5, 10, 100)
coords = np.linspace(1, 10, 100)
dat = xr.Dataset(
data_vars={'var1': ('coord', var1), 'var2': ('coord', var2)},
coords={'coord': coords}
)
dat.attrs['model_id'] = 'model1'
# coarsen dataset
dat = dat.coarsen(coord=5, keep_attrs=True).mean()
# print dataset
dat Actual Output<xarray.Dataset>
Dimensions: (coord: 20)
Coordinates:
* coord (coord) float64 1.182 1.636 2.091 2.545 ... 8.455 8.909 9.364 9.818
Data variables:
var1 (coord) float64 10.1 10.35 10.61 10.86 ... 14.14 14.39 14.65 14.9
var2 (coord) float64 5.101 5.354 5.606 5.859 ... 9.141 9.394 9.646 9.899 Expected Output<xarray.Dataset>
Dimensions: (coord: 20)
Coordinates:
* coord (coord) float64 1.182 1.636 2.091 2.545 ... 8.455 8.909 9.364 9.818
Data variables:
var1 (coord) float64 10.1 10.35 10.61 10.86 ... 14.14 14.39 14.65 14.9
var2 (coord) float64 5.101 5.354 5.606 5.859 ... 9.141 9.394 9.646 9.899
Attributes:
model_id: model1 I'm not too familiar with the xarray package internals. But I will look to see if I can do my part and do a pull request. I'll get back to you after having a look at the internals. |
Thanks for the clear report. I'll mark this as a bug then. |
@jejjohnson that means |
* Add test of DataWithCoords.coarsen() for #3376 * Add test of Variable.coarsen() for #3376 * Add keep_attrs kwarg to DataWithCoords.coarsen() for #3376 * Style and spelling fixes (#3376) * Fix test_coarsen_keep_attrs by removing self from input * Pass keep_attrs through to _coarsen_cls and _rolling_cls returns (#3376) * Move keyword from coarsen to mean in test_coarsen_keep_attrs * Start handling keep_attrs in rolling class constructors (#3376) * Update Coarsen constructor and DatasetCoarsen class method (GH3376) Assign keep_attrs keyword value to Coarsen objects in constructor Add conditional inside _reduce_method.wrapped_func branching on self.keep_attrs and pass back to returned Dataset * Incorporate code review from @max-sixty * Fix Dataset.coarsen and Variable.coarsen for GH3376 Handle global keep_attrs setting inside Variable._coarsen_reshape Pass attrs through consistently inside DatasetCoarsen._reduce_method Don't pass Variable.coarsen a keyword argument it doesn't expect inside DataArrayCoarsen._reduce_method * Update tests for GH3376 * Incorporate review changes to test_dataset for GH3376 Remove commented-out test from test_coarsen_keep_attrs Add test_rolling_keep_attrs * Change Rolling._dataset_implementation for GH3376 Return a Dataset object that results in test_rolling_keep_attrs Passing * style fixes * Remove duplicate variable assignment and document change (GH3776)
Hello,
I am not sure if this is a bug or a feature but when one calls the xarray.coarsen() on a dataset, then the attributes get removed.
Dataset Example
Actual Output
Expected Output
Problem Description
I believe the attributes should stay within the xarray.Dataset no matter what the operations that are done on it. Obviously maybe for some operations an entry like model_id could change because it's no longer the model. But I believe that should be left up to the user. Perhaps a warning in the docs might be sufficient. The behaviour isn't consistent with the xarray.coarsen() function on the xarray.DataArray example where the attributes remain the same (see details below).
DataArray Example
Expected/Actual Output
Output of
xr.show_versions()
xarray: 0.13.0
pandas: 0.25.1
numpy: 1.17.2
scipy: 1.3.1
netCDF4: 1.4.2
pydap: None
h5netcdf: None
h5py: 2.9.0
Nio: None
zarr: 2.3.2
cftime: 1.0.3.4
nc_time_axis: None
PseudoNetCDF: None
rasterio: 1.0.21
cfgrib: None
iris: None
bottleneck: None
dask: 2.4.0
distributed: None
matplotlib: 3.1.1
cartopy: 0.17.0
seaborn: None
numbagg: None
setuptools: 41.2.0
pip: 19.2.3
conda: None
pytest: None
IPython: 7.8.0
sphinx: None
The text was updated successfully, but these errors were encountered: