Knowledge base / Python

numpy.argmin: minimum positions, axes and missing data

I trace row and column minima through nine numbers, correct an incorrect result and explain why argmin does not replace nanargmin.

Kajo RudzińskiPublished Updated 7 min read

A pointer highlights the shortest block's position in a compartment grid.

You ask for a minimum and the function returns 3. Is that wrong? With numpy.argmin, it may be exactly right: 3 is a position. I begin by separating two questions: what is the smallest value, and where is it? Then I choose an axis and a rule for missing data. Mixing those decisions can produce an answer that looks plausible while referring to a different part of the input.

The examples are synthetic and follow the NumPy 2.5 documentation available on 23 November 2026. Small numeric arrays are enough to trace every answer by hand. This entry does not cover sorting a whole array or selecting several smallest elements. The aim is to interpret one minimum position correctly before combining it with further operations.

One array: an index and a value are different answers

The argmin documentation defines the result as a minimum index. For a simple array:

import numpy as np

a = np.array([10, 2, 5, 1, 8])
position = np.argmin(a)
value = a[position]

position is 3 and value is 1. Count positions from zero: 10 occupies 0, 2 occupies 1, 5 occupies 2 and the smallest value, 1, occupies 3. If you only need the value, np.min answers that question. Reading the name of the operation is less useful than checking what its output is supposed to represent.

I would not name an index variable minimum, because it could later be mistaken for the value. In a delivery-time report, position 3 does not mean three days. To identify the corresponding record you also need a link between the position and a date or identifier. The array’s ordering then becomes part of the result’s interpretation, rather than an incidental detail of how the input happened to be loaded.

Axis 0: compare down each column

Use a matrix with three rows and three columns:

matrix = np.array([
    [3, 7, 1],
    [8, 2, 9],
    [4, 6, 5]
])
column_positions = np.argmin(matrix, axis=0)
row_positions = np.argmin(matrix, axis=1)

For axis=0, the expected result is [0, 1, 0]. The first column compares 3, 8 and 4, so row 0 wins. The second compares 7, 2 and 6, so row 1 wins. The third compares 1, 9 and 5, so row 0 wins. Every position can be explained by inspecting three numbers rather than trusting an unexplained printed array.

The output contains one position per column. Those positions are row indices because that is the axis along which we searched. The phrase column minimum alone does not clarify whether somebody needs a value or a location. Preserve that distinction in names, especially when passing the result into another calculation where an integer index may also look like a perfectly reasonable numeric measurement.

Axis 1: compare along each row

For axis=1, the result is [2, 1, 0]. The first row’s minimum is 1 at column 2. The second row’s minimum is 2 at column 1. The third row’s minimum is 4 at column 0. The previous English entry incorrectly ended the result with 2, although the value 5 at that position is not the smallest in the third row.

Row Values Minimum index Minimum
0 3, 7, 1 2 1
1 8, 2, 9 1 2
2 4, 6, 5 0 4

For a two-dimensional matrix, I prefer explicit wording: axis=1 returns a column index within each row, while axis=0 returns a row index within each column. That reduces directional confusion. With more dimensions, name what every axis represents before interpreting the output. A shape alone cannot tell a report recipient whether an axis means a customer, a date or a product.

Two sets of guides show different search directions through the same grid.

Without an axis, the index is flat

flat_position = np.argmin(matrix)
coordinates = np.unravel_index(flat_position, matrix.shape)

For our matrix, flat_position is 2 and coordinates is (0, 2). In row-major order, the first row occupies positions 0, 1 and 2. That is where the value 1 lies, below every other value. The scalar 2 is not a row/column pair and should not automatically be used as an index on the first axis. Doing so would select a different object from the intended minimum element.

unravel_index converts a flat index to coordinates for the supplied shape. Its default order is C, meaning row-major. I retain that ordering here rather than mixing it with column-major F ordering. The shape must belong to the array whose index you are interpreting. Reshaping the input afterwards without reconsidering the coordinates would change what those coordinates identify.

Ties select the first minimum in the current ordering

For np.array([4, 1, 1, 7]), argmin selects position 1. The documented rule returns the first occurrence of a minimum. It does not return both positions or add a flag explaining that the result was tied. If ties matter to the decision, you need to make that requirement visible elsewhere in the analysis.

With arbitrary record ordering, first does not automatically mean best under another characteristic. Two equally priced offers may require a delivery-time comparison. Establish that rule separately. Argmin knows neither your secondary criterion nor the business meaning of the tie. Reordering the input can change which equally minimal record is selected even though the minimum value remains the same, so preserve the relationship with source identifiers.

Use nanargmin when the rule is to skip NaN

The earlier Polish entry claimed argmin ignored NaN and treated it as larger than real numbers. I remove that claim. The nanargmin documentation explicitly demonstrates ordinary argmin pointing to a NaN element. For the rule select the smallest known number, use the separate function:

with_gap = np.array([3, np.nan, 1, 5])
position = np.nanargmin(with_gap)

The expected index is 2. After disregarding NaN in the comparison, the smallest known value is 1, still at position 2 in the original array. Skipping a missing value does not shift the other positions. That matters when the returned index identifies a date or a name stored alongside the numeric data. Treating it as an index into a newly shortened array would break that relationship.

An all-NaN slice raises ValueError in nanargmin. The documentation also warns against trusting results for slices containing only NaNs and infinities. I would first establish whether an admissible observation exists for the task. Avoid replacing missing values with zero automatically: zero could become a new minimum without any substantive reason. Handling a lack of usable data is a separate decision from selecting the smallest usable observation.

keepdims helps retrieve values along the axis

positions = np.argmin(matrix, axis=1, keepdims=True)
values = np.take_along_axis(matrix, positions, axis=1)

positions has shape (3, 1) and contains [2], [1], [0]. values retains that shape and contains [1], [2], [4]. take_along_axis retrieves values using indices from the matching slices. I specify the axis explicitly in that call too, keeping the direction visible rather than depending on a reader remembering a default.

Do not casually use column positions as a selection of entire rows. That is another operation and may produce an apparently reasonable shape with a different meaning. Before joining several steps, write down the expected result shape and what each position describes. A small matrix makes this mistake easier to notice than a large dataset, where the selected rows might look plausible enough to survive a quick inspection.

Exercise: change the minimum and predict the positions

Change the last row to [4, 6, 0]. For axis=1, its index changes from 0 to 2. The global minimum moves to (2, 2), flat position 8. Next, introduce NaN and decide whether missingness invalidates the comparison or may be ignored. Set the rule before choosing the function. That keeps the exercise focused on meaning rather than merely producing an output without an exception.

With your own data, check that the evaluated axis has elements, define ties and preserve the mapping back to source records. The numpy.argmax entry covers the opposite direction, and the Python section links other operations. An index is useful once you can explain precisely what it refers to and why that location answers the intended question.

Key takeaways

Does argmin return the smallest number?

It returns its index, not its value. For [10, 2, 5, 1, 8], argmin returns 3 because indexing starts at zero.

What does axis=1 return for a matrix?

The minimum's position within each row, meaning a column index. You receive a separate answer for every row.

Does argmin skip NaN?

Do not treat it as a NaN-skipping function. Use nanargmin for that rule; an all-NaN slice raises ValueError.

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.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.