Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 0 additions & 1 deletion doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ ndarray attributes
DataArray.shape
DataArray.size
DataArray.dtype
DataArray.nbytes
DataArray.chunks


Expand Down
6 changes: 6 additions & 0 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,12 @@ def size(self) -> int:

@property
def nbytes(self) -> int:
"""
Total bytes consumed by the elements of this DataArray's data.

If the backend data array does not include ``nbytes``, estimates
the bytes consumed based on the ``size`` and ``dtype``.
"""
return self.variable.nbytes

@property
Expand Down
6 changes: 6 additions & 0 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,6 +1378,12 @@ def __array__(self, dtype=None):

@property
def nbytes(self) -> int:
"""
Total bytes consumed by the data arrays of all variables in this dataset.

If the backend array for any variable does not include ``nbytes``, estimates
the total bytes for that array based on the ``size`` and ``dtype``.
"""
return sum(v.nbytes for v in self.variables.values())

@property
Expand Down
8 changes: 7 additions & 1 deletion xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,13 @@ def shape(self):

@property
def nbytes(self):
return self.size * self.dtype.itemsize
"""
Total bytes consumed by the elements of the data array.
"""
if hasattr(self.data, "nbytes"):
return self.data.nbytes
else:
return self.size * self.dtype.itemsize

@property
def _in_memory(self):
Expand Down
6 changes: 6 additions & 0 deletions xarray/tests/test_array_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def test_indexing(arrays) -> None:
assert_equal(actual, expected)


def test_properties(arrays) -> None:
np_arr, xp_arr = arrays
assert np_arr.nbytes == 48
assert xp_arr.nbytes == 48


def test_reorganizing_operation(arrays) -> None:
np_arr, xp_arr = arrays
expected = np_arr.transpose()
Expand Down