There are three circumstances where np.nanstd might return NaN:

  1. If the input is empty

  2. If all of the elements in the input are NaN

  3. If one of the elements is either positive or negative infinity. To understand why this happens, remember that the formula for standard deviation is

    Since x contains inf, the mean of x will also be inf. Therefore when computing the deviations from the mean, there is at least one element that is equal to inf - inf. If you try this at the IPython prompt, you will see that inf - inf is defined as NaN.

In the former two cases you should get a helpful warning:

RuntimeWarning: Degrees of freedom <= 0 for slice.
Answer from ali_m on Stack Overflow
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.nanstd.html
numpy.nanstd — NumPy v2.5 Manual
numpy.nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>, mean=<no value>, correction=<no value>)[source]#
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.nanstd.html
numpy.nanstd — NumPy v2.6.dev0 Manual
numpy.nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>, mean=<no value>, correction=<no value>)[source]#
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.nanstd.html
numpy.nanstd — NumPy v2.1 Manual
numpy.nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>, mean=<no value>, correction=<no value>)[source]#
🌐
Medium
medium.com › @amit25173 › understanding-numpy-nanstd-with-examples-6b7af70c7a4e
Understanding numpy.nanstd (With Examples) | by Amit Yadav | Medium
March 6, 2025 - Here, np.nanstd(data) skips the NaN and computes the standard deviation based on the remaining values.
🌐
GeeksforGeeks
geeksforgeeks.org › numpy-nanstd-function-python
numpy.nanstd() function – Python | GeeksforGeeks
June 11, 2020 - numpy.nanstd() function compute the standard deviation along the specified axis, while ignoring NaNs.
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_nanstd_function.htm
NumPy nanstd() Function
The nanstd() function can compute the standard deviation along a specific axis of a multi-dimensional array while ignoring NaN values.
Find elsewhere
🌐
SciPy
docs.scipy.org › doc › scipy-0.14.0 › reference › generated › scipy.stats.nanstd.html
scipy.stats.nanstd — SciPy v0.14.0 Reference Guide
scipy.stats.nanstd(x, axis=0, bias=False)[source]¶ · Compute the standard deviation over the given axis, ignoring nans. See also · nanmean, nanmedian ·
🌐
Octave Forge
octave.sourceforge.io › statistics › function › nanstd.html
Function Reference: nanstd - Octave Forge - SourceForge
nanstd is identical to the std function except that NaN values are ignored. If all values are NaN, the standard deviation is returned as NaN.
🌐
GeeksforGeeks
geeksforgeeks.org › python › numpy-nanstd-function-python
numpy.nanstd() function - Python - GeeksforGeeks
June 11, 2020 - numpy.nanstd() function compute the standard deviation along the specified axis, while ignoring NaNs.
🌐
University of Texas at Austin
het.as.utexas.edu › HET › Software › Numpy › reference › generated › numpy.nanstd.html
numpy.nanstd — NumPy v1.9 Manual
numpy.nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=False)[source]¶ · Compute the standard deviation along the specified axis, while ignoring NaNs. Returns the standard deviation, a measure of the spread of a distribution, of the non-NaN array elements.
🌐
MindSpore
mindspore.cn › docs › en › r1.7 › api_python › numpy › mindspore.numpy.nanstd.html
mindspore.numpy.nanstd | MindSpore 1.7 documentation | MindSpore
mindspore.numpy.nanstd · View page source · mindspore.numpy.nanstd(a, axis=None, dtype=None, ddof=0, keepdims=False)[source] · Computes the standard deviation along the specified axis, while ignoring NaNs. Returns the standard deviation, a measure of the spread of a distribution, of the ...
🌐
NumPy
numpy.org › doc › 2.4 › reference › generated › numpy.nanstd.html
numpy.nanstd — NumPy v2.4 Manual
numpy.nanstd(a, axis=None, dtype=None, out=None, ddof=0, keepdims=<no value>, *, where=<no value>, mean=<no value>, correction=<no value>)[source]#
🌐
GeeksforGeeks
geeksforgeeks.org › scipy-stats-nanstd-function-python
sciPy stats.nanstd() function | Python | GeeksforGeeks
February 11, 2019 - scipy.stats.nanstd(array, axis=0) function calculates the standard deviation by ignoring the Nan (not a number) values of the array elements along the specified axis of the array.
🌐
GitHub
github.com › scipy › scipy › issues › 3105
numpy.std and scipy.stats.nanstd treat bias differently · Issue #3105 · scipy/scipy
December 2, 2013 - def nanstd(x, axis=0, ddof = 0): """ Compute the standard deviation over the given axis, ignoring nans. Parameters ---------- x : array_like Input array. axis : int or None, optional Axis along which the standard deviation is computed.
Author: scipy
🌐
MindSpore
mindspore.cn › mindspore.numpy.nanstd
mindspore.numpy.nanstd | MindSpore 1.5 documentation | MindSpore
mindspore.numpy.nanstd · View page source · mindspore.numpy.nanstd(a, axis=None, dtype=None, ddof=0, keepdims=False)[source] · Computes the standard deviation along the specified axis, while ignoring NaNs. Returns the standard deviation, a measure of the spread of a distribution, of the ...