Knowledge base / Python
numpy.take: positions, axes and index boundaries
I trace selections through small arrays and correct a wrapping pitfall. An explicit axis helps avoid accidentally selecting from a flattened array.

When I have a list of positions to retrieve, I can use numpy.take. First I check what a position means and which axis it addresses. Values in a customer-ID column are not automatically array indices. This entry uses small synthetic arrays so every selection can be followed visually. The description follows the NumPy 2.5 documentation available on the revision date.
Syntax and a first selection
The take reference gives np.take(a, indices, axis=None, out=None, mode='raise'). The basic example needs no out. That parameter provides a destination buffer with an appropriate shape and data type.
import numpy as np
a = np.array([10, 20, 30, 40])
selected = np.take(a, [2, 0, 2])
# selected: [30, 10, 30]
Position 2 holds 30; position 0 holds 10; the final request selects position 2 again. The result follows the requested order and repetition. It neither sorts nor removes duplicates. The source still contains its four values. This example retrieves data rather than changing the original array.
Positions start at zero. The NumPy indexing guide explains negative indexing from the end. With four elements, -1 identifies 40 and -4 identifies 10. Outside the permitted range, the default mode raises an error. A negative value is not automatically valid merely because it is negative.
Rows, columns and flattening
Suppose rows describe two measurement sites and columns describe three periods:
m = np.array([[10, 20, 30],
[40, 50, 60]])
rows = np.take(m, [1, 0], axis=0)
# [[40, 50, 60], [10, 20, 30]]
columns = np.take(m, [2, 0], axis=1)
# [[30, 10], [60, 40]]
flat = np.take(m, [1, 4])
# [20, 50]
The first selection rearranges whole rows. The second retrieves the same two columns from both rows. Omitting the axis in the third addresses the sequence 10, 20, 30, 40, 50, 60. The result [20, 50] does not represent rows one and four: this matrix has only two rows. I prefer an explicit axis when the task concerns a particular dimension, even if the default call is shorter.
With a one-dimensional list of two indices, our column selection has shape (2, 2). A scalar index removes the selected axis instead. Thus np.take(m, 1, axis=0) gives shape (3,), while np.take(m, [1], axis=0) retains shape (1, 3). That distinction can matter in the next processing step. Write down the expected dimensions alongside the expected values.
raise, wrap and clip
Compare the modes using the same four-element a:
| Selection | Result | Explanation |
|---|---|---|
np.take(a, [0, 5, 7]) |
IndexError |
5 and 7 exceed the range |
np.take(a, [0, 5, 7], mode='wrap') |
[10, 20, 40] |
5 wraps to 1; 7 wraps to 3 |
np.take(a, [0, 5, 7], mode='clip') |
[10, 40, 40] |
both large indices reach the end |
np.take(a, [-1], mode='clip') |
[10] |
the negative index reaches the beginning |
For wrapping, the cycle contains four positions. Index 5 completes a cycle and reaches position 1; index 7 reaches position 3. The last selected value is therefore 40. The earlier result ending in 30 contradicted this rule. Clipping makes no cycle: it limits a position to the boundary, explaining the repeated 40.
I would not use these modes as automatic data repairs. If index 7 came from an incorrect record match, wrapping returns a technically valid value with an accidental business meaning. The default error may expose that problem sooner. I choose a cyclic rule only when a cycle makes sense for the task. Limiting an index to an edge should also be intentional, rather than a way to suppress an inconvenient exception.

Shared positions versus a separate index per row
take answers “retrieve columns 2 and 0 from every row.” To retrieve a different column for each row, consider take_along_axis. For this matrix, selecting column 2 in the first row and column 0 in the second gives 30 and 40:
per_row = np.array([[2], [0]])
chosen = np.take_along_axis(m, per_row, axis=1)
# [[30], [40]]
This result has one column. In contrast, np.take(m, [2, 0], axis=1) produces two columns in each row. The operations answer different questions. Before choosing a function, I describe the intended positions and result shape, rather than looking for whichever spelling seems shortest.
Copies, performance and a next exercise
For the selections shown without out, I work with a separate result. I do not assume the view behaviour of a simple slice. The copies and views guide explains that advanced indexing creates copies, while slices can share data. If you need to replace source positions, read the entry on numpy.put.
I do not promise that this spelling outperforms every alternative indexing expression. I begin with meaning and readability. A performance comparison would need a particular array shape and workload; this entry provides no measurements. A universal “always faster” table would be a poor substitute for that evidence.
For a final exercise, predict the result of selecting [3, 1, 1] from a: 40, 20 and 20. Then explain the shape difference between selecting column 1 and [1] from m. If you can predict values, dimensions and boundary behaviour, you understand the basic contract. When moving to a larger array, carry over the axis meaning as carefully as the index list.
Key takeaways
Does numpy.take select by labels?
No. It selects by zero-based positions. A customer identifier does not automatically become an array index.
What does axis=None mean?
Selection uses the logically flattened array. For a matrix, explicitly use axis=0 for rows or axis=1 for columns.
Does clip keep -1 as the last element?
No. In clip mode, a negative index is limited to the beginning of the range. Ordinary indexing from the end behaves differently.
AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.
