I want to operate on numpy arrays to use their indexing, and I want to include the 0-dimensional case. Now I came across a strange situation, where a type conversion appears, if I don't use in-place multiplication:
In [1]: import numpy as np
In [2]: x = 1.*np.array(1.)
In [3]: y = np.array(1.)
In [4]: y *= 1.
In [5]: x
Out[5]: 1.0
In [6]: y
Out[6]: array(1.)
In [7]: type(x)
Out[7]: numpy.float64
In [8]: type(y)
Out[8]: numpy.ndarray
Why is the type of x different to y? I know, that the inplace operations are differently implemented and they don't create a copy of the array, but I don't get the point, why the type is changed, if I multiply a 0d-array with a float? It works for 1d-arrays:
In [1]: import numpy as np
In [2]: x = np.array(1.)
In [3]: y = np.array([1.])
In [4]: 1.*x
Out[4]: 1.0
In [5]: 1.*y
Out[5]: array([1.])
In [7]: type(1.*x)
Out[7]: numpy.float64
In [8]: type(1.*y)
Out[8]: numpy.ndarray
I think, that's strange... Now I run into the following problem, where I would have to treat 0d-array separately:
In [1]: import numpy as np
In [2]: x = np.array(1.)
In [3]: y = np.array(1.)*1.
In [4]: x[x>0]
Out[4]: array([1.])
In [5]: y[y>0]
Out[5]: array([1.])
In [6]: x[x>0] = 2.
In [7]: y[y>0] = 2.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-5f9c5b138fc0> in <module>()
----> 1 y[y>0] = 2.
TypeError: 'numpy.float64' object does not support item assignment