-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Xarray equivalent of np.place or df.map(mapping)? #2568
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
Comments
The usual way to do this in xarray would be to use |
I guess I'm thinking about more complex cases such as changing 0 -> 50, 1 -> 29, 2 -> 10
Thoughts on simplifying this? |
I would divide this into two steps: (1) write a function that does this on NumPy arrays and (2) apply it to xarray objects using import numpy as np
import xarray as xr
def remap(array, mapping):
return np.array([mapping[k] for k in array.ravel()]).reshape(array.shape)
ds = xr.Dataset({'test': ('t', [0, 1, 2])})
xr.apply_ufunc(remap, ds, kwargs=dict(mapping={0: 50, 1: 29, 2: 10})) outputs:
|
Thanks for the quick replies! Is there interest in making this a built-in function? If so, I can help contribute a PR. Also wondering about a way to wrap logic to that mapping. Like below 0, replace with -1, between 0 and 10, replace with 5, and above 10, replace with 15 which is possible with three np.place statements I think, but have to think in backwards logic with ds.where(). |
I would lean slightly against adding a dedicated method for this (but could be convinced if others are interested). Usually we copy pandas or numpy APIs, but It might make sense to copy the design of e.g., you could write something like |
Agree that How about We would definitely use this. I agree it'd probably be used less in xarray than in pandas; though I'm keen to expand the API, in a deliberate and careful way, to some of the traditional pandas use-cases (but a small vote among many) |
Has any progress been made on adding a builtin function for this? Thanks. |
No, not from me at least. |
If I were to make a PR, where would this method reside? Would it be under dataset.py and dataarray.py? Also, would I simply call np.select inside the method, and if so, how would I add support for dask? My minimal example atm:
|
In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity If this issue remains relevant, please comment here or remove the |
There's a longer discussion in #6377 so let's close this. |
Problem description
Is there a built in method to map values like 0 to 1?
Expected Output
returns
[1, 1]
The text was updated successfully, but these errors were encountered: