| 4649 | assert_allclose(actual, expected) |
| 4650 | |
| 4651 | def test_pad_constant(self) -> None: |
| 4652 | ar = DataArray(np.arange(3 * 4 * 5).reshape(3, 4, 5)) |
| 4653 | actual = ar.pad(dim_0=(1, 3)) |
| 4654 | expected = DataArray( |
| 4655 | np.pad( |
| 4656 | np.arange(3 * 4 * 5).reshape(3, 4, 5).astype(np.float32), |
| 4657 | mode="constant", |
| 4658 | pad_width=((1, 3), (0, 0), (0, 0)), |
| 4659 | constant_values=np.nan, |
| 4660 | ) |
| 4661 | ) |
| 4662 | assert actual.shape == (7, 4, 5) |
| 4663 | assert_identical(actual, expected) |
| 4664 | |
| 4665 | ar = xr.DataArray([9], dims="x") |
| 4666 | |
| 4667 | actual = ar.pad(x=1) |
| 4668 | expected = xr.DataArray([np.nan, 9, np.nan], dims="x") |
| 4669 | assert_identical(actual, expected) |
| 4670 | |
| 4671 | actual = ar.pad(x=1, constant_values=1.23456) |
| 4672 | expected = xr.DataArray([1, 9, 1], dims="x") |
| 4673 | assert_identical(actual, expected) |
| 4674 | |
| 4675 | with pytest.raises(ValueError, match="cannot convert float NaN to integer"): |
| 4676 | ar.pad(x=1, constant_values=np.nan) |
| 4677 | |
| 4678 | def test_pad_coords(self) -> None: |
| 4679 | ar = DataArray( |