Skip to content

Commit 2f34895

Browse files
Update docs on view / copies (#8744)
* Clarify #8728 in docs - Add reference to numpy docs on view / copies in the corresponding section of the xarray docs, to help clarify #8728 . - Add note that `da.values()` returns a view in the header for `da.values()`. * tweaks to the header * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * flip order of new .to_values() doc header paragraphs --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6af547c commit 2f34895

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

doc/user-guide/indexing.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ Whether array indexing returns a view or a copy of the underlying
748748
data depends on the nature of the labels.
749749

750750
For positional (integer)
751-
indexing, xarray follows the same rules as NumPy:
751+
indexing, xarray follows the same `rules`_ as NumPy:
752752

753753
* Positional indexing with only integers and slices returns a view.
754754
* Positional indexing with arrays or lists returns a copy.
@@ -765,8 +765,10 @@ Whether data is a copy or a view is more predictable in xarray than in pandas, s
765765
unlike pandas, xarray does not produce `SettingWithCopy warnings`_. However, you
766766
should still avoid assignment with chained indexing.
767767

768-
.. _SettingWithCopy warnings: https://pandas.pydata.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy
768+
Note that other operations (such as :py:meth:`~xarray.DataArray.values`) may also return views rather than copies.
769769

770+
.. _SettingWithCopy warnings: https://pandas.pydata.org/pandas-docs/stable/indexing.html#returning-a-view-versus-a-copy
771+
.. _rules: https://numpy.org/doc/stable/user/basics.copies.html
770772

771773
.. _multi-level indexing:
772774

xarray/core/dataarray.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -761,11 +761,15 @@ def data(self, value: Any) -> None:
761761
@property
762762
def values(self) -> np.ndarray:
763763
"""
764-
The array's data as a numpy.ndarray.
764+
The array's data converted to numpy.ndarray.
765765
766-
If the array's data is not a numpy.ndarray this will attempt to convert
767-
it naively using np.array(), which will raise an error if the array
768-
type does not support coercion like this (e.g. cupy).
766+
This will attempt to convert the array naively using np.array(),
767+
which will raise an error if the array type does not support
768+
coercion like this (e.g. cupy).
769+
770+
Note that this array is not copied; operations on it follow
771+
numpy's rules of what generates a view vs. a copy, and changes
772+
to this array may be reflected in the DataArray as well.
769773
"""
770774
return self.variable.values
771775

0 commit comments

Comments
 (0)