html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue https://github.com/pydata/xarray/issues/5727#issuecomment-904642129,https://api.github.com/repos/pydata/xarray/issues/5727,904642129,IC_kwDOAMm_X841675R,4160723,2021-08-24T13:27:20Z,2021-08-24T13:27:20Z,MEMBER,"I agree that this kind of ""boolean indexing"" could be clarified a bit in the docs. I also had to double check here since I use `isel()` / `sel()` much more often than `[]` and `.loc[]`. > Even now, it's not really clear to me why ""boolean""-masking should only work with integer-indexing and not label-indexing. Given that the highlighted maybe_cast_to_coords_dtype is the only reason it does not work for label-based indexing. Hmm you're right. The primary reason of `maybe_cast_to_coords_dtype` is because pandas indexes don't support floating point values with lower precision than `float64` (e.g., #5700). But here https://github.com/pydata/xarray/blob/a6b44d72010c5311fad4f60194b527194e26094b/xarray/core/indexes.py#L236-L237 we actually bypass the underlying pandas index when the labels are a boolean array-like so that boolean indexing should normally work with both integer and label based indexing (#182). So we should probably fix `maybe_cast_to_coords_dtype` with something like ```python def maybe_cast_to_coords_dtype(label, coords_dtype): label_dtype = np.dtype(getattr(label, ""dtype"", None)) if coords_dtype.kind == ""f"" and not isinstance(label, slice) and label_dtype.kind != ""b"": label = np.asarray(label, dtype=coords_dtype) return label ```","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,976207971 https://github.com/pydata/xarray/issues/5727#issuecomment-904597086,https://api.github.com/repos/pydata/xarray/issues/5727,904597086,IC_kwDOAMm_X8416w5e,5802846,2021-08-24T12:28:18Z,2021-08-24T12:31:21Z,CONTRIBUTOR,"Thank you! You are right. I guess based on the docs for [Assigning values with indexing](http://xarray.pydata.org/en/stable/user-guide/indexing.html#assigning-values-with-indexing) I assumed only `.loc` features item assignment . I vaguely remember there where issues with assigning via integer-based indexing. Well, propably my bad. Should we add an example how to perform masking this way to the docs? There are many examples about masking using different ways, but not this simple, intuitive way. Even now, it's not really clear to me why ""boolean""-masking should only work with integer-indexing and not label-indexing. Given that the highlighted `maybe_cast_to_coords_dtype` is the only reason it does not work for label-based indexing. edit: probably the reason is to support label-based indexing for boolean coords?!","{""total_count"": 1, ""+1"": 1, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,976207971 https://github.com/pydata/xarray/issues/5727#issuecomment-904578829,https://api.github.com/repos/pydata/xarray/issues/5727,904578829,IC_kwDOAMm_X8416scN,4160723,2021-08-24T12:05:44Z,2021-08-24T12:05:44Z,MEMBER,"With `da.loc[{'x':~mask}]`, you're actually doing label-based indexing, so `~mask` will select repeatedly the items for which x=1.0 (True) and x=0.0 (False): ```python >>> da.loc[{'x':~mask}] array([1., 0., 1., 0., 1., 0., 1., 0., 1., 0.]) Coordinates: * x (x) float64 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 1.0 0.0 ``` What you want is integer-based indexing (i.e., not using `.loc`): ```python >>> da[{'x':~mask}] array([0., 2., 4., 6., 8.]) Coordinates: * x (x) float64 0.0 2.0 4.0 6.0 8.0 ``` [doc link](http://xarray.pydata.org/en/stable/user-guide/indexing.html#indexing-and-selecting-data).","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,976207971