문제

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