There are three circumstances where np.nanstd might return NaN:
If the input is empty
If all of the elements in the input are NaN
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 OverflowThere are three circumstances where np.nanstd might return NaN:
If the input is empty
If all of the elements in the input are NaN
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.
Another possible explanation for inf output from np.nanstd is related what Numpy data type is used to store data. See the example below:
import numpy as np
a = np.array([1239., 1485., 63., 393., 37., 1186., 13., 402., 404., 915.], dtype='float16')
print(np.nanstd(a)) # returns inf
a = a.astype('float32')
print(np.nanstd(a)) # returns 519.87177