Skip to content

roll doesn't handle periodic boundary conditions well #1875

Closed
@shoyer

Description

@shoyer

DataArray.roll() currently rolls both data variables and coordinates:

>>> arr = xr.DataArray(range(4), [('x', range(0, 360, 90))])
>>> arr.roll(x=2)
<xarray.DataArray (x: 4)>
array([2, 3, 0, 1])
Coordinates:
  * x        (x) int64 180 270 0 90

This is sort of makes sense, but the labels are now all non-monotonic, so you can't even plot the data with xarray. In my experience, you probably want coordinate labels that either look like:

  1. The unrolled original coordinates: [0, 90, 180, 270]
  2. Shifted coordinates: [-180, -90, 0, 90]

It should be easier to accomplish this is in xarray. I currently resort to using roll and manually fixing up coordinates after the fact.

I'm actually not sure if there are any use-cases for the current behavior. Choice (1) would have the virtue of being consistent with shift():

>>> arr.shift(x=2)
<xarray.DataArray (x: 4)>
array([nan, nan,  0.,  1.])
Coordinates:
  * x        (x) int64 0 90 180 270

We could potentially add optional another argument for shifting labels, too, or requiring fixing that up by subtraction.

Note: you might argue that this is overly geoscience specific, and it would be, if this was only for handling a longitude coordinate. But periodic boundary conditions are common in many areas of the physical sciences.

Metadata

Metadata

Assignees

No one assigned

    Labels

    API designneeds releasebreaking changes that should be held until a major release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions