Skip to content

Add a simple nbytes representation in DataArrays and Dataset repr #8702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
74 changes: 74 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
pytest.mark.filterwarnings("error:All-NaN (slice|axis) encountered"),
]

ON_WINDOWS = sys.platform == "win32"


class TestDataArray:
@pytest.fixture(autouse=True)
Expand All @@ -87,6 +89,10 @@ def setup(self):
)
self.mda = DataArray([0, 1, 2, 3], coords={"x": self.mindex}, dims="x")

@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)}
Expand All @@ -105,6 +111,32 @@ def test_repr(self) -> None:
)
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]])
Coordinates:
* x (x) int32 12B 0 1 2
other int32 4B 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(
"""\
Expand All @@ -117,6 +149,26 @@ def test_repr_multiindex(self) -> None:
)
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 16B MultiIndex
* level_1 (x) object 16B 'a' 'a' 'b' 'b'
* level_2 (x) int64 16B 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]],
Expand All @@ -135,6 +187,28 @@ def test_repr_multiindex_long(self) -> None:
)
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])
Coordinates:
* x (x) object 128B MultiIndex
* level_1 (x) object 128B 'a' 'a' 'a' 'a' 'a' 'a' ... 'd' 'd' 'd' 'd' 'd' 'd'
* level_2 (x) int64 128B 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)

def test_properties(self) -> None:
assert_equal(self.dv.variable, self.v)
assert_array_equal(self.dv.values, self.v.values)
Expand Down
46 changes: 33 additions & 13 deletions xarray/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from xarray.core import formatting
from xarray.tests import requires_cftime, requires_dask, requires_netCDF4

ON_WINDOWS = sys.platform == "win32"


class TestFormatting:
def test_get_indexer_at_least_n_items(self) -> None:
Expand Down Expand Up @@ -470,12 +472,20 @@ def test_array_repr(self) -> None:

# Test repr function behaves correctly:
actual = formatting.array_repr(ds_12)
expected = dedent(
"""\
<xarray.DataArray (1, 2) (test: 1)> Size: 8B
array([0])
Dimensions without coordinates: test"""
)
if ON_WINDOWS:
expected = dedent(
"""\
<xarray.DataArray (1, 2) (test: 1)> Size: 4B
array([0])
Dimensions without coordinates: test"""
)
else:
expected = dedent(
"""\
<xarray.DataArray (1, 2) (test: 1)> Size: 8B
array([0])
Dimensions without coordinates: test"""
)

assert actual == expected

Expand All @@ -489,12 +499,21 @@ def test_array_repr(self) -> None:

with xr.set_options(display_expand_data=False):
actual = formatting.array_repr(ds[(1, 2)])
expected = dedent(
"""\
<xarray.DataArray (1, 2) (test: 1)> Size: 8B
0
Dimensions without coordinates: test"""
)
if ON_WINDOWS:
expected = dedent(
"""\
<xarray.DataArray (1, 2) (test: 1)> Size: 4B
0
Dimensions without coordinates: test"""
)

else:
expected = dedent(
"""\
<xarray.DataArray (1, 2) (test: 1)> Size: 8B
0
Dimensions without coordinates: test"""
)

assert actual == expected

Expand Down Expand Up @@ -708,8 +727,9 @@ def test__mapping_repr(display_max_rows, n_vars, n_attr) -> None:
dims_values = formatting.dim_summary_limited(
ds, col_width=col_width + 1, max_rows=display_max_rows
)
expected_size = "640B" if ON_WINDOWS else "1kB"
expected = f"""\
<xarray.Dataset> Size: 1kB
<xarray.Dataset> Size: {expected_size}
{dims_start}({dims_values})
Coordinates: ({n_vars})
Data variables: ({n_vars})
Expand Down
30 changes: 22 additions & 8 deletions xarray/tests/test_variable.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import sys
import warnings
from abc import ABC
from copy import copy, deepcopy
Expand Down Expand Up @@ -58,6 +59,8 @@
[{"x": (3, 1), "z": 2}, ((3, 1), (0, 0), (2, 2))],
]

ON_WINDOWS = sys.platform == "win32"


@pytest.fixture
def var():
Expand Down Expand Up @@ -1228,15 +1231,26 @@ def test_as_variable(self):

def test_repr(self):
v = Variable(["time", "x"], [[1, 2, 3], [4, 5, 6]], {"foo": "bar"})
expected = dedent(
if ON_WINDOWS:
expected = dedent(
"""
<xarray.Variable (time: 2, x: 3)> Size: 24B
array([[1, 2, 3],
[4, 5, 6]])
Attributes:
foo: bar
"""
<xarray.Variable (time: 2, x: 3)> Size: 48B
array([[1, 2, 3],
[4, 5, 6]])
Attributes:
foo: bar
"""
).strip()
).strip()
else:
expected = dedent(
"""
<xarray.Variable (time: 2, x: 3)> Size: 48B
array([[1, 2, 3],
[4, 5, 6]])
Attributes:
foo: bar
"""
).strip()
assert expected == repr(v)

def test_repr_lazy_data(self):
Expand Down