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
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
https://numpy.org/doc/stable/glossary.html#term-view instead of a
copy as in the case ofbuilt-in Python sequences such as string, tuple
and list.Care must be taken when extractinga small portion from a large
array which becomes useless after theextraction, because the small
portion extracted contains a referenceto the large original array whose
memory will not be released untilall arrays derived from it are
garbage-collected. In such cases anexplicit copy() is recommended.
------ Original Message ------
From: "Leon fevang-gunn"
participants (2)
-
anthony.flury
-
Leon fevang-gunn