Skip to main content

What is a view?

A view is an array that shares its underlying data buffer with another array. When you create a view, no data is copied — the new array simply references the same memory with different shape, strides, or offset metadata. Modifying elements through a view changes the original array, and vice versa. A copy is an independent array with its own data buffer. Changes to a copy never affect the original.

Operations that return views

These operations return arrays that share data with the input. No memory is allocated for array elements.
ravel returns a view only when the source array is C-contiguous. reshape returns a view whenever the existing strides can describe the new shape, which always holds for a C-contiguous array and sometimes for others (a transpose reshaped to its own shape, for example); otherwise it copies. A copy made by reshape never aliases the input, but, as in NumPy, it can still report OWNDATA as false because it is a view of its own new buffer.
The last four entries changed in v1.7.0 — they previously returned copies. Each one now matches NumPy, which returns a view in exactly these cases, but code that writes to the result (or to an apply_along_axis callback argument) will now see that write reach the input array. Call .copy() on the result if you relied on the old behavior.
Each of these falls back to allocating when it has real work to do: real_if_close on complex input, and round on floats or with decimals < 0. Rather than guessing from the dtype, compare the result with the input and check base — a result aliases the input when it is the input itself or has a base:
imag(x) for real x is the exception worth remembering: it allocates a fresh zero-filled array and reports no base, so it is never aliased even though real(x) on the same input is.

Operations that return copies

These operations always allocate a new data buffer.

Detecting views: base and flags

The base property

Every view has a base property pointing to the array that owns the data. Arrays that own their data return null.

The flags property

The flags object exposes three boolean flags:

View mutation affects the original

This is the most important consequence of views. Changing a value through any view changes the underlying data for every array that references it.

Contiguity and reshape

An array is C-contiguous when its elements are stored in row-major order with no gaps. Most freshly created arrays are C-contiguous. However, operations like transpose change the stride pattern without moving data, producing arrays that are no longer C-contiguous. This matters because reshape can only return a view when the existing strides can describe the new shape:
If you need to ensure an array is contiguous before reshaping (to guarantee a view), use ascontiguousarray first.

Explicit copies

When you need an independent copy, use copy:
Or use the method form on NDArray:
If you pass an array to a function and want to prevent the function from modifying your data, pass copy(arr) instead. numpy-ts follows NumPy’s convention: slicing returns views, not copies.

Summary