Discussion:
[Numpy-discussion] Misleading/erroneous TypeError message
Peter Creasey
2015-11-24 19:42:19 UTC
Permalink
Hi,

I just upgraded my numpy and started to received a TypeError from one of my
codes that relied on the old, less strict, casting behaviour. The error
message, however, left me scratching my head when trying to debug something
a = array([0],dtype=uint64)
a += array([1],dtype=int64)
TypeError: Cannot cast ufunc add output from dtype('float64') to
dtype('uint64') with casting rule 'same_kind'

Where does the 'float64' come from?!?!

Peter

PS Thanks for all the great work guys, numpy is a fantastic tool and has
been a lot of help to me over the years!
Charles R Harris
2015-11-24 20:39:20 UTC
Permalink
On Tue, Nov 24, 2015 at 12:42 PM, Peter Creasey <
Post by Peter Creasey
Hi,
I just upgraded my numpy and started to received a TypeError from one of
my codes that relied on the old, less strict, casting behaviour. The error
message, however, left me scratching my head when trying to debug something
a = array([0],dtype=uint64)
a += array([1],dtype=int64)
TypeError: Cannot cast ufunc add output from dtype('float64') to
dtype('uint64') with casting rule 'same_kind'
Where does the 'float64' come from?!?!
The combination of uint64 and int64 leads to promotion to float64 as the
best option for the combination of signed and unsigned. To fix things, you
can either use `np.add` with an output argument and `casting='unsafe'` or
just be careful about using unsigned types.

Chuck
Peter Creasey
2015-11-25 01:42:51 UTC
Permalink
Post by Charles R Harris
Post by Peter Creasey
I just upgraded my numpy and started to received a TypeError from one of
my codes that relied on the old, less strict, casting behaviour. The error
message, however, left me scratching my head when trying to debug something
a = array([0],dtype=uint64)
a += array([1],dtype=int64)
TypeError: Cannot cast ufunc add output from dtype('float64') to
dtype('uint64') with casting rule 'same_kind'
Where does the 'float64' come from?!?!
The combination of uint64 and int64 leads to promotion to float64 as the
best option for the combination of signed and unsigned. To fix things, you
can either use `np.add` with an output argument and `casting='unsafe'` or
just be careful about using unsigned types.
Thanks for the quick response. I understand there are reasons for the
promotion to float64 (although my expectation would usually be that
Numpy is going to follow C conventions), however the I found the error
a little unhelpful. In particular Numpy is complaining about a dtype
(float64) that it silently promoted to, rather than the dtype that the
user provided, which generally seems like a bad idea. Could Numpy
somehow complain about the original dtypes in this case? Or at least
give a warning about the first promotion (e.g. loss of precision)?

Peter

Loading...