5

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?

1 Answer 1

6

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.])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.