Hello,
Currently, the built-in Python
round (which is different from
np.round) when called on a
np.float64 returns a
np.float64, due to its
__round__ method. A congruous statement is true for
np.float32. However, since Python 3, the default behavior of
round is to return a Python
int when it operates on a Python
float. This is a mismatch according to the
Liskov Substitution Principle, as both these types subclass Python’s
float. This has been brought up in
gh-15297. Here is the problem summed up in code:
>>> type(round(np.float64(5)))
<class 'numpy.float64'>
>>> type(round(np.float32(5)))
<class 'numpy.float32'>
>>> type(round(float(5)))
<class 'int'>
This problem manifests itself most prominently when trying to index into collections:
>>> np.arange(6)[round(float(5))]
5
>>> np.arange(6)[round(np.float64(5))]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
There still remains the question, do we return Python
ints or
np.int64s?
This was issue was discussed in the weekly triage meeting today, and the following plan of action was proposed:
Does anyone have any thoughts on the proposal?
Best regards,
Hameer Abbasi