question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.sum on an empty matching set results in NaN

See original GitHub issue

What 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:closed
  • Created 7 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
bminercommented, Sep 11, 2018

This is silly IMHO. sum should return null or 0. Not NaN.

6reactions
janudewanggacommented, Mar 14, 2018

Is it fixed yet? I just had a same problem in 4.36.1

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found