You rarely need loops for vector operations in numpy. You can create an uninitialized array and assign to all entries at once:

>>> a = numpy.empty((3,3,))
>>> a[:] = numpy.nan
>>> a
array([[ NaN,  NaN,  NaN],
       [ NaN,  NaN,  NaN],
       [ NaN,  NaN,  NaN]])

I have timed the alternatives a[:] = numpy.nan here and a.fill(numpy.nan) as posted by Blaenk:

$ python -mtimeit "import numpy as np; a = np.empty((100,100));" "a.fill(np.nan)"
10000 loops, best of 3: 54.3 usec per loop
$ python -mtimeit "import numpy as np; a = np.empty((100,100));" "a[:] = np.nan" 
10000 loops, best of 3: 88.8 usec per loop

The timings show a preference for ndarray.fill(..) as the faster alternative. OTOH, I like numpy's convenience implementation where you can assign values to whole slices at the time, the code's intention is very clear.

Note that ndarray.fill performs its operation in-place, so numpy.empty((3,3,)).fill(numpy.nan) will instead return None.

Answer from u0b34a0f6ae on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.5 Manual
numpy.isnan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'isnan'># Test element-wise for NaN and return result as a boolean array.
🌐
AskPython
askpython.com › python › examples › nan-in-numpy-and-pandas
Understanding np.nan in NumPy and Pandas - AskPython
September 30, 2026 - Use the mask to select, replace or count those entries. NumPy documents the same rule in its missing-value guidance, which recommends isnan instead of equality. NaN propagates through ordinary arithmetic because the result depends on a value that is unknown.
🌐
NumPy
numpy.org › doc › stable › user › misc.html
Miscellaneous — NumPy v2.5 Manual
>>> myarr = np.array([1., 0., np.nan, 3.]) >>> np.nonzero(myarr == np.nan) (array([], dtype=int64),)
People also ask

Is np.nan a float?
Yes. np.nan is a floating-point value, so including it in a NumPy array of integers causes the array to use a floating-point dtype.
🌐
askpython.com
askpython.com › python › examples › nan-in-numpy-and-pandas
Understanding np.nan in NumPy and Pandas - AskPython
What does np.nan do?
np.nan is a floating-point NaN value used to mark an unavailable numeric entry. Use np.isnan to detect it in a NumPy array.
🌐
askpython.com
askpython.com › python › examples › nan-in-numpy-and-pandas
Understanding np.nan in NumPy and Pandas - AskPython
Can I sum an array that contains np.nan?
Yes, but a regular NumPy sum returns NaN when a NaN participates. Use np.nansum only when ignoring missing entries is valid for the calculation.
🌐
askpython.com
askpython.com › python › examples › nan-in-numpy-and-pandas
Understanding np.nan in NumPy and Pandas - AskPython
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.nansum.html
numpy.nansum — NumPy v2.5 Manual
Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. In NumPy versions <= 1.9.0 Nan is returned for slices that are all-NaN or empty.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.nansum.html
numpy.nansum — NumPy v2.1 Manual
Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero. In NumPy versions <= 1.9.0 Nan is returned for slices that are all-NaN or empty.
🌐
Reintech
reintech.io › blog › handling-missing-data-nan-values-numpy
Handling Missing Data and NaN Values in NumPy - Reintech
June 13, 2026 - NumPy uses IEEE 754 floating-point special value np.nan to represent missing or undefined numerical data.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.2 Manual
numpy.isnan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'isnan'># Test element-wise for NaN and return result as a boolean array.
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.1 Manual
numpy.isnan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'isnan'># Test element-wise for NaN and return result as a boolean array.
Find elsewhere
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Replace NaN (np.nan) using np.nan_to_num() and np.isnan() | note.nkmk.me
January 23, 2024 - If you want to generate NaN explicitly, use np.nan or float('nan'). You can also import the math module of the standard library and use math.nan.
🌐
Interactive Chaos
interactivechaos.com › en › python › function › numpynan
numpy.nan | Interactive Chaos
January 16, 2021 - numpy.nan · Library · NumPy · Syntax · numpy.nan · Description · numpy.nan is a special value from the NumPy library that represents the null value. Parameters · numpy.nan does not accept arguments. Result · numpy.nan returns a value of type float. Examples · We can create a NumPy ...
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Remove NaN (np.nan) from an array | note.nkmk.me
January 23, 2024 - You can use np.isnan() to check if values in an ndarray are NaN.
🌐
Appdividend
appdividend.com › np-nan
Numpy.nan (np.nan) in Python
July 11, 2025 - The np.nan is a special floating-point constant defined in the NumPy library that represents "Not a Number" (NaN).
🌐
NumPy
numpy.org › doc › stable › reference › constants.html
Constants — NumPy v2.5 Manual
numpy.nan# IEEE 754 floating point representation of Not a Number (NaN). Returns · y : A floating point representation of Not a Number. See Also · isnan : Shows which elements are Not a Number. isfinite : Shows which elements are finite (not one of Not a Number, positive infinity and negative ...
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.nan_to_num.html
numpy.nan_to_num — NumPy v2.5 Manual
Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.
🌐
Saturn Cloud
saturncloud.io › blog › efficiently-checking-if-arbitrary-object-is-nan-in-python-numpy-pandas
Efficiently Checking if Arbitrary Object is NaN in Python Numpy and Pandas | Saturn Cloud Blog
May 1, 2026 - In NumPy, the nan_to_num() function can be used to replace NaN values with a specified value. In Pandas, the isna() function can be used to check for NaN values in a DataFrame or Series, and the fillna() function can be used to replace NaN values ...
🌐
Kasim Te
kasimte.com › 2020 › 02 › 13 › how-do-i-count-numpy-nans.html
How do I count numpy nans? | Kasim Te
February 13, 2020 - In order to count the number of nan instances in the dataset, we can call np.isnan to return a mask of true / false depending on whether the data is nan.
🌐
Codecademy
codecademy.com › docs › python:numpy › math methods › .nansum()
Python:NumPy | Math Methods | .nansum() | Codecademy
November 28, 2024 - In NumPy, the .nansum() function calculates the sum of array elements, treating NaN (Not a Number) values as zero.
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.isnan.html
numpy.isnan — NumPy v2.6.dev0 Manual
numpy.isnan(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature]) = <ufunc 'isnan'># Test element-wise for NaN and return result as a boolean array.