0

Why is this Pandas series giving sum = .99999999 where as answer is 1. In my program, I need to assert on 'sum is equal to 1'. And, assertion is failing even if condition is correct.

s = pd.Series([0.41,0.25,0.25,0.09])
print("Pandas version = " + pd.__version__)
print(s)
print(type(s))
print(type(s.values))
print(s.values.sum())

The output is:

Pandas version = 0.23.4
0    0.41
1    0.25
2    0.25
3    0.09
dtype: float64
<class 'pandas.core.series.Series'>
<class 'numpy.ndarray'>
0.9999999999999999
Manvi
  • 1,136
  • 2
  • 18
  • 41

1 Answers1

3

Use np.isclose to determine if two values are arbitrarily close. It's a remnant of how floats are stored in the machine

IanQ
  • 1,831
  • 5
  • 20
  • 29
  • 1
    To expand upon this, most built-in floating point types are not entirely precise. They cannot always exactly represent the result of a calculation. This means that you should never do comparisons without using an epsilon value (an acceptable error range) like np.isclose will do. If you must have results that add up exactly, you should be using something like [decimal](https://docs.python.org/3/library/decimal.html). – Wieschie Nov 20 '18 at 18:10
  • Thanks so much Ian Quah and @Wieschie for your inputs. – Manvi Nov 20 '18 at 18:19