-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
DataArray.loc fails for duplicates where DataFrame works #2399
Comments
Thanks for the report! This was actually a somewhat intentional omission in xarray, but if would not be particularly difficult to add in this feature if we want it. At the very least, we should note this deviation somewhere in the docs. There are two potentially problematic aspects to the pandas behavior:
Now that you bring this up, I wonder how the existing supporting for indexing like |
Thanks for the feedback!
import pandas as pd
df = pd.DataFrame(data=[0, 1, 2], index=list("aab"))
print(df.loc[list("ab")])
# 0
# a 0
# a 1
# b 2 is an INNER JOIN between the two indexes
Another example: import pandas as pd
df = pd.DataFrame(data=[0, 1], index=list("aa"))
print(df.loc[list("aa")])
# 0
# a 0
# a 1
# a 0
# a 1 is again an INNER JOIN between the two indexes
This translate into an unidimensional index:
Converting it back to the matricial representation:
In summary, my suggestion is to consider the possibility of defining indexing The multi-dimensional indexing, as far as I can see, can always be transformed into the uni-dimensional case and treated as such. |
Let me give a more concrete example of the issue for multi-dimensional indexing: da_unique = xr.DataArray([0, 1], dims=['x'], coords={'x': ['a', 'b']})
da_nonunique = xr.DataArray([0, 1], dims=['x'], coords={'x': ['a', 'a']})
indexer = xr.DataArray([['a']], dims=['y', 'z']) With a unique index, notice how the result takes on the dimensions of the indexer:
What would you propose for the result of |
Now I see the problem. But I think it is solvable. I will ignore the dimension names for now as I don't have The code da_nonunique = xr.DataArray([0, 1], dims=['x'], coords={'x': ['a', 'a']}
indexer = xr.DataArray([['a']], dims=['y', 'z']) can be understood as defining two indexed arrays:
Algorithm:
Concretely, the solution is a bi-dimensional, 1x2 array: | 0 1 |. There is another relevant example. Let the code be da_nonunique = xr.DataArray([0, 1, 2], dims=['x'], coords={'x': ['a', 'a', 'b']}
indexer = xr.DataArray([['a', 'b']], dims=['y', 'z']) We have Algorithm:
The solution is a bi-dimensional, 1x3 array: | 0 1 2 | Explanation
|
Please take a look at xarray's detailed indexing rules: http://xarray.pydata.org/en/stable/indexing.html#indexing-rules
I think this is the crux of the problem. Put another way: why should the result of indexing be a 1x2 array instead of a 2x1 array? Currently (with the exception of indexing by a scalar with an index with duplicates), xarray determines the shape/dimensions resulting from indexing from the shape/dimensions of the indexers not the array being indexed. |
I see. Now I read about it, let me give another shot. Let
and
The result of
as per column vector representation assumption. AnswerLaying down the first dimension gives
By order,
where
And here is my suggestions. Use the mapping The answer is
for
|
Hi again. I'm working on a precise definition of xarray and indexing. I find the official one a bit hard to understand. It might help me come up with a reasonable way to handle duplicate indices. https://drive.google.com/file/d/1uJ_U6nedkNe916SMViuVKlkGwPX-mGK7/view?usp=sharing |
CC @fujiisoup who implemented much of this. I will also take a look at your doc when I have the chance. I do think that handling duplicate matches with indexing is an important use-case. This comes up with nearest neighbor matching as well -- it would be useful to be able to return the full set of matches within a given distance, not just the nearest match. I wonder if it would be more productive to consider a new indexing API for one -> many matches. |
Sorry that I couldn't join the discussion here. Thanks, @horta, for giving the nice document. For
As xarray inherits not only from pandas but also from numpy's multi-dimensional array.
I also think that what is lacking in xarray is this functionality. |
Yes, I'm working on that doc for now to come up a very precise and as simple as possible definitions. |
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 |
Xarray fails with the exception
Output of
xr.show_versions()
The text was updated successfully, but these errors were encountered: