MCPcopy
hub / github.com/pydata/xarray / test_1d_math

Method test_1d_math

xarray/tests/test_variable.py:346–398  ·  view source on GitHub ↗
(self, dtype: np.typing.DTypeLike | None)

Source from the content-addressed store, hash-verified

344
345 @pytest.mark.parametrize("dtype", [float, int])
346 def test_1d_math(self, dtype: np.typing.DTypeLike | None) -> None:
347 x = np.arange(5, dtype=dtype)
348 y = np.ones(5, dtype=dtype)
349
350 # should we need `.to_base_variable()`?
351 # probably a break that `+v` changes type?
352 v = self.cls(["x"], x)
353 base_v = v.to_base_variable()
354 # unary ops
355 assert_identical(base_v, +v)
356 assert_identical(base_v, abs(v))
357 assert_array_equal((-v).values, -x)
358 # binary ops with numbers
359 assert_identical(base_v, v + 0)
360 assert_identical(base_v, 0 + v)
361 assert_identical(base_v, v * 1)
362 if dtype is int:
363 assert_identical(base_v, v << 0)
364 assert_array_equal(v << 3, x << 3)
365 assert_array_equal(v >> 2, x >> 2)
366 # binary ops with numpy arrays
367 assert_array_equal((v * x).values, x**2)
368 assert_array_equal((x * v).values, x**2)
369 assert_array_equal(v - y, v - 1)
370 assert_array_equal(y - v, 1 - v)
371 if dtype is int:
372 assert_array_equal(v << x, x << x)
373 assert_array_equal(v >> x, x >> x)
374 # verify attributes are dropped
375 v2 = self.cls(["x"], x, {"units": "meters"})
376 with set_options(keep_attrs=False):
377 assert_identical(base_v, +v2)
378 # binary ops with all variables
379 assert_array_equal(v + v, 2 * v)
380 w = self.cls(["x"], y, {"foo": "bar"})
381 # With drop_conflicts, v (no attrs) + w (has attrs) should keep w's attrs
382 # Note: IndexVariable ops return Variable, not IndexVariable
383 expected = self.cls(["x"], x + y, {"foo": "bar"}).to_base_variable()
384 assert_identical(v + w, expected)
385 assert_array_equal((v * w).values, x * y)
386
387 # something complicated
388 assert_array_equal((v**2 * w - 1 + x).values, x**2 * y - 1 + x)
389 # make sure dtype is preserved (for Index objects)
390 assert dtype == (+v).dtype
391 assert dtype == (+v).values.dtype
392 assert dtype == (0 + v).dtype
393 assert dtype == (0 + v).values.dtype
394 # check types of returned data
395 assert isinstance(+v, Variable)
396 assert not isinstance(+v, IndexVariable)
397 assert isinstance(0 + v, Variable)
398 assert not isinstance(0 + v, IndexVariable)
399
400 def test_1d_reduce(self):
401 x = np.arange(5)

Callers

nothing calls this directly

Calls 5

assert_identicalFunction · 0.90
set_optionsClass · 0.90
arangeMethod · 0.80
to_base_variableMethod · 0.80
clsMethod · 0.45

Tested by

no test coverage detected