MCPcopy Index your code
hub / github.com/pydata/xarray / test_coords

Method test_coords

xarray/tests/test_dataarray.py:1520–1568  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1518 assert_array_equal(da.coords["time.dayofyear"], da.values)
1519
1520 def test_coords(self) -> None:
1521 # use int64 to ensure repr() consistency on windows
1522 coords = [
1523 IndexVariable("x", np.array([-1, -2], "int64")),
1524 IndexVariable("y", np.array([0, 1, 2], "int64")),
1525 ]
1526 da = DataArray(np.random.randn(2, 3), coords, name="foo")
1527
1528 # len
1529 assert len(da.coords) == 2
1530
1531 # iter
1532 assert list(da.coords) == ["x", "y"]
1533
1534 assert coords[0].identical(da.coords["x"])
1535 assert coords[1].identical(da.coords["y"])
1536
1537 assert "x" in da.coords
1538 assert 0 not in da.coords
1539 assert "foo" not in da.coords
1540
1541 with pytest.raises(KeyError):
1542 da.coords[0]
1543 with pytest.raises(KeyError):
1544 da.coords["foo"]
1545
1546 # repr
1547 expected_repr = dedent(
1548 """\
1549 Coordinates:
1550 * x (x) int64 16B -1 -2
1551 * y (y) int64 24B 0 1 2"""
1552 )
1553 actual = repr(da.coords)
1554 assert expected_repr == actual
1555
1556 # dtypes
1557 assert da.coords.dtypes == {"x": np.dtype("int64"), "y": np.dtype("int64")}
1558
1559 del da.coords["x"]
1560 da._indexes = filter_indexes_from_coords(da.xindexes, set(da.coords))
1561 expected = DataArray(da.values, {"y": [0, 1, 2]}, dims=["x", "y"], name="foo")
1562 assert_identical(da, expected)
1563
1564 with pytest.raises(
1565 ValueError, match=r"cannot drop or update coordinate.*corrupt.*index "
1566 ):
1567 self.mda["level_1"] = ("x", np.arange(4))
1568 self.mda.coords["level_1"] = ("x", np.arange(4))
1569
1570 def test_coords_to_index(self) -> None:
1571 da = DataArray(np.zeros((2, 3)), [("x", [1, 2]), ("y", list("abc"))])

Callers

nothing calls this directly

Calls 7

IndexVariableClass · 0.90
DataArrayClass · 0.90
assert_identicalFunction · 0.90
arangeMethod · 0.80
identicalMethod · 0.45
dtypeMethod · 0.45

Tested by

no test coverage detected