-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
ENH: three argument version of where #1496
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
Conversation
One design decision here is how to handle alignment. The current version of I don't like this behavior for the three argument version of We can't use an outer join because we don't know how to handle NaNs in
I am inclined to require exact alignment (i.e., |
How difficult would it be to include
From your example above (haven't gone through the code), what you have implemented in this PR is a special case, namely the xarray analog to I recently had a usecase where this would be handy. |
That works, too, e.g.,
You can even put Dataset objects in any of the arguments and it should broadcast across variables. One annoyance is that instead of |
Thanks @shoyer. I guess what I had in mind is the case where both
|
@spencerahill It sounds like we should just add the function, too, so you can write |
This is excellent! Thank you @shoyer . I agree with forcing exact alignment for the 3 arg version. Does |
I'm not quite sure what you mean by this, can you explain? I think the answer is probably no (but perhaps we could change that) |
Not a clear comment. I had meant for this to work, which it does: In [1]: import xarray as xr
In [2]: import numpy as np
In [3]: a = xr.DataArray(np.random.rand(3,4), dims=['x','y'])
In [4]: b = xr.DataArray(np.random.rand(3,1), dims=['x','y'])
In [5]: a+b.squeeze()
Out[5]:
<xarray.DataArray (x: 3, y: 4)>
array([[ 1.394345, 0.916842, 0.806284, 1.604707],
[ 1.011313, 0.736347, 0.677679, 0.970856],
[ 0.477433, 0.825672, 1.014959, 0.495829]])
Dimensions without coordinates: x, y
In [6]: a.where(a>0.5, b.squeeze())
Out[6]:
<xarray.DataArray (x: 3, y: 4)>
array([[ 0.676462, 0.717883, 0.717883, 0.886823],
[ 0.568829, 0.442484, 0.442484, 0.528372],
[ 0.100403, 0.725269, 0.914556, 0.100403]])
Dimensions without coordinates: x, y I had meant: |
Yes, that works :). Exact only requires that common dimensions have the same lengths/labels, not that all arguments have the same dimensions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shoyer - This is looking very clean. I actually had a branch working on the same thing but this is a far better way to do this. My only comment is that I think we do want the where function to be exposed as part of this PR so let's do that before merging.
doc/whats-new.rst
Outdated
@@ -31,6 +31,9 @@ Enhancements | |||
|
|||
- Speed-up (x 100) of :py:func:`~xarray.conventions.decode_cf_datetime`. | |||
By `Christian Chwala <https://github.com/cchwala>`_. | |||
- :py:meth:`~xarray.Dataset.where` now supports the ``other`` argument, for | |||
filling with a value other than ``NaN`` (:issue:`576`). | |||
By `Stephan Hoyer <https://github.com/shoyer>`_. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also comment about xr.where
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MaximilianR and @spencerahill - any final comments. This is looking pretty good to me.
xarray/tests/__init__.py
Outdated
def raises_regex(error, pattern): | ||
with pytest.raises(error) as excinfo: | ||
yield | ||
excinfo.match(pattern) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recall now that the match
method was only added pytest 3.0. I think there's a simple work around for earlier versions of pytest, so I'm inclined to switch to that rather than require newer pytest for the test suite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to play devils advocate, 3.0 has been out for a year now. For us developers, its trivial to get the current release via conda/pip... so I'm not sure we really need to worry about maintaining backward compatibility.
Obviously, I won't fight you if you think we should handle version 2 as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I'm not opposed to requiring pytest 3 when we have something that really needs it. But the work around here is barely an inconvenience.
LGTM. Merge when ready. |
* ENH: three argument version of where Fixes GH576 * Docstring fixup * Use join=exact for three argument where * Add where function * Don't require pytest 3 for raises_regex
Example usage:
git diff upstream/master | flake8 --diff
whats-new.rst
for all changes andapi.rst
for new APICC @MaximilianR