series automatically created with the int64 data int64 (since range(20) contains only integers). When you try to set the value of this series to float, the values ββare truncated to an integer because Pandas will not automatically advertise the Series data type. *
The easiest way is to create a series with the required data type:
series = pd.Series(range(20), dtype=float)
Or you can overlay an integer series after creating it:
series = series.astype(float)
Then you can set the float values ββin Series.
* This is somewhat similar to the behavior of NumPy with arrays. However, unlike NumPy, Pandas will promote the Series data type from integer to floating type if you try to set the nan value:
series[10] = np.nan # series is promoted to float64 type
Pandas will also push the series to the object type if you try to set a string value:
series[5] = 'some string' # series is promoted to object type
source share