pydata / pydata/xarray

Expose `coord_mode` in `.pad()`

Open
#6,425 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
4.2k
Forks
1.4k
Avg merge
2d 15h
Merged PRs (30d)
14

Description

Is your feature request related to a problem?

I am experiencing some issues with the way coordinates are padded. Currently the coordinates are padded according to the mode parameter passed to pad() here. For my particular issue I want to be able to have different modes for the data, but effectively pin the mode that is used to pad the coordinate values.

A simple example:

import xarray as xr
import numpy as np

da = xr.DataArray([1,2,3], dims=['x'], coords={'x':[4,5,6]})
da
image

If I pad with constant

da.pad(x=(0,1), mode='constant')

I get this:
image
which is the coordinate padding I need.

But if I pad with wrap:

da.pad(x=(0,1), mode='wrap')

which gives me repeated coordinate values
image

Describe the solution you'd like

I would like to be able to do something like this:

da.pad(x=(0,1), mode='wrap', coord_pad_mode='constant')

and get something like this
image

Since #3596 internally already defines coord_pad_mode it should be easy enough to expose this to the user?

Possibly related (but I think not incompatible with the changes propsed here?): #3868

Happy to work with @TomNicholas on a PR, but wanted to get some feedback/comments first.

cc @dcherian @mark-boer who worked on this code.

Describe alternatives you've considered

No response

Additional context

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in xarray/core/dataset.py around line 7342 and trace how pad() passes its mode to coordinate padding; inspect the prior coord_pad_mode work referenced in #3596. Add the public option so the example can use wrap for data and constant for coordinates, then verify that the resulting coordinates match the requested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.