Closed max-sixty closed 3 weeks ago
I'm trying to use more xindex rather than MultiIndex, in pursuit of the new world :)
xindex
But it seems that that causes .where to fail?
.where
MCVE & traceback below
I don't see why this shouldn't work — possibly I'm misunderstanding something?
import numpy as np import xarray as xr ds = xr.tutorial.open_dataset("air_temperature") ds = ds.assign_coords(lat2=ds.lat + 2).set_xindex('lat2').where(lambda x: x)
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[4], line 1 ----> 1 ds = ds.assign_coords(lat2=ds.lat + 2).set_xindex('lat2').where(lambda x: x) File ~/workspace/xarray/xarray/core/common.py:1233, in DataWithCoords.where(self, cond, other, drop) 1230 self = self.isel(**indexers) 1231 cond = cond.isel(**indexers) -> 1233 return ops.where_method(self, cond, other) File ~/workspace/xarray/xarray/core/ops.py:179, in where_method(self, cond, other) 177 # alignment for three arguments is complicated, so don't support it yet 178 join = "inner" if other is dtypes.NA else "exact" --> 179 return apply_ufunc( 180 duck_array_ops.where_method, 181 self, 182 cond, 183 other, 184 join=join, 185 dataset_join=join, 186 dask="allowed", 187 keep_attrs=True, 188 ) File ~/workspace/xarray/xarray/core/computation.py:1252, 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, on_missing_core_dim, *args) 1250 # feed datasets apply_variable_ufunc through apply_dataset_vfunc 1251 elif any(is_dict_like(a) for a in args): -> 1252 return apply_dataset_vfunc( 1253 variables_vfunc, 1254 *args, 1255 signature=signature, 1256 join=join, 1257 exclude_dims=exclude_dims, 1258 dataset_join=dataset_join, 1259 fill_value=dataset_fill_value, 1260 keep_attrs=keep_attrs, 1261 on_missing_core_dim=on_missing_core_dim, 1262 ) 1263 # feed DataArray apply_variable_ufunc through apply_dataarray_vfunc 1264 elif any(isinstance(a, DataArray) for a in args): File ~/workspace/xarray/xarray/core/computation.py:509, in apply_dataset_vfunc(func, signature, join, dataset_join, fill_value, exclude_dims, keep_attrs, on_missing_core_dim, *ar gs) 505 objs = _all_of_type(args, Dataset) 507 if len(args) > 1: 508 args = tuple( --> 509 deep_align( 510 args, 511 join=join, 512 copy=False, 513 exclude=exclude_dims, 514 raise_on_invalid=False, 515 ) 516 ) 518 list_of_coords, list_of_indexes = build_output_coords_and_indexes( 519 args, signature, exclude_dims, combine_attrs=keep_attrs 520 ) 521 args = tuple(getattr(arg, "data_vars", arg) for arg in args) File ~/workspace/xarray/xarray/core/alignment.py:947, in deep_align(objects, join, copy, indexes, exclude, raise_on_invalid, fill_value) 944 else: 945 out.append(variables) --> 947 aligned = align( 948 *targets, 949 join=join, 950 copy=copy, 951 indexes=indexes, 952 exclude=exclude, 953 fill_value=fill_value, 954 ) 956 for position, key, aligned_obj in zip(positions, keys, aligned, strict=True): 957 if key is no_key: File ~/workspace/xarray/xarray/core/alignment.py:883, in align(join, copy, indexes, exclude, fill_value, *objects) 687 """ 688 Given any number of Dataset and/or DataArray objects, returns new 689 objects with aligned indexes and dimension sizes. (...) 873 874 """ 875 aligner = Aligner( 876 objects, 877 join=join, (...) 881 fill_value=fill_value, 882 ) --> 883 aligner.align() 884 return aligner.results File ~/workspace/xarray/xarray/core/alignment.py:574, in Aligner.align(self) 572 self.find_matching_indexes() 573 self.find_matching_unindexed_dims() --> 574 self.assert_no_index_conflict() 575 self.align_indexes() 576 self.assert_unindexed_dim_sizes_equal() File ~/workspace/xarray/xarray/core/alignment.py:318, in Aligner.assert_no_index_conflict(self) 314 if dup: 315 items_msg = ", ".join( 316 f"{k!r} ({v} conflicting indexes)" for k, v in dup.items() 317 ) --> 318 raise ValueError( 319 "cannot re-index or align objects with conflicting indexes found for " 320 f"the following {msg}: {items_msg}\n" 321 "Conflicting indexes may occur when\n" 322 "- they relate to different sets of coordinate and/or dimension names\n" 323 "- they don't have the same type\n" 324 "- they may be used to reindex data along common dimensions" 325 ) ValueError: cannot re-index or align objects with conflicting indexes found for the following dimensions: 'lat' (2 conflicting indexes) Conflicting indexes may occur when - they relate to different sets of coordinate and/or dimension names - they don't have the same type - they may be used to reindex data along common dimensions
No response
Ah great, I think this is a dupe of that, so I'll close.
Though this is a nice example where, IIUC, the behavior doesn't make sense — they're exactly the same indexes, they came from the same object...
What happened?
I'm trying to use more
xindex
rather than MultiIndex, in pursuit of the new world :)But it seems that that causes
.where
to fail?MCVE & traceback below
What did you expect to happen?
I don't see why this shouldn't work — possibly I'm misunderstanding something?
Minimal Complete Verifiable Example
MVCE confirmation
Relevant log output
Anything else we need to know?
No response
Environment