Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate floating points imprecision in python

I am writing a program for which it is important to compare and rank values in a date series. However, I am running into problems with the imprecision of floats

I am pulling these data from my SQL server that are both supposed to be 1.6. However, they turn out to be slightly different (see below). Therefore, when I use dataframe.rank(), it doesn't treat these two dates as the same rank, but rather ranks 01/02/2004 above 02/01/2005.

Anyone have any idea how to deal with this so that these two would end up on the same rank?

modelInputData.loc['01/02/2004',('Level','inflationCore','EUR')]
Out[126]: 1.6000000000000003

modelInputData.loc['02/01/2005',('Level','inflationCore','EUR')]
Out[127]: 1.6000000000000001
like image 946
jjvandermade Avatar asked Feb 07 '23 08:02

jjvandermade


2 Answers

You can use pd.Series.round() on the columns with floats.

precision = 2
df['col'] = df['col'].round(decimals = precision)

See: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.round.html

like image 56
Nathan Clement Avatar answered Feb 12 '23 10:02

Nathan Clement


I would recommend you to do it as bankers do - use cents and integers instead of EUR/USD and float/decimal variables

either convert it to cents on the MySQL side or do it in pandas:

df['amount'] = round(df['amount']*100)

You'll have much less problems then

like image 38
MaxU - stop WAR against UA Avatar answered Feb 12 '23 10:02

MaxU - stop WAR against UA