I'm trying to add two numpy arrays, one of which contains NoneType values. Of course when I add them, I get this error:

TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

Is there a way to define the sum of a NoneType and float to be NoneType and keep it in the new array?

有帮助吗?

解决方案

If None is the only non-numeric value that's allowed, then you might want to represent it using NaN instead:

>>> x = np.ones(4)
>>> y = np.array([1., 2., None, 4.], dtype=np.float)
>>> x + y
array([  2.,   3.,  nan,   5.])
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top