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 1 commit
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
19 changes: 19 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5557,6 +5557,25 @@ 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(['f', 'b', 's'])
(array([-1, 1, 3, 4, -1]), array([0, 2]))

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


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

Notice that the return value is a tuple contains two items.
The first item is an array of locations in ``index`` and ``x``
Copy link
Member

Choose a reason for hiding this comment

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

what's x?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My mistake, there is no x in my examples ;) In this example, it should be q, r, and t.

is marked by -1, as it is not in ``index``. The second item
is a mask for new index given the current index.
Copy link
Member

Choose a reason for hiding this comment

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

not sure we need to repeat the return description here - perhaps, describe the mask given the example? e.g. given that the first and third elements are missing, the mask shows [0, 2]

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed, there is no need to repeat return sentence. I will add an explanation of what the mask shows.

"""

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