First let’s skip the complexity of a float step, and use a simple integer start and stop:
In [141]: np.arange(0,5)
Out[141]: array([0, 1, 2, 3, 4])
In [142]: np.arange(0,5, dtype=int)
Out[142]: array([0, 1, 2, 3, 4])
In [143]: np.arange(0,5, dtype=float)
Out[143]: array([0., 1., 2., 3., 4.])
In [144]: np.arange(0,5, dtype=complex)
Out[144]: array([0.+0.j, 1.+0.j, 2.+0.j, 3.+0.j, 4.+0.j])
In [145]: np.arange(0,5, dtype="datetime64[D]")
Out[145]:
array(['1970-01-01', '1970-01-02', '1970-01-03', '1970-01-04',
'1970-01-05'], dtype="datetime64[D]")
Even bool
work, within a certain range:
In [149]: np.arange(0,1, dtype=bool)
Out[149]: array([False])
In [150]: np.arange(0,2, dtype=bool)
Out[150]: array([False, True])
In [151]: np.arange(0,3, dtype=bool)
ValueError: no fill-function for data-type.
In [156]: np.arange(0,3).astype(bool)
Out[156]: array([False, True, True])
There are 2 possible boolean values, so asking for more should produce some sort of error.
arange
is compiled code, so we can’t readily examine its logic (but you are welcome to search for the C code on github).
The examples show that it does, in some sense convert the parameters into the corresponding dtype
, and perform an iteration on that. It doesn’t simply generate the range and convert to dtype at the end.
1
solved python numpy arange dtpye? why converting to integer was zero