-
-
Notifications
You must be signed in to change notification settings - Fork 18.6k
DOC: Add %in% operator into compare w r (GH3850) #5875
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,44 @@ function. | |
For more details and examples see :ref:`the groupby documentation | ||
<groupby.split>`. | ||
|
||
|match|_ | ||
~~~~~~~~~~~~ | ||
|
||
A common way to select data in R is using ``%in%`` which is defined using the | ||
function ``match``. The operator ``%in%`` is used to return a logical vector | ||
indicating if there is a match or not: | ||
|
||
.. code-block:: r | ||
|
||
s <- 0:4 | ||
s %in% c(2,4) | ||
|
||
The :meth:`~pandas.DataFrame.isin` method is similar to R ``%in%`` operator: | ||
|
||
.. ipython:: python | ||
|
||
s = pd.Series(np.arange(5),index=np.arange(5)[::-1],dtype=np.float32) | ||
s.isin([2, 4]) | ||
|
||
The ``match`` function returns a vector of the positions of matches | ||
of its first argument in its second: | ||
|
||
.. code-block:: r | ||
|
||
s <- 0:4 | ||
match(s, c(2,4)) | ||
|
||
The :meth:`~pandas.core.groupby.GroupBy.apply` method can be used to replicate | ||
this: | ||
|
||
.. ipython:: python | ||
|
||
s = pd.Series(np.arange(5),index=np.arange(5)[::-1],dtype=np.float32) | ||
s.apply(lambda x: [2, 4].index(x) if x in [2,4] else np.nan) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right. pd.match() ignores it's na_sentinal argument, but otherwise it's a 1:1 match. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm...looks kind of like Index.get_indexer (and the non_unique version). Never even knew this existed. Maybe should open an issue to see use case / doc or deprecate? I don't see it being used anywhere internally There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
For more details and examples see :ref:`the reshaping documentation | ||
<indexing.basics.indexing_isin>`. | ||
|
||
|tapply|_ | ||
~~~~~~~~~ | ||
|
||
|
@@ -372,6 +410,9 @@ For more details and examples see :ref:`the reshaping documentation | |
.. |aggregate| replace:: ``aggregate`` | ||
.. _aggregate: http://finzi.psych.upenn.edu/R/library/stats/html/aggregate.html | ||
|
||
.. |match| replace:: ``match`` / ``%in%`` | ||
.. _match: http://finzi.psych.upenn.edu/R/library/base/html/match.html | ||
|
||
.. |tapply| replace:: ``tapply`` | ||
.. _tapply: http://finzi.psych.upenn.edu/R/library/base/html/tapply.html | ||
|
||
|
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.
Why the
np.arange(5)[::-1]
? Doesn't that make just more complicated for the reader to understand?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 it would, I was thinking of something...strange.
On 8 January 2014 22:01, Joris Van den Bossche [email protected]:
Chapman