Domanda

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?

È stato utile?

Soluzione

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.])
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top