question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mask DataArray values outside given range

See original GitHub issue

I did a quick search and couldn’t find anything like this.

I often find that I need to remove values outside of a certain range (i.e. mask values outside a range to NaN). This means I usually use xr.DataArray.where. However, this is not easy to do when you’re piping operations and the initial array is not equivalent to the modified DataArray you’re operating on.

# this is difficult to do when chaining/piping operations
xda = xda.where((xda > minval) & (xda < maxval))

xr.DataArray.clip achieves something similar, but does not mask the output. It would be great if xr.DataArray.clip or an equivalent could return an array where outside the min and max is the specified value (e.g. NaN). Alternatively, new functions could be added to DataArray as between/outside.

The first option would be to overload the implementation of np.clip, but either way, functionally it would be doing something like in the hacky solution below:

def between(self, min=-np.inf, max=np.inf, return_value=np.NaN):
    xda = self.copy()
    mask = ((xda < min) | (xda > max)).values
    xda.values[mask] = return_value
    return xda

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dcheriancommented, Jul 15, 2020

Sorry @luke-gregor I totally misunderstood your code!

1reaction
keewiscommented, Jul 15, 2020

we need to update the docstrings, right now the only way to learn about that is by reading the examples.

I’m reopening this to make sure we won’t forget.

Read more comments on GitHub >

github_iconTop Results From Across the Web

xarray.DataArray.where
Filter elements from this object according to a condition. This operation follows the normal broadcasting and alignment rules that xarray uses for binary ......
Read more >
xarray.Dataset.where — xarray 0.8.1 documentation
This operation follows the normal broadcasting and alignment rules that xarray uses for binary arithmetic. Parameters: cond : boolean DataArray or Dataset.
Read more >
Xarray masking at certain location - Stack Overflow
I have a xarray data set and want to transform the values of my variable in a specific region within the dataset to...
Read more >
The numpy.ma module — NumPy v1.24 Manual
Mask the array x where the data are exactly equal to value. masked_outside (x, v1, v2[, copy]). Mask an array outside a given...
Read more >
How to clip an xarray to a smaller extent given the lat/lon ...
One way is to create a boolean mask for the dataset coordinates using the extent you specified and then using the . where()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found