Open TomNicholas opened 2 years ago
The problem appears to be caused by a bug with our dtypes
module. In this line the current padding code assumes that this
import xarray.core.dtypes
In [20]: dtypes.NA is np.NAN
Out[20]: False
would evaluate to True.
@TomNicholas, I believe the pad() method does not consider any coordinates and only pads the data along the dimensions it contains. That's why the padding leads to a new data array that has the same dimension name as the original one but no coordinates.
We can set the coordinates explicitly using the coords
attribute of the DataArray after padding. Check this example:
import numpy as np
import xarray as xr
da = xr.DataArray(np.arange(9), dim='x')
padded_da = da.pad({'x': (0, 1)}, 'constant')
padded_da.coords['x'] = np.arange(padded_da.shape[0])
print(padded_da)
<xarray.DataArray (x: 3)>
array([ 0., 1., nan])
Coordinates:
* x (x) int64 0 1 2
Hopefully this helps!
What happened?
What did you expect to happen?
It should have successfully padded with a NaN, same as it does if you don't specify
constant_values
:Minimal Complete Verifiable Example
No response
Relevant log output
No response
Anything else we need to know?
No response
Environment
INSTALLED VERSIONS
commit: None python: 3.9.7 | packaged by conda-forge | (default, Sep 29 2021, 19:20:46) [GCC 9.4.0] python-bits: 64 OS: Linux OS-release: 5.11.0-7620-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: ('en_US', 'UTF-8') libhdf5: 1.12.1 libnetcdf: 4.8.1
xarray: 0.20.3.dev4+gdbc02d4e pandas: 1.4.0 numpy: 1.21.4 scipy: 1.7.3 netCDF4: 1.5.8 pydap: None h5netcdf: None h5py: None Nio: None zarr: 2.10.3 cftime: 1.5.1.1 nc_time_axis: None PseudoNetCDF: None rasterio: None cfgrib: None iris: None bottleneck: None dask: 2022.01.1 distributed: 2022.01.1 matplotlib: None cartopy: None seaborn: None numbagg: None fsspec: 2022.01.0 cupy: None pint: None sparse: None setuptools: 59.6.0 pip: 21.3.1 conda: 4.11.0 pytest: 6.2.5 IPython: 8.2.0 sphinx: 4.4.0