39 lines
1.7 KiB
Java
39 lines
1.7 KiB
Java
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var util_1 = require("@antv/util");
|
|
var simple_statistics_1 = require("simple-statistics");
|
|
var partition_1 = require("../util/partition");
|
|
var STATISTICS_METHODS = ['mean', 'median', 'max', 'min', 'value'];
|
|
function impute(rows, options) {
|
|
var field = options.field, groupBy = options.groupBy, method = options.method;
|
|
var newRows = util_1.deepMix([], rows);
|
|
if (!method || typeof method !== 'string') {
|
|
throw new TypeError('Invalid method!');
|
|
}
|
|
if (method === 'value' && !('value' in options)) {
|
|
throw new TypeError('Invalid value: it is nil.');
|
|
}
|
|
var column = newRows.map(function (item) { return item[field]; }).filter(function (value) { return value !== undefined; });
|
|
var groups = partition_1.partition(newRows, groupBy);
|
|
util_1.forIn(groups, function (group) {
|
|
var fieldValues = group.map(function (row) { return row[field]; }).filter(function (value) { return value !== undefined; });
|
|
if (fieldValues.length === 0) {
|
|
fieldValues = column;
|
|
}
|
|
var value = options.value;
|
|
if (method !== 'value') {
|
|
if (!STATISTICS_METHODS.includes(method)) {
|
|
throw new TypeError("Invalid method: must be a function or one of " + STATISTICS_METHODS.join(', '));
|
|
}
|
|
value = { min: simple_statistics_1.min, max: simple_statistics_1.max, mean: simple_statistics_1.mean, median: simple_statistics_1.median }[method](fieldValues);
|
|
}
|
|
group.forEach(function (row) {
|
|
if (row[field] === undefined) {
|
|
row[field] = value;
|
|
}
|
|
});
|
|
});
|
|
return newRows;
|
|
}
|
|
exports.impute = impute;
|