Skip to content

Commit 60f8c3d

Browse files
Max JonesIllviljandcherian
authored
Pull xarray's nbytes from nbytes attribute on arrays (#6797)
* Pull xarray's nbytes from nbytes attribute on arrays * Calculate nbytes if it doesn't exist * Add test * Add docstrings * Apply suggestions from code review Co-authored-by: Illviljan <[email protected]> * Add sparse variable test * Add whats-new note Co-authored-by: Illviljan <[email protected]> Co-authored-by: dcherian <[email protected]> Co-authored-by: Deepak Cherian <[email protected]>
1 parent ed56df2 commit 60f8c3d

File tree

7 files changed

+31
-3
lines changed

7 files changed

+31
-3
lines changed

doc/api.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ ndarray attributes
282282
DataArray.shape
283283
DataArray.size
284284
DataArray.dtype
285-
DataArray.nbytes
286285
DataArray.chunks
287286

288287

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ Deprecations
3434
Bug fixes
3535
~~~~~~~~~
3636

37+
- :py:attr:`DataArray.nbytes` now uses the ``nbytes`` property of the underlying array if available.
38+
By `Max Jones <https://github.com/maxrjones>`_.
3739

3840
Documentation
3941
~~~~~~~~~~~~~

xarray/core/dataarray.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,12 @@ def size(self) -> int:
646646

647647
@property
648648
def nbytes(self) -> int:
649+
"""
650+
Total bytes consumed by the elements of this DataArray's data.
651+
652+
If the backend data array does not include ``nbytes``, estimates
653+
the bytes consumed based on the ``size`` and ``dtype``.
654+
"""
649655
return self.variable.nbytes
650656

651657
@property

xarray/core/dataset.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1378,6 +1378,12 @@ def __array__(self, dtype=None):
13781378

13791379
@property
13801380
def nbytes(self) -> int:
1381+
"""
1382+
Total bytes consumed by the data arrays of all variables in this dataset.
1383+
1384+
If the backend array for any variable does not include ``nbytes``, estimates
1385+
the total bytes for that array based on the ``size`` and ``dtype``.
1386+
"""
13811387
return sum(v.nbytes for v in self.variables.values())
13821388

13831389
@property

xarray/core/variable.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,14 @@ def shape(self):
334334
return self._data.shape
335335

336336
@property
337-
def nbytes(self):
338-
return self.size * self.dtype.itemsize
337+
def nbytes(self) -> int:
338+
"""
339+
Total bytes consumed by the elements of the data array.
340+
"""
341+
if hasattr(self.data, "nbytes"):
342+
return self.data.nbytes
343+
else:
344+
return self.size * self.dtype.itemsize
339345

340346
@property
341347
def _in_memory(self):

xarray/tests/test_array_api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ def test_indexing(arrays) -> None:
4343
assert_equal(actual, expected)
4444

4545

46+
def test_properties(arrays) -> None:
47+
np_arr, xp_arr = arrays
48+
assert np_arr.nbytes == np_arr.data.nbytes
49+
assert xp_arr.nbytes == np_arr.data.nbytes
50+
51+
4652
def test_reorganizing_operation(arrays) -> None:
4753
np_arr, xp_arr = arrays
4854
expected = np_arr.transpose()

xarray/tests/test_sparse.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,9 @@ def setUp(self):
274274
self.data = sparse.random((4, 6), random_state=0, density=0.5)
275275
self.var = xr.Variable(("x", "y"), self.data)
276276

277+
def test_nbytes(self):
278+
assert self.var.nbytes == self.data.nbytes
279+
277280
def test_unary_op(self):
278281
assert_sparse_equal(-self.var.data, -self.data)
279282
assert_sparse_equal(abs(self.var).data, abs(self.data))

0 commit comments

Comments
 (0)