Updating Values with Masks
Introduction
Welcome back to Boolean Arrays and Masks. We’re on Lesson 4, the final lesson in this course. So far, masks have helped us select matching values and answer questions about a dataset. Now we’ll use a mask to change matching values directly while leaving the rest alone. We’ll work through three cases: replacing impossible negative sensor readings, setting sold-out menu prices to 0, and filling in missing step counts. By the end, we’ll know how to update values with a mask and what to watch for when the array stores integers.
Assign a New Value Where the Mask Is True
We can use a mask on the left side of an assignment: array[mask] = value. NumPy replaces the values at positions marked True; values at False positions stay as they are. As you may recall from our earlier lessons, a comparison creates a mask with one result for each array position. Here, a rain gauge has logged a few negative readings. Rain can’t be negative, so for this sensor we treat them as glitches and replace them with 0. (For sensors that can legitimately read below zero, such as thermometers, you would not do this.) As always, we import NumPy as np first; the later snippets in this lesson use the same import.
The condition readings < 0 marks the two negative values. The assignment changes only those positions. The first printed line shows the original array; the second shows the result, with the other readings unchanged.
Watch the Array Change in Place
This assignment changes readings itself. It does not create a separate, shorter array, and it does not require a loop. That is different from filtering: when we used a mask to select values, we got a new array containing only the matches. Here, the array keeps its original length and order, and only the matching positions change. This is useful when we want to clean a dataset but still keep every reading in its original position.
Build the Mask from a Different Array
The array that supplies the mask does not have to be the one we update. If two arrays have the same length and their positions correspond, we can use a condition on one array to update the other. For a menu, stock == 0 marks dishes with no portions left. As a masking exercise, we use that mask to set the matching prices to 0. (A real menu system would record availability separately rather than overwrite a dish’s price; here it just shows one array’s condition updating another array.)
Each position in stock lines up with the same dish in prices. The mask is True for the second and fourth dishes, so only those prices change. The first and third prices remain as they were.
Mind the Dtype: Floats into Integer Arrays
One detail matters when we assign a value: the array keeps its existing dtype. The steps array contains integers, with -1 marking a missing day. Assigning the estimate 7342.6 through a mask does not turn the whole array into floats. NumPy stores the assigned value as an integer, so the fractional part is dropped. We can check both the updated values and the dtype:
The mask marks the positions containing -1, and the assignment replaces those entries. The output shows 7342 at each marked position, and the integer dtype confirms that the array still stores integers. The exact integer type depends on your platform (some systems print int32 instead of int64), but the fractional part is dropped either way. If we need to preserve a fractional estimate, we should use a floating-point array.
Conclusion and Next Steps
We’ve learned to update selected values with array[mask] = value. The mask can come from the array being changed or from another aligned array, and only positions marked True are overwritten. We also saw that the array keeps its dtype, so a float assigned to an integer array loses its fractional part. You’ve completed the final lesson; finish the practices to complete the course. In them, we’ll cap impossible humidity readings, zero out damaged box weights, and fill missing passenger counts while checking the dtype.
