Skip to content
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

Expose coord_mode in .pad() #6425

Open
jbusecke opened this issue Mar 29, 2022 · 1 comment
Open

Expose coord_mode in .pad() #6425

jbusecke opened this issue Mar 29, 2022 · 1 comment

Comments

@jbusecke
Copy link
Contributor

jbusecke commented Mar 29, 2022

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

@TomNicholas
Copy link
Member

TomNicholas commented Mar 29, 2022

This change seems to be enough to get the behaviour @jbusecke wants without breaking any existing tests:

diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py
index 855718cf..21fb0203 100644
--- a/xarray/core/dataset.py
+++ b/xarray/core/dataset.py
@@ -7228,6 +7228,7 @@ class Dataset(DataWithCoords, DatasetReductions, DatasetArithmetic, Mapping):
         ) = None,
         end_values: int | tuple[int, int] | Mapping[Any, tuple[int, int]] | None = None,
         reflect_type: str = None,
+        coord_mode: str = None,
         **pad_width_kwargs: Any,
     ) -> Dataset:
         """Pad this dataset along one or more dimensions.
@@ -7304,6 +7305,7 @@ class Dataset(DataWithCoords, DatasetReductions, DatasetArithmetic, Mapping):
             default with an unaltered reflection around the edge value.  For
             the "odd" style, the extended part of the array is created by
             subtracting the reflected values from two times the edge value.
+        coord_mode : str, default: mode
         **pad_width_kwargs
             The keyword arguments form of ``pad_width``.
             One of ``pad_width`` or ``pad_width_kwargs`` must be provided.
@@ -7339,7 +7341,7 @@ class Dataset(DataWithCoords, DatasetReductions, DatasetArithmetic, Mapping):
         pad_width = either_dict_or_kwargs(pad_width, pad_width_kwargs, "pad")
 
         if mode in ("edge", "reflect", "symmetric", "wrap"):
-            coord_pad_mode = mode
+            coord_pad_mode = coord_mode if coord_mode is not None else mode
             coord_pad_options = {
                 "stat_length": stat_length,
                 "constant_values": constant_values,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants