-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
xr.DataArray.where(drop=True) crashes if the result is False everywhere (h5netcdf engine) #4524
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
Comments
This also fails for ds = xr.tutorial.open_dataset('ROMS_example', engine='netcdf4')
da = ds["zeta"].isel(eta_rho=0, xi_rho=0)
print(da.where(da>1000, drop=True)) with a different error: Traceback ValueError Traceback (most recent call last)
<ipython-input-14-9df3a1c8f9e1> in <module>
----> 1 da.where(da>1000, drop=True)
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/common.py in where(self, cond, other, drop)
1266 cond = cond.isel(**indexers)
1267
-> 1268 return ops.where_method(self, cond, other)
1269
1270 def close(self: Any) -> None:
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/ops.py in where_method(self, cond, other)
191 # alignment for three arguments is complicated, so don't support it yet
192 join = "inner" if other is dtypes.NA else "exact"
--> 193 return apply_ufunc(
194 duck_array_ops.where_method,
195 self,
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/computation.py in apply_ufunc(func, input_core_dims, output_core_dims, exclude_dims, vectorize, join, dataset_join, dataset_fill_value, keep_attrs, kwargs, dask, output_dtypes, output_sizes, meta, dask_gufunc_kwargs, *args)
1102 # feed DataArray apply_variable_ufunc through apply_dataarray_vfunc
1103 elif any(isinstance(a, DataArray) for a in args):
-> 1104 return apply_dataarray_vfunc(
1105 variables_vfunc,
1106 *args,
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/computation.py in apply_dataarray_vfunc(func, signature, join, exclude_dims, keep_attrs, *args)
257 else:
258 name = result_name(args)
--> 259 result_coords = build_output_coords(args, signature, exclude_dims)
260
261 data_vars = [getattr(a, "variable", a) for a in args]
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/computation.py in build_output_coords(args, signature, exclude_dims)
222 else:
223 # TODO: save these merged indexes, instead of re-computing them later
--> 224 merged_vars, unused_indexes = merge_coordinates_without_align(
225 coords_list, exclude_dims=exclude_dims
226 )
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/merge.py in merge_coordinates_without_align(objects, prioritized, exclude_dims)
327 filtered = collected
328
--> 329 return merge_collected(filtered, prioritized)
330
331
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/merge.py in merge_collected(grouped, prioritized, compat)
227 variables = [variable for variable, _ in elements_list]
228 try:
--> 229 merged_vars[name] = unique_variable(name, variables, compat)
230 except MergeError:
231 if compat != "minimal":
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/merge.py in unique_variable(name, variables, compat, equals)
118 if compat == "broadcast_equals":
119 dim_lengths = broadcast_dimension_size(variables)
--> 120 out = out.set_dims(dim_lengths)
121
122 if compat == "no_conflicts":
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/variable.py in set_dims(self, dims, shape)
1438 # don't use broadcast_to unless necessary so the result remains
1439 # writeable if possible
-> 1440 expanded_data = self.data
1441 elif shape is not None:
1442 dims_map = dict(zip(dims, shape))
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/variable.py in data(self)
357 return self._data
358 else:
--> 359 return self.values
360
361 @data.setter
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/variable.py in values(self)
508 def values(self):
509 """The variable's data as a numpy.ndarray"""
--> 510 return _as_array_or_item(self._data)
511
512 @values.setter
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/variable.py in _as_array_or_item(data)
270 data = data.get()
271 else:
--> 272 data = np.asarray(data)
273 if data.ndim == 0:
274 if data.dtype.kind == "M":
~/conda/envs/xarray_dev/lib/python3.8/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
81
82 """
---> 83 return array(a, dtype, copy=False, order=order)
84
85
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/indexing.py in __array__(self, dtype)
683
684 def __array__(self, dtype=None):
--> 685 self._ensure_cached()
686 return np.asarray(self.array, dtype=dtype)
687
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/indexing.py in _ensure_cached(self)
680 def _ensure_cached(self):
681 if not isinstance(self.array, NumpyIndexingAdapter):
--> 682 self.array = NumpyIndexingAdapter(np.asarray(self.array))
683
684 def __array__(self, dtype=None):
~/conda/envs/xarray_dev/lib/python3.8/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
81
82 """
---> 83 return array(a, dtype, copy=False, order=order)
84
85
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/indexing.py in __array__(self, dtype)
653
654 def __array__(self, dtype=None):
--> 655 return np.asarray(self.array, dtype=dtype)
656
657 def __getitem__(self, key):
~/conda/envs/xarray_dev/lib/python3.8/site-packages/numpy/core/_asarray.py in asarray(a, dtype, order)
81
82 """
---> 83 return array(a, dtype, copy=False, order=order)
84
85
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/indexing.py in __array__(self, dtype)
558 def __array__(self, dtype=None):
559 array = as_indexable(self.array)
--> 560 return np.asarray(array[self.key], dtype=None)
561
562 def transpose(self, order):
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/backends/netCDF4_.py in __getitem__(self, key)
70
71 def __getitem__(self, key):
---> 72 return indexing.explicit_indexing_adapter(
73 key, self.shape, indexing.IndexingSupport.OUTER, self._getitem
74 )
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/core/indexing.py in explicit_indexing_adapter(key, shape, indexing_support, raw_indexing_method)
843 """
844 raw_key, numpy_indices = decompose_indexer(key, shape, indexing_support)
--> 845 result = raw_indexing_method(raw_key.tuple)
846 if numpy_indices.tuple:
847 # index the loaded np.ndarray
~/conda/envs/xarray_dev/lib/python3.8/site-packages/xarray/backends/netCDF4_.py in _getitem(self, key)
83 with self.datastore.lock:
84 original_array = self.get_array(needs_lock=False)
---> 85 array = getitem(original_array, key)
86 except IndexError:
87 # Catch IndexError in netCDF4 and return a more informative
netCDF4/_netCDF4.pyx in netCDF4._netCDF4.Variable.__getitem__()
~/conda/envs/xarray_dev/lib/python3.8/site-packages/netCDF4/utils.py in _StartCountStride(elem, shape, dimensions, grp, datashape, put, use_get_vars)
435 # ITERABLE #
436 elif np.iterable(e) and np.array(e).dtype.kind in 'i': # Sequence of integers
--> 437 start[...,i] = np.apply_along_axis(lambda x: e*x, i, np.ones(sdim[:-1]))
438 indices[...,i] = np.apply_along_axis(lambda x: np.arange(sdim[i])*x, i, np.ones(sdim[:-1], int))
439
<__array_function__ internals> in apply_along_axis(*args, **kwargs)
~/conda/envs/xarray_dev/lib/python3.8/site-packages/numpy/lib/shape_base.py in apply_along_axis(func1d, axis, arr, *args, **kwargs)
374 ind0 = next(inds)
375 except StopIteration as e:
--> 376 raise ValueError(
377 'Cannot apply_along_axis when any iteration dimensions are 0'
378 ) from None
ValueError: Cannot apply_along_axis when any iteration dimensions are 0 It does not fail when you load the data beforehand: import xarray as xr
ds = xr.tutorial.open_dataset('ROMS_example', engine='h5netcdf')
da = ds["zeta"]
da.load()
print(da.where(da>1000, drop=True)) |
@alexamici could this be a similar issue as #4737? (we decided against #4453) |
@mathause I think you are right, #4733 (the issue that #4737 aimed to fix) is probably not related to cfgrib, I didn't notice that no backend code was mentioned in the traceback. I was fooled by the fact that we had a know bug in cfgrib ecmwf/cfgrib#157 and the error message reported in #4733 was the same. So the PR #4737 is needed anyhow, but it doesn't really solve the issue it refers to :/ |
Description of the issue:
For the bug to occur, it looks like the following requirements need to be met :
h5netcdf
engine.lon_rho
,lat_rho
andh
). Edit: By "auxiliary coordinate", I mean coordinates that depend on a dimension, without being dimensions themselves..where()
must giveFalse
results everywhere.drop=True
must be used.Minimal Complete Verifiable Example:
This is what
da
looks like before the call to .where()What happened:
This is the result obtained from the example above with xarray v0.16.1, as well as the current master.
What you expected to happen:
This is the result obtained for the example above with xarray v0.15.1
Anything else we need to know?:
This following example works as intended. This seems to occur because
isel()
eliminates auxiliary coordinates.Environment:
Output of xr.show_versions()
The text was updated successfully, but these errors were encountered: