.sum on an empty matching set results in NaN
See original GitHub issueWhat you are doing?
I’m trying to use the SUM aggregate on an empty matching set
//imagine we have Payments table empty yet
Payment.sum('amount').then(console.log)
//logs NaN
What do you expect to happen?
I’d like the value in this case to be 0
since by definition, a sum is a number, not a NaN.
Why this happens?
I’ve quickly found that this happens due to this:
//node_modules/sequelize/lib/query-interface.js:702
//result contains null as we don't have any matching records
result = parseFloat(result);
What could be done?
since the dataType requested is dataType instanceof DataTypes.DECIMAL || dataType instanceof DataTypes.FLOAT
as stated in node_modules/sequelize/lib/query-interface.js:701
, we can additionally check whether the result
variable contains null
and in this case return 0
. I can submit a PR with this fix easily though I’d first like to ensure that I’m not the only one expecting 0
in this case rather than NaN
.
__Dialect: postgres __Database version: 9.5.3 __Sequelize version: 3.19.3
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
How to sum with missing values in Pandas? - Stack Overflow
In my case the Series comes from value_counts() over several columns and I wanted to use sum() but it gives me NaN for...
Read more >Working with missing data — pandas 1.5.2 documentation
Because NaN is a float, a column of integers with even one missing values is cast to ... The sum of an empty...
Read more >The Pandas .notnull Method: The Definitive Guide [+ Examples]
.sum() adds the totals held in each row of the Series. The result is below. Cells not missing data showing 111,295 printed to...
Read more >Working with missing values in Pandas - Towards Data Science
And you should be aware that regardless of the operations, the result of arithmetic with NaN will be another NaN . For example...
Read more >Python's sum(): The Pythonic Way to Sum Values
Because the recursive case uses a shorter sequence on each iteration, you expect to run into the base case when numbers is a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is silly IMHO.
sum
should returnnull
or 0. NotNaN
.Is it fixed yet? I just had a same problem in
4.36.1