Is it possible to set a number to NaN or infinity?
问题 Is it possible to set an element of an array to NaN in Python? Additionally, is it possible to set a variable to +/- infinity? If so, is there any function to check whether a number is infinity or not? 回答1: Cast from string using float() : >>> float('NaN') nan >>> float('Inf') inf >>> -float('Inf') -inf >>> float('Inf') == float('Inf') True >>> float('Inf') == 1 False 回答2: Yes, you can use numpy for that. import numpy as np a = arange(3,dtype=float) a[0] = np.nan a[1] = np.inf a[2] = -np.inf