8000 Should xarray.align sort indexes in alignment? · Issue #2719 · pydata/xarray · GitHub
[go: up one dir, main page]

Skip to content
Should xarray.align sort indexes in alignment? #2719
@shoyer

Description

@shoyer

I noticed in pandas-dev/pandas#24959 (which turned up as a failure in our test suite) that pandas sorts by default in Index.union and now Index.intersection, unless the indexes are the same or either index has duplicates. (These aspects are probably bugs.)

It occurs to me that we should make an intentional choice about sorting in xarray.align(), rather than merely following the whims of changed upstream behavior. Note that align() is called internally by all xarray operations that combine multiple objects (e.g., in arithmetic).

My proposal is to use "order of appearance" and not sort by default, but add a sort keyword argument to allow users to control this. Reasons for the default behavior of not sorting:

  1. Sorting can't be undone if the original order is lost, so this preserve maximum flexibility for users.
  2. This matches how we handle the ordering of dimensions in broadcasting.
  3. Pandas is quite inconsistent with how it applies sorting and we don't want to copy that in xarray. We definitely don't want to sort in all cases by default (e.g., if objects have the same index), so we should avoid sorting in others.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      < 2A14 /div>
      0