Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: prevent values in Pandas Series rounding to integer

I was trying to set some values on a Series, but it will automatically round to an integer, what should I do to prevent this?

from __future__ import division
import pandas as pd

In [100]: series = pd.Series(range(20))

In [101]: series[10]
Out[101]: 10

In [102]: series[10] = 0.05

In [103]: series[10]
Out[103]: 0

In [104]: series[10] = 2.5

In [105]: series[10]
Out[105]: 2

In [106]: series[10] = float(2.5)

In [107]: series[10]
Out[107]: 2

In [108]: float(2/3)
Out[108]: 0.6666666666666666

In [109]: series[10] = float(2/3)

In [110]: series[10]
Out[110]: 0
like image 952
Jim Avatar asked Jul 08 '15 12:07

Jim


1 Answers

series is automatically created with the int64 data type (since range(20) contains only integers). When you try to set a value of this Series to a float, the values is truncated to an integer because Pandas won't automatically promote the data type of the Series.*

The easiest way around this is to create you Series with the required datatype:

series = pd.Series(range(20), dtype=float)

Or you could cast the integer Series once it has been created:

series = series.astype(float)

You will then be able to set float values to the Series.


* This is somewhat similar to NumPy's behaviour with arrays. However, unlike NumPy, Pandas will promote the data type of a Series from integer to floating type if you try to set a nan value:

series[10] = np.nan # series is promoted to float64 type

Pandas will also promote the Series to an object datatype if you try set a string value:

series[5] = 'some string' # series is promoted to object type
like image 132
Alex Riley Avatar answered Nov 11 '22 11:11

Alex Riley