1

Do numpy views store pointers or float objects?

I am trying to use numpy sliding view to perform something without pandas

arr =np.lib.stride_tricks.sliding_window_view(
    (np.arange(510)), (50), axis=0
)

This arr is a read-only object, but is it using the same amount of memory as an ndarray? Apparently .nbytes returns the same, but

from sys import getsizeof
getsizeof(arr)
# Returns 128
getsizeof(arr.copy())
# Returns 184528

What is happening here?

=======================Edit=======================

@juanpa has pointed out views share the same underlying buffer.

AFAI understand, it means views are pass-by-reference

feiyang472
  • 63
  • 5
  • 5
    views share the *same underlying buffer*. Note, `arr` **is** a `numpy.ndarray`. It just shares it's buffer with another array (the one you passed to `np.lib.stride_tricks.sliding_window_view`) – juanpa.arrivillaga Nov 09 '22 at 21:06
  • That small `getsizeof` value means the `view` does not own its databuffer. It's using/referencing the buffer created by the `arange`. – hpaulj Nov 09 '22 at 21:31

0 Answers0