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/1772#issuecomment-1085742545,https://api.github.com/repos/pydata/xarray/issues/1772,1085742545,IC_kwDOAMm_X85Atx3R,32069530,2022-04-01T10:42:20Z,2022-04-01T10:42:20Z,NONE,"I wake up this issue, Any news ?","{""total_count"": 2, ""+1"": 2, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,280875330 https://github.com/pydata/xarray/issues/1772#issuecomment-791379121,https://api.github.com/repos/pydata/xarray/issues/1772,791379121,MDEyOklzc3VlQ29tbWVudDc5MTM3OTEyMQ==,1119116,2021-03-05T12:08:17Z,2021-03-05T12:09:32Z,NONE,"A possible code for this functionality could be: ``` def nonzero4xarray (da:xarray.DataArray): c = da.where(da, drop=True).coords s = da.sel(c).to_series() s = s[s] o = [] for record in s.index: o.append(dict(list(zip(s.index.names, record)))) return o ``` however the output is sub-optimal, as it yields a list of dicts, each with the corresponding coordinate to the nonzero value. Consider the following example: ``` >>> import numpy as np >>> import xarray as xr >>> x = np.arange(5) * 100 >>> y = np.arange(4) * 100 >>> z = np.arange(3) * 10 >>> da = xr.DataArray(np.random.randn(z.size, y.size, x.size), dims=('z', 'y', 'x'), coords={'x': x, 'y': y, 'z': z}) >>> da.loc[da.isel({'x':[0, 2], 'y':[1,3], 'z':[0]}).coords] = np.nan >>> da.loc[da.isel({'x':[1, 3], 'y':[0], 'z':[-1]}).coords] = np.nan ``` and imagine I want to capture the locations with NaN values. If I do: ``` >>> coords = da.where(da.isnull(), drop=True).coords >>> da.sel(coords) array([[[ 1.586261, -0.2003 , 0.734872, -1.198746], [ nan, -0.108265, nan, 0.754857], [ nan, 0.700742, nan, -1.514642]], [[ 1.109573, nan, -1.596756, nan], [ 0.031771, -0.341263, 1.358767, 0.221174], [-0.671605, 0.923703, 0.085335, -1.672686]]]) ``` I will get the coordinates for a mix of NaN and valid values, depending on how these are distributed. If I want to obtain only the NaN locations: ``` >>> coords = nonzero4xarray(da.isnull()) >>> coords [{'z': 0, 'y': 100, 'x': 0}, {'z': 0, 'y': 100, 'x': 200}, {'z': 0, 'y': 300, 'x': 0}, {'z': 0, 'y': 300, 'x': 200}, {'z': 20, 'y': 0, 'x': 100}, {'z': 20, 'y': 0, 'x': 300}] >>> vals = [da.sel(c).values[()] for c in coords] >>> vals [nan, nan, nan, nan, nan, nan] ``` whose functionality is similar to `numpy.nonzero`, although sub-optimal as we cannot issue `da.sel(coords)` directly.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,280875330 https://github.com/pydata/xarray/issues/1772#issuecomment-553705751,https://api.github.com/repos/pydata/xarray/issues/1772,553705751,MDEyOklzc3VlQ29tbWVudDU1MzcwNTc1MQ==,26384082,2019-11-14T03:18:57Z,2019-11-14T03:18:57Z,NONE,"In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity If this issue remains relevant, please comment here or remove the `stale` label; otherwise it will be marked as closed automatically ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,280875330 https://github.com/pydata/xarray/issues/1772#issuecomment-351594036,https://api.github.com/repos/pydata/xarray/issues/1772,351594036,MDEyOklzc3VlQ29tbWVudDM1MTU5NDAzNg==,6815844,2017-12-14T02:58:02Z,2017-12-14T02:58:02Z,MEMBER,"For the above example, I consider the following output, ```python Dimensions: (nz_points: 11) Dimensions without coordinates: nz_points Data variables: x (nz_points) int64 0 0 1 1 1 2 2 2 3 3 3 y (nz_points) int64 1 2 0 1 2 0 1 2 0 1 2 ``` It would return a `xr.Dataset` each keys of which are the dimension names and their values are `xr.DataArray` of valid indices. `da[da.nonzero()]` or `da.isel(**da.nonzero())` would return a 1d-DataArray with non-dimensional coordinates, `x` and `y`. (`nz_points` is the default dimension, equivalent to `da.nonzero(dim='nz_points')`) `nonzero` method for `Dataset` is not obvious, but the return of `xr.DataArray.nonzero` should be passed to `xr.Dataset.isel()`. ","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,280875330 https://github.com/pydata/xarray/issues/1772#issuecomment-351585455,https://api.github.com/repos/pydata/xarray/issues/1772,351585455,MDEyOklzc3VlQ29tbWVudDM1MTU4NTQ1NQ==,1217238,2017-12-14T02:03:21Z,2017-12-14T02:03:21Z,MEMBER,"What exactly should the result of `DataArray.nonzero()` or `Dataset.nonzero()` be? In NumPy, `nonzero()` returns a tuple of 1D integer arrays suitable for indexing. It's not obvious to me how that generalizes to xarray.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,280875330