Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 14 additions & 82 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@
pytest.mark.filterwarnings("error:All-NaN (slice|axis) encountered"),
]

ON_WINDOWS = sys.platform == "win32"


class TestDataArray:
@pytest.fixture(autouse=True)
Expand All @@ -87,121 +85,55 @@ def setup(self):
self.mindex = pd.MultiIndex.from_product(
[["a", "b"], [1, 2]], names=("level_1", "level_2")
)
self.mda = DataArray([0, 1, 2, 3], coords={"x": self.mindex}, dims="x")
self.mda = DataArray([0, 1, 2, 3], coords={"x": self.mindex}, dims="x").astype(
np.uint64
)

@pytest.mark.skipif(
ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr(self) -> None:
v = Variable(["time", "x"], [[1, 2, 3], [4, 5, 6]], {"foo": "bar"})
coords = {"x": np.arange(3, dtype=np.int64), "other": np.int64(0)}
v = v.astype(np.uint64)
coords = {"x": np.arange(3, dtype=np.uint64), "other": np.uint64(0)}
data_array = DataArray(v, coords, name="my_variable")
expected = dedent(
"""\
<xarray.DataArray 'my_variable' (time: 2, x: 3)> Size: 48B
array([[1, 2, 3],
[4, 5, 6]])
Coordinates:
* x (x) int64 24B 0 1 2
other int64 8B 0
Dimensions without coordinates: time
Attributes:
foo: bar"""
)
assert expected == repr(data_array)

@pytest.mark.skipif(
not ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr_windows(self) -> None:
v = Variable(["time", "x"], [[1, 2, 3], [4, 5, 6]], {"foo": "bar"})
coords = {"x": np.arange(3, dtype=np.int64), "other": np.int64(0)}
data_array = DataArray(v, coords, name="my_variable")
expected = dedent(
"""\
<xarray.DataArray 'my_variable' (time: 2, x: 3)> Size: 24B
array([[1, 2, 3],
[4, 5, 6]])
[4, 5, 6]], dtype=uint64)
Coordinates:
* x (x) int64 24B 0 1 2
other int64 8B 0
* x (x) uint64 24B 0 1 2
other uint64 8B 0
Dimensions without coordinates: time
Attributes:
foo: bar"""
)
assert expected == repr(data_array)

@pytest.mark.skipif(
ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr_multiindex(self) -> None:
expected = dedent(
"""\
<xarray.DataArray (x: 4)> Size: 32B
array([0, 1, 2, 3])
array([0, 1, 2, 3], dtype=uint64)
Coordinates:
* x (x) object 32B MultiIndex
* level_1 (x) object 32B 'a' 'a' 'b' 'b'
* level_2 (x) int64 32B 1 2 1 2"""
)
assert expected == repr(self.mda)

@pytest.mark.skipif(
not ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr_multiindex_windows(self) -> None:
expected = dedent(
"""\
<xarray.DataArray (x: 4)> Size: 16B
array([0, 1, 2, 3])
Coordinates:
* x (x) object 32B MultiIndex
* level_1 (x) object 32B 'a' 'a' 'b' 'b'
* level_2 (x) int64 32B 1 2 1 2"""
)
assert expected == repr(self.mda)

@pytest.mark.skipif(
ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr_multiindex_long(self) -> None:
mindex_long = pd.MultiIndex.from_product(
[["a", "b", "c", "d"], [1, 2, 3, 4, 5, 6, 7, 8]],
names=("level_1", "level_2"),
)
mda_long = DataArray(list(range(32)), coords={"x": mindex_long}, dims="x")
mda_long = DataArray(
list(range(32)), coords={"x": mindex_long}, dims="x"
).astype(np.uint64)
expected = dedent(
"""\
<xarray.DataArray (x: 32)> Size: 256B
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
Coordinates:
* x (x) object 256B MultiIndex
* level_1 (x) object 256B 'a' 'a' 'a' 'a' 'a' 'a' ... 'd' 'd' 'd' 'd' 'd' 'd'
* level_2 (x) int64 256B 1 2 3 4 5 6 7 8 1 2 3 4 ... 5 6 7 8 1 2 3 4 5 6 7 8"""
)
assert expected == repr(mda_long)

@pytest.mark.skipif(
not ON_WINDOWS,
reason="Default numpy's dtypes vary according to OS",
)
def test_repr_multiindex_long_windows(self) -> None:
mindex_long = pd.MultiIndex.from_product(
[["a", "b", "c", "d"], [1, 2, 3, 4, 5, 6, 7, 8]],
names=("level_1", "level_2"),
)
mda_long = DataArray(list(range(32)), coords={"x": mindex_long}, dims="x")
expected = dedent(
"""\
<xarray.DataArray (x: 32)> Size: 128B
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31])
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31],
dtype=uint64)
Coordinates:
* x (x) object 256B MultiIndex
* level_1 (x) object 256B 'a' 'a' 'a' 'a' 'a' 'a' ... 'd' 'd' 'd' 'd' 'd' 'd'
Expand Down
Loading