Skip to content

DOC: add examples to get_indexer_non_unique #50152

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

Merged
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5557,6 +5557,32 @@ def _should_fallback_to_positional(self) -> bool:
missing : np.ndarray[np.intp]
An indexer into the target of the values not found.
These correspond to the -1 in the indexer array.

Examples
--------
>>> index = pd.Index(['c', 'b', 'a', 'b', 'b'])
>>> index.get_indexer_non_unique(['b', 'b'])
(array([1, 3, 4, 1, 3, 4]), array([], dtype=int64))
Comment on lines +5563 to +5565
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we give this example first?

and then have the examples with missing values

Apart from the first example, can we also have a sentence before the missing values one, describing what the example is demonstrating?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we give this example first?

and then have the examples with missing values

Certainly. I will change the order of examples as you suggested.

Apart from the first example, can we also have a sentence before the missing values one, describing what the example is demonstrating?

Adding a sentence before the example with missing values sounds like a great idea.

To make this example more understandable I suggest the following explanation :
In the example below there are no matched values. For this reason, the returned indexer contains only integers equal to -1. It demonstrates no index at these positions that match the corresponding target values. The mask [0, 1, 2] in the return value shows that the first, second, and third elements are missing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah looks fine


In the example below there are no matched values.

>>> index = pd.Index(['c', 'b', 'a', 'b', 'b'])
>>> index.get_indexer_non_unique(['q', 'r', 't'])
(array([-1, -1, -1]), array([0, 1, 2]))

For this reason, the returned ``indexer`` contains only integers equal to -1.
It demonstrates no index at these positions that match the corresponding
``target`` values. The mask [0, 1, 2] in the return value shows that the first,
second, and third elements are missing.

Notice that the return value is a tuple contains two items. In the example
below the first item is an array of locations in ``index``. The second
item is a mask shows that the first and third elements are missing.

>>> index = pd.Index(['c', 'b', 'a', 'b', 'b'])
>>> index.get_indexer_non_unique(['f', 'b', 's'])
(array([-1, 1, 3, 4, -1]), array([0, 2]))

"""

@Appender(_index_shared_docs["get_indexer_non_unique"] % _index_doc_kwargs)
Expand Down