Leon,

The documentation at : https://numpy.org/doc/stable/user/basics.indexing.html?highlight=slice does say that slicing creates a view into the original, and not a new object.

So it isn't a bug - it is entirely expected behaviour

To Quote :


NumPy slicing creates a view instead of a copy as in the case of built-in Python sequences such as string, tuple and list. Care must be taken when extracting a small portion from a large array which becomes useless after the extraction, because the small portion extracted contains a reference to the large original array whose memory will not be released until all arrays derived from it are garbage-collected. In such cases an explicit copy() is recommended.




------ Original Message ------
From: "Leon fevang-gunn" <leon.fevanggunn@gmail.com>
To: docs@python.org
Sent: Monday, 18 Jul, 22 At 15:59
Subject: [docs] Suspected bug

Hello.

So I managed to fix it, but I think I had an experience with a bug... It seems that a slice of a numpy array gave me some trouble. Only after redefining the slice as a numpy array would the code run as normal again. In other words:

NOT working:
zk = np.array(list1)
zk2 = zk[a:b]

Working:
zk = np.array(list1)
zk2 = np.array(zk[a:b])

I also checked and it does say that a slice of an array should be an array:

in: zk[a:b]
out: arra(list1[a:b])


The problem was that it would link the two variables zk and zk2 together:

zk = array1
zk2 = array1[a:b]

When i changed zk2[0] to a different value, the corresponding element, zk[a], also changes. So for example:

zk = np.array([1,2,3,4,5])
zk2 = np.array(zk[1:4])


in: print(zk)
print(zk2)
out: array([1,2,3,4,5])
array([2,3,4])

in: zk_2[0] = 0
print(zk)
print(zk2)
out: array([1,0,3,4,5])
array([0,3,4])

Additional information:
- zk is read in from a .csv file using pandas.
- I am using Spyder/Anaconda
- When i change a value of zk_2 in the command window, zk remains the same


Allright, thought I would let someone know. Let me know if you need additional information.

Kind regards,
Leon



_______________________________________________
docs mailing list -- docs@python.org
To unsubscribe send an email to docs-leave@python.org
https://mail.python.org/mailman3/lists/docs.python.org/
Member address: anthony.flury@btinternet.com


-- <br>Anthony Flury<br>anthony.flury@btinternet.com