(self)
| 1212 | assert v1.no_conflicts(v4) |
| 1213 | |
| 1214 | def test_as_variable(self): |
| 1215 | data = np.arange(10) |
| 1216 | expected = Variable("x", data) |
| 1217 | expected_extra = Variable( |
| 1218 | "x", data, attrs={"myattr": "val"}, encoding={"scale_factor": 1} |
| 1219 | ) |
| 1220 | |
| 1221 | assert_identical(expected, as_variable(expected)) |
| 1222 | |
| 1223 | ds = Dataset({"x": expected}) |
| 1224 | var = as_variable(ds["x"]).to_base_variable() |
| 1225 | assert_identical(expected, var) |
| 1226 | assert not isinstance(ds["x"], Variable) |
| 1227 | assert isinstance(as_variable(ds["x"]), Variable) |
| 1228 | |
| 1229 | xarray_tuple = ( |
| 1230 | expected_extra.dims, |
| 1231 | expected_extra.values, |
| 1232 | expected_extra.attrs, |
| 1233 | expected_extra.encoding, |
| 1234 | ) |
| 1235 | assert_identical(expected_extra, as_variable(xarray_tuple)) |
| 1236 | |
| 1237 | with pytest.raises(TypeError, match=r"tuple of form"): |
| 1238 | as_variable(tuple(data)) |
| 1239 | with pytest.raises(ValueError, match=r"tuple of form"): # GH1016 |
| 1240 | as_variable(("five", "six", "seven")) |
| 1241 | with pytest.raises(TypeError, match=r"without an explicit list of dimensions"): |
| 1242 | as_variable(data) |
| 1243 | |
| 1244 | with pytest.warns(FutureWarning, match="IndexVariable"): |
| 1245 | actual = as_variable(data, name="x") |
| 1246 | assert_identical(expected.to_index_variable(), actual) |
| 1247 | |
| 1248 | actual = as_variable(0) |
| 1249 | expected = Variable([], 0) |
| 1250 | assert_identical(expected, actual) |
| 1251 | |
| 1252 | data2: np.ndarray[tuple[int, int], np.dtype[np.signedinteger[Any]]] = np.arange( |
| 1253 | 9 |
| 1254 | ).reshape((3, 3)) |
| 1255 | expected = Variable(("x", "y"), data2) |
| 1256 | with pytest.raises(ValueError, match=r"without explicit dimension names"): |
| 1257 | as_variable(data2, name="x") |
| 1258 | |
| 1259 | # name of nD variable matches dimension name |
| 1260 | actual = as_variable(expected, name="x") |
| 1261 | assert_identical(expected, actual) |
| 1262 | |
| 1263 | # test datetime, timedelta conversion |
| 1264 | dt = np.array([datetime(1999, 1, 1) + timedelta(days=x) for x in range(10)]) |
| 1265 | with pytest.warns(FutureWarning, match="IndexVariable"): |
| 1266 | assert as_variable(dt, "time").dtype.kind == "M" |
| 1267 | td = np.array([timedelta(days=x) for x in range(10)]) |
| 1268 | with pytest.warns(FutureWarning, match="IndexVariable"): |
| 1269 | assert as_variable(td, "time").dtype.kind == "m" |
| 1270 | |
| 1271 | with pytest.raises(TypeError): |
nothing calls this directly
no test coverage detected