-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
DOC: Improve lookup documentation #61471
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
Open
stevenae
wants to merge
3
commits into
pandas-dev:main
Choose a base branch
from
stevenae:improve_lookup_documentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+25
−8
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add pd_lookup_het() and pd_lookup_hom()
This was referenced May 21, 2025
rhshadrach
requested changes
May 21, 2025
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.
Thanks for the PR! No strong opposition to having both functions, but the performance gain of the _het
version does not seem significant to me.
Addressed your concerns! If you have time for a review.
…On Wed, May 21, 2025, 4:33 PM Richard Shadrach ***@***.***> wrote:
***@***.**** requested changes on this pull request.
No strong opposition to having both functions, but the performance gain of
the _het version does not seem significant to me.
------------------------------
In doc/source/user_guide/indexing.rst
<#61471 (comment)>:
>
- df = pd.DataFrame({'col': ["A", "A", "B", "B"],
- 'A': [80, 23, np.nan, 22],
- 'B': [80, 55, 76, 67]})
- df
- idx, cols = pd.factorize(df['col'])
- df.reindex(cols, axis=1).to_numpy()[np.arange(len(df)), idx]
+ def pd_lookup_hom(df, row_labels, col_labels):
+ rows = df.index.get_indexer(row_labels)
Can you add df = df.loc[:, sorted(set(col_labels))] here.
------------------------------
In doc/source/user_guide/indexing.rst
<#61471 (comment)>:
> +
+.. code-block:: python
+
+ def pd_lookup_het(df, row_labels, col_labels):
+ rows = df.index.get_indexer(row_labels)
+ cols = df.columns.get_indexer(col_labels)
+ sub = df.take(np.unique(cols), axis=1)
+ sub = sub.take(np.unique(rows), axis=0)
+ rows = sub.index.get_indexer(row_labels)
+ values = sub.melt()["value"]
+ cols = sub.columns.get_indexer(col_labels)
+ flat_index = rows + cols * len(sub)
+ result = values[flat_index]
+ return result
+
+For homogeneous column types, it is fastest to skip column subsetting and go directly to numpy:
Nit: NumPy
------------------------------
In doc/source/user_guide/indexing.rst
<#61471 (comment)>:
>
-.. ipython:: python
+For heterogeneous column types, we subset columns to avoid unnecessary numpy conversions:
NumPy again.
—
Reply to this email directly, view it on GitHub
<#61471 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAFZOHK43SLWEMO55YCEVV327TPLNAVCNFSM6AAAAAB5TRZE7WVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDQNJZGAZTKOJZGA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.Follows from #61185
Examples available at https://colab.research.google.com/drive/1MGWX6JVJL5yHyK7BeEBPQAW4tLM3TZL9#scrollTo=DjWfk4i1SiOY