-
-
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
Should Xarray stop doing automatic index-based alignment? #7045
Comments
I think I agree here but a lot of things are going to break. IMO we could first align (hah) these choices to be the same:
so that they're all controlled by
What do you think of making the default FloatIndex use a reasonable (hard to define!) |
The problem is that user expectations are actually rather different for different options:
This would definitely be a step forward! However, it's a tricky nut to crack. We would both need a heuristic for defining Even then, automatic alignment is often problematic, e.g., imagine cases where a coordinate is defined in separate units. |
@shoyer could you maybe provide a code example of the current index aligned behaviour and a future not index aligned behaviour? I am a bit worried about transitioning previous code bases to such new xarray releases |
As a concrete example, suppose we have two datasets:
import numpy as np
import pandas as pd
import xarray
predictions = xarray.DataArray(
np.random.RandomState(0).randn(24*10),
{'time': pd.date_range('2022-01-01', '2022-01-11', freq='1h', closed='left')},
)
observations = xarray.DataArray(
np.random.RandomState(1).randn(31),
{'time': pd.date_range('2022-01-01', '2022-01-31', freq='24h')},
) Today, if you compare these datasets, they automatically align:
With this proposed change, you would get an error, e.g., something like:
Instead, you would need to manually align these objects, e.g., with
or
or
To (partially) simulate the effect of this change on a codebase today, you could write |
I think I really empathize with the pain here. There's a very real explicitness vs "helpfulness" tradeoff, often depending on whether people are doing exploratory research vs hardened production (a bit like Ask vs Guess culture!). But from the perspective of someone who works with lots of people who use Xarray for their daily research, I think this would be a big hurdle, even without considering the change costs. One analogy is xarray vs. pandas for 2D data — among my colleagues xarray is known to be a smaller, more reliable API surface, while pandas is more fully featured but also a maze of surprising methods and behavior ( "Make another mode" can seem like an easy decision — "who doesn't want another mode" — but it could make development more difficult, since we'd need calls to check which mode we're in & tests for those. It's not insurmountable though, and maybe it would only be required in a couple of methods, so testing those would be sufficient to ensure the resulting behavior would be correct? (FWIW we don't use float indexes, so it could be fine to dispense with those) |
I still find myself struggling to understand which of those options are needed for my use cases (inner, outer etc.). Default is working in many cases, but in other cases it is trial and error. In that sense this proposal would make me have to really understand what's going on. The suggestion of another mode by @max-sixty just made me think, if this automatic alignment machinery could be moved to another package. If that package is installed the current behaviour is preserved, if not then the new behaviour proposed by @shoyer comes into play. |
This suggestion looks roughly like what we are discussing in #7041 (comment), i.e., using a custom index that avoids this? So maybe the question here is whether such an Aside from that, with my outside perspective (having used Xarray extremely little, looking at the docs and code occasionally, but developing a similar library that does not have indexes): Indexes (including alignment behavior) feel like a massive complication of Xarray, both conceptually (which includes documentation and teaching efforts) as well as code. If all you require is the |
Another solution for more flexibility or a smooth transition may be to add a build option to the
I agree, although this is getting addressed slowly but surely. In Xarray internals, most of the indexes logic is now in the IMO nearly all the complication and confusion emerge from the mixed concept of a dimension coordinate in the Xarray data model. Once the concept of an index is clearly decoupled from the concept of a coordinate and both concepts are represented as 1st-class citizens, it will help users focusing on the parts of the API and/or documentation that are relevant to their needs. It will also help "selling" Xarray to users who don't need much of the index capabilities (this has been discussed several times, either as external feedback or between Xarray devs, e.g., proposal of a "xarray-lite" package). Finally it will make more affordable major changes such as the one proposed here by @shoyer. |
My take: the main confusion is from trying to support a relational-database-like data model (where inner/outer joins make sense because values are discrete/categorical) AND a multi-dimensional array model for physical sciences (where typically values are floating-point, exact alignment is required, and interpolation is used when alignment is inexact). As a physical sciences guy, I basically never use the database-like behavior, and it only serves to silence alignment errors so that the fallout happens downstream (NaNs from outer joins, empty arrays on inner joins), making it harder to debug. TIL I can just
What happens if I have Cartesian
From my perspective, the dimensions are special coordinates that the arrays happen to be sampled in a rectangular grid on. It's not confusing to me, but maybe that's b/c of my perspective from physical sciences background/usecases. I suppose one could in principle have an array with coordinates such that none of the coordinates aligned with any particular axis, but it seems improbable.
IMO this is asking for weird bugs. In my work I either expect exact alignment, or I want to interpolate. I never want to ignore a mismatch because it's basically just sweeping an error under the rug. In fact, I'd really just like to test that all the dimension coordinates are the same objects, although Python's semantics don't really work with that.
Getting this right would be really powerful. |
I find the analogy with relational databases quite meaningful! Rectangular grids likely have been the primary use case in Xarray for a long time, but I wonder to which extent it is the case nowadays. Probably a good question to ask for the next user survey? Interestingly, the 2021 user survey results (*) show that "interoperability with pandas" is not a critical feature while "label-based indexing, interpolation, groupby, reindexing, etc." is most important, although the description of the latter is rather broad. It would be interesting to compute the correlation between these two variables. The results also show that "more flexible indexing (selection, alignment)" is very useful or critical for 2/3 of the participants. Not sure how to interpret those results within the context of this discussion, though. (*) The 2022 user survey results doesn't show significant differences in general
Not that improbable for unstructured meshes, curvilinear grids, staggered grids, etc. Xarray is often chosen to handle them too (e.g., uxarray, xgcm). |
Another example (recent issue) in favor of stopping doing automatic index-based alignment: #7695. It is hard to report a meaningful error to end-users when alignment fails due to conflicting indexes since it may happen in a lot of different contexts and from a lot of different places. Recently many users have been struggling a lot with this error:
I totally understand their confusion. Most often it is raised from very deep in the stack and things only start to become clearer when we reduce the reproducible example down to User experience could certainly be improved, but it would be a hard task to output a more specific message for each of the many possible use cases. |
In the error message, we can recommend that the user call |
Yes that would certainly be an improvement. I'm not sure how best to represent "the objects they're working with" in the message, though. The Python object names given by the user would be better than the object reprs. Also I guess that it is possible that some of those objects are transformed through the call stack? But yeah even just suggesting to call |
What is your issue?
I am increasingly thinking that automatic index-based alignment in Xarray (copied from pandas) may have been a design mistake. Almost every time I work with datasets with different indexes, I find myself writing code to explicitly align them:
Would it be insane to consider changing Xarray's behavior to stop doing automatic alignment? I imagine we could roll this out slowly, first with warnings and then with an option for disabling it.
If you think this is a good or bad idea, consider responding to this issue with a 👍 or 👎 reaction.
The text was updated successfully, but these errors were encountered: