Pandas series giving incorrect sum [duplicate]
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
python pandas series numpy-ndarray
marked as duplicate by timgeb
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.
add a comment |
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
python pandas series numpy-ndarray
marked as duplicate by timgeb
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.
add a comment |
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
python pandas series numpy-ndarray
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
python pandas series numpy-ndarray
asked Nov 20 '18 at 18:00
ManviManvi
537321
537321
marked as duplicate by timgeb
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
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Use np.isclose to determine if two values are arbitrarily close. It's a remnant of how floats are stored in the machine
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use np.isclose to determine if two values are arbitrarily close. It's a remnant of how floats are stored in the machine
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
add a comment |
Use np.isclose to determine if two values are arbitrarily close. It's a remnant of how floats are stored in the machine
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
add a comment |
Use np.isclose to determine if two values are arbitrarily close. It's a remnant of how floats are stored in the machine
Use np.isclose to determine if two values are arbitrarily close. It's a remnant of how floats are stored in the machine
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
add a comment |
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
add a comment |