Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is float64 cast to int when multiplying with a list?

Tags:

python

numpy

When multiplying a numpy float with a list, the float is automatically cast to int

>>> import numpy as np
>>> a = [1, 2, 3]
>>> np.float64(2.0) * a ### This behaves as 2 * a
[1, 2, 3, 1, 2, 3]

A normal float gives a TypeError

>>> 2.0 * a ### This does not
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'

However, the numpy float cannot be used for indexing

>>> a[np.float64(2.0)]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not numpy.float64

What is the logic behind this behaviour?

like image 704
wernere Avatar asked Dec 25 '14 15:12

wernere


1 Answers

You've hit up against a known bug in NumPy. The GitHub issue was closed last year, but the behavior remains in NumPy version 1.9.1.

like image 91
Andrew Avatar answered Oct 27 '22 18:10

Andrew