Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Map values according to a dict#257

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
eriknw wants to merge6 commits intopython-graphblas:main
base:main
Choose a base branch
Loading
fromeriknw:apply_dict

Conversation

eriknw
Copy link
Member

Closes#246.

A.apply({1: 10, 2: 20}, 999)

Heh, I know this isn't high priority (at all!), but it is oh-so-fun and was quick to do. I think the function we jit with numba is pretty competitive.

I doubt this handles UDTs, and we could probably be a bit smarter with dtypes in general.

@eriknweriknw requested a review fromjim22kJune 4, 2022 04:10
@coveralls
Copy link

coveralls commentedJun 4, 2022
edited
Loading

Coverage Status

Coverage: 99.449% (-0.02%) from 99.47% when pullingb703d61 on eriknw:apply_dict into9a4808a on python-graphblas:main.

@jim22k
Copy link
Member

I'm currently -1 on this. The mental model I see here is that we can take a dict and construct a Matrix from it, then use that to update another Matrix. In that case, it would be assign, not apply.

Looking at your example was not at all obvious what was going on. And that is the worry I have with each new addition. They seem simple, but unless someone knows all the corners and shortcuts, it makes it harder to read someone else's code.

I'm open to being convinced, but these are my initial thoughts on the proposal.

@eriknw
Copy link
MemberAuthor

Round(s) of convincing (and likely improvements) is expected and appreciated.

The mental model I see here is that we can take a dict and construct a Matrix from it, then use that to update another Matrix. In that case, it would be assign, not apply.

I have no idea what you mean by this. The equivalent recipe is:

C=Matrix(A.dtype,A.nrows,A.ncols)C(A.S)<<defaultfork,vind.items()C(A==k)<<v

This is just likedf.map(d) in pandas.map would probably be a better method name for us too--it reads better, andapply is already pretty loaded. Having a separate method also make future enhancements easier (no idea what, but I'm regularly impressed by the savviness of users and their suggestions).

I don't doubt use cases for this will arise (supposing we get more users), and alternatives to perform this are pretty ugly and most likely slower. So, I think this is worth exploring, but we can keep it on the back-burner and wait until we have conventions for showing examples in docstrings. We've been pushing core features forward faster than we can document them (with good reason), but we need documentation to catch up, and we should be in the habit of requiring sufficient documentation for PRs like this.

Possible enhancements:

  • It would be nice if we could come up with an efficient recipe that could result in missing values instead of using a default value.
  • Alternatively, to use the original value instead of default or missing.
  • Maybe get the default value fromd.__missing__ if available (pandas does this).

@eriknweriknw added discussionDiscussing a topic with no specific actions yet featureSomething is missing lowpriority labelsJun 4, 2022
@eriknweriknw changed the titleApply with a dict 🎉Map values according to a dictJun 6, 2022
@eriknw
Copy link
MemberAuthor

eriknw commentedJun 12, 2022
edited
Loading

Okay, I think the natural API for this is:

defapplymap(self,mapping,default=None):

wheremapping is a dict-likeMapping. This returns anexpression and does not modify the original.

default can be:

  • None: drop non-matching values
  • scalar: use this as the default value
  • Matrix: get default values from this object
    • if default is self, then the original value is "passed through", and we can have a more efficient recipe
    • mymatrix.applymap acceptsMatrix andmyvector.applymap acceptsVector (naturally)

I think the keys should be coerced to the dtype of the parent object dtype, and values coerced to the output object dtype. This will let us handle UDTs and awkward dtypes likeuint64.

Edit: changed name frommap toapplymap based on suggestion from@jim22k

@eriknweriknw marked this pull request as draftNovember 16, 2022 18:32
@eriknweriknw mentioned this pull requestDec 7, 2022
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@jim22kjim22kAwaiting requested review from jim22k

Assignees
No one assigned
Labels
discussionDiscussing a topic with no specific actions yetfeatureSomething is missinglowpriority
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Apply dict to map values
3 participants
@eriknw@coveralls@jim22k

[8]ページ先頭

©2009-2025 Movatter.jp