magnitude[r:r+64] where r is an array is wrong. The variables in the slice must be scalars, magnitude[3:67], not magnitude[[1,2,3]:[5,6,7]].

If you want to collect multiple slices you have to do something like

In [345]: x=np.arange(10)
In [346]: [x[i:i+3] for i in range(4)]
Out[346]: [array([0, 1, 2]), array([1, 2, 3]), array([2, 3, 4]), array([3, 4, 5])]
In [347]: np.array([x[i:i+3] for i in range(4)])
Out[347]: 
array([[0, 1, 2],
       [1, 2, 3],
       [2, 3, 4],
       [3, 4, 5]])

Other SO questions have explored variations on this, trying to find the fastest, but it's hard to get around some sort loop or list comprehension.

I'd suggest working with this answer, and come back with a new question, and a small working example, if you think you need more speed.

Answer from hpaulj on Stack Overflow
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python โ€บ numpy
NumPy: Slicing ndarray | note.nkmk.me
September 26, 2020 - The number of elements selected by slice and the number of elements to be assigned do not have to match. ... In numpy.ndarray, the value on the right side is converted by broadcasting and assigned.
๐ŸŒ
Finxter
blog.finxter.com โ€บ how-to-use-slice-assignment-in-numpy
How to Use Slice Assignment in NumPy? โ€“ Be on the Right Side of Change
NumPy slice assignment allows you to use slicing on the left-hand side of an assignment operation to overwrite a specific subsequence of a NumPy array at once. The right side of the slice assignment operation provides the exact number of elements to replace the selected slice.
Discussions

python - Assign value to multiple slices in numpy - Stack Overflow
In Matlab, you can assign a value to multiple slices of the same list: >> a = 1:10 a = 1 2 3 4 5 6 7 8 9 10 >> a([1:3,7:9]) = 10 a = 10... More on stackoverflow.com
๐ŸŒ stackoverflow.com
April 25, 2017
python - Numpy assigning to slice, when is the array copied - Stack Overflow
Communities for your favorite technologies. Explore all Collectives ยท Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Numpy: Trying to set value on a slice of a slice of an array - Stack Overflow
I've been trying to perform the following: Given an array, select a first set of indices. Then select another set of indices, and change the value of the array to some other value. Sample code: ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Idea: Allow assigning a scalar to all elements of a list slice, just as is allowed with a numpy array - Ideas - Discussions on Python.org
It is easy to assign the same scalar value to every element of a slice of a numpy array, but not so easy for a python list: somenparray[start::step]=42 Doing that to a slice of python list requires specifying how many elements are in the slice: somelist[start::step]=lenslice*[42] # You have ... More on discuss.python.org
๐ŸŒ discuss.python.org
0
May 30, 2023
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.5 Manual
Some useful concepts to remember include: The basic slice syntax is i:j:k where i is the starting index, j is the stopping index, and k is the step (\(k\neq0\)). This selects the m elements (in the corresponding dimension) with index values i, i + k, โ€ฆ, i + (m - 1) k where \(m = q + (r\neq0)\) ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_slicing.asp
NumPy Array Slicing
ufunc Intro ufunc Create Function ufunc Simple Arithmetic ufunc Rounding Decimals ufunc Logs ufunc Summations ufunc Products ufunc Differences ufunc Finding LCM ufunc Finding GCD ufunc Trigonometric ufunc Hyperbolic ufunc Set Operations ... Slicing in python means taking elements from one given index to another given index. We pass slice instead of index like this: [start:end]. We can also define the step, like this: [start:end:step]. ... Note: The result includes the start index, but excludes the end index. Slice elements from index 4 to the end of the array: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[4:]) Try it Yourself ยป
Find elsewhere
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ numpy โ€บ array-slicing
NumPy Array Slicing (With Examples)
Array Slicing is the process of extracting a portion of an array.Array Slicing is the process of extracting a portion of an array. With slicing, we can easily access elements in the array. It can be done on one or more dimensions of a NumPy array. Syntax of NumPy Array Slicing Here's the syntax of array slicing in NumPy: array[start:stop:step] Here,
๐ŸŒ
Earth Data Science
earthdatascience.org โ€บ home
Slice (or Select) Data From Numpy Arrays | Earth Data Science - Earth Lab
September 23, 2019 - One way to get around having to explicit know the number of elements is to use shortcuts such as -1 which identifies the last index for you: # Select the last element of the array avg_monthly_precip[-1] ... You can slice a range of elements from one-dimensional numpy arrays such as the third, fourth and fifth elements, by specifying an index range: [starting_value, ending_value].
๐ŸŒ
Pythoninformer
pythoninformer.com โ€บ python-libraries โ€บ numpy โ€บ index-and-slice
PythonInformer - Indexing and slicing numpy arrays
February 4, 2018 - As with indexing, the array you get back when you index or slice a numpy array is a view of the original array. It is the same data, just accessed in a different order. This is different to lists, where a slice returns a completely new list. Just a quick recap on how slicing works with normal Python lists. Suppose we have a list: ... The slice notation specifies a start and end value [start:end] and copies the list from start up to but not including end.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ reference โ€บ generated โ€บ numpy.s_.html
numpy.s_ โ€” NumPy v2.4 Manual
A nicer way to build up index tuples for arrays. ... Use one of the two predefined instances index_exp or s_ rather than directly using IndexExpression. For any index combination, including slicing and axis insertion, a[indices] is the same as a[np.index_exp[indices]] for any array a. However, np.index_exp[indices] can be used anywhere in Python code and returns a tuple of slice objects that can be used in the construction of complex index expressions.
๐ŸŒ
Turing
turing.com โ€บ kb โ€บ guide-to-numpy-array-slicing
A Useful Guide to NumPy Array Slicing
To slice a 2-D array in NumPy, you have to specify row index and column index which are separated using a comma as shown below.
๐ŸŒ
Python.org
discuss.python.org โ€บ ideas
Idea: Allow assigning a scalar to all elements of a list slice, just as is allowed with a numpy array - Ideas - Discussions on Python.org
May 30, 2023 - It is easy to assign the same scalar value to every element of a slice of a numpy array, but not so easy for a python list: somenparray[start::step]=42 Doing that to a slice of python list requires specifying how many elements are in the slice: ...
๐ŸŒ
Problem Solving with Python
problemsolvingwithpython.com โ€บ 05-NumPy-and-Arrays โ€บ 05.06-Array-Slicing
Array Slicing - Problem Solving with Python
Where <slice> is the slice or section of the array object <array>. The index of the slice is specified in [start:stop]. Remember Python counting starts at 0 and ends at n-1. The index [0:2] pulls the first two values out of an array. The index [1:3] pulls the second and third values out of an array. An example of slicing the first two elements out of an array is below. In [1]: import numpy as np ยท a = np.array([2, 4, 6]) b = a[0:2] print(b) [2 4] On either sides of the colon, a blank stands for "default". [:2] corresponds to [start=default:stop=2] [1:] corresponds to [start=1:stop=default] Therefore, the slicing operation [:2] pulls out the first and second values in an array.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ numpy โ€บ numpy_slicing.htm
NumPy - Slicing
Slicing is the way to extract a subset of data from a NumPy array. It can be performed on one or more dimensions of a NumPy array. We can define which part of the array to be sliced by specifying the start and end index values using [start : end]
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ array-slicing
NumPy Array Slicing
The slicing syntax follows the format array[start:stop:step], where start is the index to begin the slice, stop is the index to end the slice (exclusive), and step defines the interval between elements. import numpy as np # Syntax array_slice = array[start:stop:step]
๐ŸŒ
Towards Data Science
towardsdatascience.com โ€บ home โ€บ latest โ€บ slicing in python: a comprehensive guide
Slicing in Python: A Comprehensive Guide | Towards Data Science
January 16, 2025 - As with tuples, you cannot assign new values to a slice of a string. This will result in a TypeError. NumPy arrays are a powerful data structure used in data science for numerical computing in Python.
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ 2.2 โ€บ user โ€บ basics.indexing.html
Indexing on ndarrays โ€” NumPy v2.2 Manual
As mentioned, one can select a subset of an array to assign to using a single index, slices, and index and mask arrays. The value being assigned to the indexed array must be shape consistent (the same shape or broadcastable to the shape the index produces).