Skip to content

isin: better docs, support older numpy and use dask.array.isin. - #2038

Merged
shoyer merged 4 commits into
pydata:masterfrom
shoyer:isin-updates
Apr 6, 2018
Merged

shoyer merged 4 commits into
pydata:masterfrom
shoyer:isin-updates

Conversation

@shoyer

@shoyer shoyer commented Apr 5, 2018

Copy link
Copy Markdown
Member

I added a section to the indexing docs showing how to use isin with where 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

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
@shoyer
shoyer requested a review from max-sixty April 5, 2018 05:31
Comment thread 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},

Copy link
Copy Markdown
Contributor

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)

Comment thread xarray/core/npcompat.py Outdated

def isin(element, test_elements, assume_unique=False, invert=False):
"""
Calculates `element in test_elements`, broadcasting over `element` only.

Copy link
Copy Markdown
Contributor

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)

Comment thread 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.

Copy link
Copy Markdown
Contributor

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)

Comment thread 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.

Copy link
Copy Markdown
Contributor

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)

Comment thread 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

Copy link
Copy Markdown
Contributor

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)

Comment thread xarray/core/npcompat.py Outdated
"""
element = np.asarray(element)
return np.in1d(element, test_elements, assume_unique=assume_unique,
invert=invert).reshape(element.shape) No newline at end of file

Copy link
Copy Markdown
Contributor

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

@max-sixty

Copy link
Copy Markdown
Collaborator

Looks great!! Thank you

def test_isin_dataset():
ds = Dataset({'x': [1, 2]})
with pytest.raises(TypeError):
ds.isin(ds)

Copy link
Copy Markdown
Collaborator

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

Comment thread doc/whats-new.rst
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>`_.

Copy link
Copy Markdown
Collaborator

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

@shoyer
shoyer merged commit 6402391 into pydata:master Apr 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants