Python NumPy Copy vs View: Memory Sharing Explained
Learn how NumPy decides whether an operation returns a copy or a view, how to detect which one you have, and how mutation and memory usage behave.

When you slice a NumPy array, the result sometimes shares memory with the original and sometimes does not. That distinction — whether an operation returns a copy or a view — determines whether mutations propagate back to the source array. Understanding the copy-versus-view distinction is essential for writing correct array code and for controlling memory usage in numerical workloads.
The base attribute reveals the relationship
Every NumPy array has a base attribute. If the array owns its own data buffer, base is None. If the array is a view, base points to the array that owns the underlying data.
import numpy as np
a = np.arange(10)
b = a[2:8]
print(a.base) # None
print(b.base) # [0 1 2 3 4 5 6 7 8 9]The base chain can be deeper than one level. A view of a view points back through that chain, so you may need to trace it when debugging.
Operations that return views
Basic slicing, reshape, transpose, and ravel all return views when the memory layout permits them.
a = np.arange(12).reshape(3, 4)
row = a[1] # view
t = a.T # view
r = a.reshape(4, 3) # view
v = a.ravel() # view when the array is contiguousThese operations do not copy the data buffer. They create a new array object that points at the same memory. Writing through any of them changes the original array.
row[0] = 99
print(a[1, 0]) # 99, because row is a view of areshape returns a view only when the new shape is compatible with the existing memory layout. If the array is non-contiguous or the reshape requires a different traversal order, NumPy falls back to a copy. ravel behaves the same way: it returns a view for contiguous arrays, otherwise a copy. flatten always returns a copy.
Operations that return copies
Fancy indexing and boolean indexing always return copies. So does the explicit copy method.
a = np.arange(10)
c1 = a[[0, 2, 4]] # fancy indexing: copy
c2 = a[a > 5] # boolean indexing: copy
c3 = a.copy() # explicit copynp.array(a) also copies by default. The copy keyword on np.array is True by default, so np.array(a) produces an independent array. Passing copy=False may return a view, but NumPy may still copy if the input cannot be used directly.
d = np.array(a) # copy
e = np.array(a, copy=False) # may be a view, may be a copyThe distinction matters because a copy has its own data buffer. Mutations to a copy do not affect the source, and mutations to the source do not affect the copy.
Checking whether an array is a view or a copy
The most reliable check is np.shares_memory, which compares two arrays and reports whether they share any memory.
a = np.arange(10)
b = a[2:8]
c = a.copy()
print(np.shares_memory(a, b)) # True
print(np.shares_memory(a, c)) # FalseTwo lighter-weight checks are base and flags.owndata:
print(b.base is not None) # True, b is a view
print(b.flags.owndata) # False
print(c.flags.owndata) # Trueflags.owndata reports whether the array owns its data buffer. A view always has owndata set to False. Keep in mind that base is None is not sufficient when the array was created from a buffer or a memory map, so np.shares_memory is the safest general-purpose check.
Memory and mutation behavior
Views share the data buffer, so creating a view costs almost nothing in memory. The view object itself carries metadata — shape, strides, dtype — but the underlying data is not duplicated. Copies allocate a new buffer and copy every element, which costs time and memory proportional to the array size.
The mutation behavior follows directly. Writing through a view modifies the shared buffer, so the original array changes. Writing through a copy leaves the original untouched.
a = np.arange(10)
v = a[1:5] # view
v[:] = 0
print(a) # [0 0 0 0 0 5 6 7 8 9]This is the source of most bugs in this area: code that slices an array, modifies the slice, and expects the original to remain unchanged. It will not remain unchanged because the slice is a view.
When to force a copy explicitly
You should force a copy when the data must be isolated from future in-place mutations. A view keeps the base object alive and shares its buffer, so reassigning or deleting the original variable does not by itself require a copy; what matters is whether any surviving reference mutates the shared data.
snapshot = data[data > threshold].copy()A copy is also the safe choice when you are about to hand an array to code you do not control. If the receiving function mutates its argument in place, a view would silently change your source data. A copy prevents that.
copy accepts an order parameter for controlling the memory layout of the result: 'C' for row-major, 'F' for column-major, 'A' to match the source, and 'K' to match the layout as closely as possible. Use order='F' when the copy will be consumed by Fortran-order code.
Common pitfalls in production code
The most common mistake is assuming that slicing returns a copy. It does not. The second most common is using reshape and then mutating the result, which changes the original array because the reshape was a view.
a = np.arange(12).reshape(3, 4)
flat = a.reshape(12)
flat[0] = 100
print(a[0, 0]) # 100Another pitfall is relying on base is None to detect views. That check fails for arrays created from memory-mapped files or from foreign buffers. Prefer np.shares_memory when you need a definitive answer.
When debugging, print the shape, strides, and base of the suspect array, then call np.shares_memory against the original. If the answer is True, the operation you used returned a view, and any in-place mutation will propagate.
Try matching browser tools
All toolsSmall utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Python guides
All Python guidesPython iter(): How the Built-in Function Works
Explains Python's iter() built-in: how it creates iterators, the two-argument sentinel form, custom iterator classes, and common edge cases.
Python Pika: Queues, Exchanges, Routing Keys, and Acknowledgements
Learn how to use Python's Pika client with RabbitMQ: declare queues and exchanges, publish with routing keys, consume messages, and handle acknowledgements reliably.
Python Kafka: kafka-python vs Confluent Client
python kafka python vs confluent kafka: Compare kafka-python and Confluent Kafka Python clients: API differences, performance, feature coverage, and when to choose eac...
Python Kafka Offsets, Partitions, and Manual Commits
Learn how Kafka offsets and partitions work and how to manually commit offsets in Python consumers with kafka-python and confluent-kafka. Covers sync vs async commits, rebalances, and seeking.
More in Python
All Python guidesThe guides filed next to this one in Python, in library order.