I would like to convert a NumPy (version 1.11.0) array from float64
to int64
. I want this operation to be able to be done on whole numbers but fail on non-whole numbers.
My understanding was that I could use casting=safe
though clearly my understanding was wrong...
I would hope that the following would work:
np.array([1.0]).astype(int, casting='safe')
And that this would fail:
np.array([1.1]).astype(int, casting='safe')
However they both fail with this error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-51-7261ddf00794> in <module>()
1 import numpy as np
2 print(np.__version__)
----> 3 np.array([1.0]).astype(int, casting='safe')
TypeError: Cannot cast array from dtype('float64') to dtype('int64') according to the rule 'safe'
I'm guessing I have a fundamental misunderstanding of what safe casting means, so perhaps that is not the best way to achieve this, is there a better way for the first example to work but the second to fail?