|
13 | 13 | import pytz
|
14 | 14 |
|
15 | 15 | from xarray import Coordinate, Dataset, IndexVariable, Variable, set_options
|
16 |
| -from xarray.core import indexing |
| 16 | +from xarray.core import dtypes, indexing |
17 | 17 | from xarray.core.common import full_like, ones_like, zeros_like
|
18 | 18 | from xarray.core.indexing import (
|
19 | 19 | BasicIndexer, CopyOnWriteArray, DaskIndexingAdapter,
|
@@ -1179,33 +1179,41 @@ def test_indexing_0d_unicode(self):
|
1179 | 1179 | expected = Variable((), u'tmax')
|
1180 | 1180 | assert_identical(actual, expected)
|
1181 | 1181 |
|
1182 |
| - def test_shift(self): |
| 1182 | + @pytest.mark.parametrize('fill_value', [dtypes.NA, 2, 2.0]) |
| 1183 | + def test_shift(self, fill_value): |
1183 | 1184 | v = Variable('x', [1, 2, 3, 4, 5])
|
1184 | 1185 |
|
1185 | 1186 | assert_identical(v, v.shift(x=0))
|
1186 | 1187 | assert v is not v.shift(x=0)
|
1187 | 1188 |
|
1188 |
| - expected = Variable('x', [np.nan, 1, 2, 3, 4]) |
1189 |
| - assert_identical(expected, v.shift(x=1)) |
1190 |
| - |
1191 | 1189 | expected = Variable('x', [np.nan, np.nan, 1, 2, 3])
|
1192 | 1190 | assert_identical(expected, v.shift(x=2))
|
1193 | 1191 |
|
1194 |
| - expected = Variable('x', [2, 3, 4, 5, np.nan]) |
1195 |
| - assert_identical(expected, v.shift(x=-1)) |
| 1192 | + if fill_value == dtypes.NA: |
| 1193 | + # if we supply the default, we expect the missing value for a |
| 1194 | + # float array |
| 1195 | + fill_value_exp = np.nan |
| 1196 | + else: |
| 1197 | + fill_value_exp = fill_value |
| 1198 | + |
| 1199 | + expected = Variable('x', [fill_value_exp, 1, 2, 3, 4]) |
| 1200 | + assert_identical(expected, v.shift(x=1, fill_value=fill_value)) |
| 1201 | + |
| 1202 | + expected = Variable('x', [2, 3, 4, 5, fill_value_exp]) |
| 1203 | + assert_identical(expected, v.shift(x=-1, fill_value=fill_value)) |
1196 | 1204 |
|
1197 |
| - expected = Variable('x', [np.nan] * 5) |
1198 |
| - assert_identical(expected, v.shift(x=5)) |
1199 |
| - assert_identical(expected, v.shift(x=6)) |
| 1205 | + expected = Variable('x', [fill_value_exp] * 5) |
| 1206 | + assert_identical(expected, v.shift(x=5, fill_value=fill_value)) |
| 1207 | + assert_identical(expected, v.shift(x=6, fill_value=fill_value)) |
1200 | 1208 |
|
1201 | 1209 | with raises_regex(ValueError, 'dimension'):
|
1202 | 1210 | v.shift(z=0)
|
1203 | 1211 |
|
1204 | 1212 | v = Variable('x', [1, 2, 3, 4, 5], {'foo': 'bar'})
|
1205 | 1213 | assert_identical(v, v.shift(x=0))
|
1206 | 1214 |
|
1207 |
| - expected = Variable('x', [np.nan, 1, 2, 3, 4], {'foo': 'bar'}) |
1208 |
| - assert_identical(expected, v.shift(x=1)) |
| 1215 | + expected = Variable('x', [fill_value_exp, 1, 2, 3, 4], {'foo': 'bar'}) |
| 1216 | + assert_identical(expected, v.shift(x=1, fill_value=fill_value)) |
1209 | 1217 |
|
1210 | 1218 | def test_shift2d(self):
|
1211 | 1219 | v = Variable(('x', 'y'), [[1, 2], [3, 4]])
|
|
0 commit comments