Pandas series giving incorrect sum [duplicate]












0
















This question already has an answer here:




  • Is floating point math broken?

    28 answers




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









share|improve this question













marked as duplicate by timgeb python
Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 '18 at 18:02


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.























    0
















    This question already has an answer here:




    • Is floating point math broken?

      28 answers




    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









    share|improve this question













    marked as duplicate by timgeb python
    Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 20 '18 at 18:02


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      0












      0








      0









      This question already has an answer here:




      • Is floating point math broken?

        28 answers




      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









      share|improve this question















      This question already has an answer here:




      • Is floating point math broken?

        28 answers




      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




      This question already has an answer here:




      • Is floating point math broken?

        28 answers








      python pandas series numpy-ndarray






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 18:00









      ManviManvi

      537321




      537321




      marked as duplicate by timgeb python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 20 '18 at 18:02


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by timgeb python
      Users with the  python badge can single-handedly close python questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 20 '18 at 18:02


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          3














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






          share|improve this answer



















          • 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.

            – Wieschie
            Nov 20 '18 at 18:10











          • Thanks so much Ian Quah and @Wieschie for your inputs.

            – Manvi
            Nov 20 '18 at 18:19


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














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






          share|improve this answer



















          • 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.

            – Wieschie
            Nov 20 '18 at 18:10











          • Thanks so much Ian Quah and @Wieschie for your inputs.

            – Manvi
            Nov 20 '18 at 18:19
















          3














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






          share|improve this answer



















          • 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.

            – Wieschie
            Nov 20 '18 at 18:10











          • Thanks so much Ian Quah and @Wieschie for your inputs.

            – Manvi
            Nov 20 '18 at 18:19














          3












          3








          3







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






          share|improve this answer













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







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 18:01









          Ian QuahIan Quah

          811816




          811816








          • 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.

            – Wieschie
            Nov 20 '18 at 18:10











          • Thanks so much Ian Quah and @Wieschie for your inputs.

            – Manvi
            Nov 20 '18 at 18:19














          • 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.

            – Wieschie
            Nov 20 '18 at 18:10











          • Thanks so much Ian Quah and @Wieschie for your inputs.

            – Manvi
            Nov 20 '18 at 18:19








          1




          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.

          – Wieschie
          Nov 20 '18 at 18:10





          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.

          – Wieschie
          Nov 20 '18 at 18:10













          Thanks so much Ian Quah and @Wieschie for your inputs.

          – Manvi
          Nov 20 '18 at 18:19





          Thanks so much Ian Quah and @Wieschie for your inputs.

          – Manvi
          Nov 20 '18 at 18:19





          Popular posts from this blog

          How to change which sound is reproduced for terminal bell?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

          Can I use Tabulator js library in my java Spring + Thymeleaf project?