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
isin: better docs, support older numpy and use dask.array.isin. #2038
Conversation
I added a section to the indexing docs showing how to use `isin` with `where` to do idnexing. Adding support for older numpy turned out to be pretty easy, so I added a copy of np.isin in npcompat. This PR also makes use of dask.array.isin, so that we can support a dask array properly as the second argument: dask/dask#3363
xarray/core/dask_array_compat.py
Outdated
mapped = da.atop(_isin_kernel, element_axes + test_axes, | ||
element, element_axes, | ||
test_elements, test_axes, | ||
adjust_chunks={axis: lambda _: 1 for axis in test_axes}, |
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.
E501 line too long (81 > 79 characters)
xarray/core/npcompat.py
Outdated
|
||
def isin(element, test_elements, assume_unique=False, invert=False): | ||
""" | ||
Calculates `element in test_elements`, broadcasting over `element` only. |
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.
E501 line too long (80 > 79 characters)
xarray/core/npcompat.py
Outdated
""" | ||
Calculates `element in test_elements`, broadcasting over `element` only. | ||
Returns a boolean array of the same shape as `element` that is True | ||
where an element of `element` is in `test_elements` and False otherwise. |
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.
E501 line too long (80 > 79 characters)
xarray/core/npcompat.py
Outdated
|
||
`isin` is an element-wise function version of the python keyword `in`. | ||
``isin(a, b)`` is roughly equivalent to | ||
``np.array([item in b for item in a])`` if `a` and `b` are 1-D sequences. |
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.
E501 line too long (81 > 79 characters)
xarray/core/npcompat.py
Outdated
|
||
`element` and `test_elements` are converted to arrays if they are not | ||
already. If `test_elements` is a set (or other non-sequence collection) | ||
it will be converted to an object array with one element, rather than an |
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.
E501 line too long (80 > 79 characters)
""" | ||
element = np.asarray(element) | ||
return np.in1d(element, test_elements, assume_unique=assume_unique, | ||
invert=invert).reshape(element.shape) |
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.
W292 no newline at end of file
Looks great!! Thank you |
def test_isin_dataset(): | ||
ds = Dataset({'x': [1, 2]}) | ||
with pytest.raises(TypeError): | ||
ds.isin(ds) |
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.
You're right, this wasn't really an xfail
which test each value in the array for whether it is contained in the | ||
supplied list, returning a bool array. See :ref:`selecting values with isin` | ||
for full details. Similar to the ``np.isin`` function. | ||
By `Maximilian Roos <https://github.com/maxim-lian>`_. |
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.
Feel free to change! Thank you for adding the docs
I added a section to the indexing docs showing how to use
isin
withwhere
to do indexing.Adding support for older numpy turned out to be pretty easy, so I added a copy of np.isin in npcompat.
This PR also makes use of dask.array.isin, so that we can support a dask array properly as the second argument:
dask/dask#3363