Skip to content

Apply dict to map values #246

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
eriknw opened this issue May 23, 2022 · 3 comments · May be fixed by #257
Open

Apply dict to map values #246

eriknw opened this issue May 23, 2022 · 3 comments · May be fixed by #257
Labels
enhancement Improve existing functionality or make things work better lowpriority

Comments

@eriknw
Copy link
Member

eriknw commented May 23, 2022

This is probably a crazy idea (and I don't currently have a use case), but could we make it easy to apply a dict to map values? For example, to make the following easier and more reliable (i.e., not rely on inlining the dict):

In [10]: v = gb.Vector.from_values(np.arange(5), np.arange(5))

In [11]: v
Out[11]:
"v_2"      nvals  size  dtype  format
gb.Vector      5     5  INT64    full
-------------------------------------
index  0  1  2  3  4
value  0  1  2  3  4

In [12]: v.apply(lambda x: {0: 0, 1: 2, 2: 4, 3: 6, 4: 8}[x]).new()
Out[12]:
"v_4"      nvals  size  dtype  format
gb.Vector      5     5  INT64    full
-------------------------------------
index  0  1  2  3  4
value  0  2  4  6  8

This requires Numba to know the types of the dictionary. Also, I don't know how easy it would be to have a default value. This may require its own method.

This is inspired by the Pandas functionality to be able to use x.map(d) to apply a dict or Series mapping.

@eriknw eriknw added enhancement Improve existing functionality or make things work better lowpriority labels May 23, 2022
@eriknw eriknw linked a pull request Jun 4, 2022 that will close this issue
@SultanOrazbayev
Copy link
Member

I think using dicts is a bit counter-intuitive here. In the spirit of working with matrix representations, wouldn't we want to express this as a matrix operation (via permutation matrix, that could be constructed from dict)?

@eriknw
Copy link
Member Author

eriknw commented Dec 7, 2022

Thanks for reading our issues and commenting @SultanOrazbayev :)

My initial description of and examples for this operation confused @jim22k too. See more discussions in #257

For example, say you wanted to change all 2 values in your matrix to 4 (and let's ignore what happens to the other values for now). This issue proposes "applying a dict mapping" as e.g. A.applymap({2: 4}).

Make sense? We're not doing a permutation, and we're not treating the dict as a matrix.

@SultanOrazbayev
Copy link
Member

OK, I see now. I misread the example to change the indexes, so for mapping values using dict makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improve existing functionality or make things work better lowpriority
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants