Count too high following where
evandavey opened this issue · 5 comments
Code (data-forge 1.8.12):
const dataForge = require('data-forge');
const data = [100, 100, null, null];
const series = new dataForge.Series(data).where((val) => !isNaN(val));
console.log(series.toArray(), `count=${series.count()}`, `average=${series.average()}`);
Output:
[ 100, 100 ] count=4 average=50
Expected Output
[ 100, 100 ] count=2 average=100
What am I missing?
Good catch!
It seems that nulls are being treated as zeros by count and average functions.
So it looks like your filter is not removing the nulls.
Do it like this:
const series = new dataForge.Series(data).where((val) => val !== null);
Arguably functions like average
should automatically remove undefined and null. I'll consider implementing that in the future.
Shouldn't that mean series.toArray()
should be [100, 100, null, null]
?
toArray
filters out null and undefined values.
average
and other mathematical functions should probably do this as well.
This issue has been fixed.
All the DF maths functions (sum, average, std, invert, min, max) now either strip or ignore nulls and undefined.
This has been published in Data-Forge v1.8.17.