UNPKG

danfojs

Version:

JavaScript library providing high performance, intuitive, and easy to use data structures for manipulating and processing structured data.

1,189 lines (1,187 loc) 99.3 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); /** * @license * Copyright 2022 JsData. All rights reserved. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ========================================================================== */ var dummy_encoder_1 = __importDefault(require("../transformers/encoders/dummy.encoder")); var mathjs_1 = require("mathjs"); var tensorflowlib_1 = __importDefault(require("../shared/tensorflowlib")); var defaults_1 = require("../shared/defaults"); var groupby_1 = __importDefault(require("../aggregators/groupby")); var errors_1 = __importDefault(require("../shared/errors")); var indexing_1 = require("./indexing"); var utils_1 = __importDefault(require("../shared/utils")); var generic_1 = __importDefault(require("./generic")); var table_1 = require("table"); var series_1 = __importDefault(require("./series")); var plotting_1 = require("../../danfojs-base/plotting"); var utils = new utils_1.default(); /** * Two-dimensional ndarray with axis labels. * The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index. * Operations between DataFrame (+, -, /, , *) align values based on their associated index values– they need not be the same length. * @param data 2D Array, JSON, Tensor, Block of data. * @param options.index Array of numeric or string names for subseting array. If not specified, indexes are auto generated. * @param options.columns Array of column names. If not specified, column names are auto generated. * @param options.dtypes Array of data types for each the column. If not specified, dtypes are/is inferred. * @param options.config General configuration object for extending or setting NDframe behavior. */ var DataFrame = /** @class */ (function (_super) { __extends(DataFrame, _super); function DataFrame(data, options) { if (options === void 0) { options = {}; } var _this = this; var index = options.index, columns = options.columns, dtypes = options.dtypes, config = options.config; _this = _super.call(this, { data: data, index: index, columns: columns, dtypes: dtypes, config: config, isSeries: false }) || this; _this.$setInternalColumnDataProperty(); return _this; } /** * Maps all column names to their corresponding data, and return them as Series objects. * This makes column subsetting works. E.g this can work ==> `df["col1"]` * @param column Optional, a single column name to map */ DataFrame.prototype.$setInternalColumnDataProperty = function (column) { var self = this; if (column && typeof column === "string") { Object.defineProperty(self, column, { get: function () { return self.$getColumnData(column); }, set: function (arr) { self.$setColumnData(column, arr); } }); } else { var columns = this.columns; var _loop_1 = function (i) { var column_1 = columns[i]; Object.defineProperty(this_1, column_1, { get: function () { return self.$getColumnData(column_1); }, set: function (arr) { self.$setColumnData(column_1, arr); } }); }; var this_1 = this; for (var i = 0; i < columns.length; i++) { _loop_1(i); } } }; /** * Returns the column data from the DataFrame by column name. * @param column column name to get the column data * @param returnSeries Whether to return the data in series format or not. Defaults to true */ DataFrame.prototype.$getColumnData = function (column, returnSeries) { if (returnSeries === void 0) { returnSeries = true; } var columnIndex = this.columns.indexOf(column); if (columnIndex == -1) { errors_1.default.throwColumnNotFoundError(this); } var dtypes = [this.$dtypes[columnIndex]]; var index = __spreadArray([], this.$index, true); var columns = [column]; var config = __assign({}, this.$config); if (this.$config.isLowMemoryMode) { var data = []; for (var i = 0; i < this.values.length; i++) { var row = this.values[i]; data.push(row[columnIndex]); } if (returnSeries) { return new series_1.default(data, { dtypes: dtypes, index: index, columns: columns, config: config }); } else { return data; } } else { var data = this.$dataIncolumnFormat[columnIndex]; if (returnSeries) { return new series_1.default(data, { dtypes: dtypes, index: index, columns: columns, config: config }); } else { return data; } } }; /** * Updates the internal column data via column name. * @param column The name of the column to update. * @param arr The new column data */ DataFrame.prototype.$setColumnData = function (column, arr) { var columnIndex = this.$columns.indexOf(column); if (columnIndex == -1) { throw new Error("ParamError: column " + column + " not found in " + this.$columns + ". If you need to add a new column, use the df.addColumn method. "); } var colunmValuesToAdd; if (arr instanceof series_1.default) { colunmValuesToAdd = arr.values; } else if (Array.isArray(arr)) { colunmValuesToAdd = arr; } else { throw new Error("ParamError: specified value not supported. It must either be an Array or a Series of the same length"); } if (colunmValuesToAdd.length !== this.shape[0]) { errors_1.default.throwColumnLengthError(this, colunmValuesToAdd.length); } if (this.$config.isLowMemoryMode) { //Update row ($data) array for (var i = 0; i < this.$data.length; i++) { this.$data[i][columnIndex] = colunmValuesToAdd[i]; } //Update the dtypes this.$dtypes[columnIndex] = utils.inferDtype(colunmValuesToAdd)[0]; } else { //Update row ($data) array for (var i = 0; i < this.values.length; i++) { this.$data[i][columnIndex] = colunmValuesToAdd[i]; } //Update column ($dataIncolumnFormat) array since it's available in object this.$dataIncolumnFormat[columnIndex] = arr; //Update the dtypes this.$dtypes[columnIndex] = utils.inferDtype(colunmValuesToAdd)[0]; } }; /** * Return data with missing values removed from a specified axis * @param axis 0 or 1. If 0, column-wise, if 1, row-wise */ DataFrame.prototype.$getDataByAxisWithMissingValuesRemoved = function (axis) { var oldValues = this.$getDataArraysByAxis(axis); var cleanValues = []; for (var i = 0; i < oldValues.length; i++) { var values = oldValues[i]; cleanValues.push(utils.removeMissingValuesFromArray(values)); } return cleanValues; }; /** * Return data aligned to the specified axis. Transposes the array if needed. * @param axis 0 or 1. If 0, column-wise, if 1, row-wise */ DataFrame.prototype.$getDataArraysByAxis = function (axis) { if (axis === 1) { return this.values; } else { var dfValues = void 0; if (this.config.isLowMemoryMode) { dfValues = utils.transposeArray(this.values); } else { dfValues = this.$dataIncolumnFormat; } return dfValues; } }; /* * checks if DataFrame is compactible for arithmetic operation * compatible Dataframe must have only numerical dtypes **/ DataFrame.prototype.$frameIsNotCompactibleForArithmeticOperation = function () { var dtypes = this.dtypes; var str = function (element) { return element == "string"; }; return dtypes.some(str); }; /** * Return Tensors in the right axis for math operations. * @param other DataFrame or Series or number or array * @param axis 0 or 1. If 0, column-wise, if 1, row-wise * */ DataFrame.prototype.$getTensorsForArithmeticOperationByAxis = function (other, axis) { if (typeof other === "number") { return [this.tensor, tensorflowlib_1.default.scalar(other)]; } else if (other instanceof DataFrame) { return [this.tensor, other.tensor]; } else if (other instanceof series_1.default) { if (axis === 0) { return [this.tensor, tensorflowlib_1.default.tensor2d(other.values, [other.shape[0], 1])]; } else { return [this.tensor, tensorflowlib_1.default.tensor2d(other.values, [other.shape[0], 1]).transpose()]; } } else if (Array.isArray(other)) { if (axis === 0) { return [this.tensor, tensorflowlib_1.default.tensor2d(other, [other.length, 1])]; } else { return [this.tensor, tensorflowlib_1.default.tensor2d(other, [other.length, 1]).transpose()]; } } else { throw new Error("ParamError: Invalid type for other parameter. other must be one of Series, DataFrame or number."); } }; /** * Returns the dtype for a given column name * @param column */ DataFrame.prototype.$getColumnDtype = function (column) { var columnIndex = this.columns.indexOf(column); if (columnIndex === -1) { throw Error("ColumnNameError: Column \"" + column + "\" does not exist"); } return this.dtypes[columnIndex]; }; DataFrame.prototype.$logicalOps = function (tensors, operation) { var newValues = []; switch (operation) { case 'gt': newValues = tensors[0].greater(tensors[1]).arraySync(); break; case 'lt': newValues = tensors[0].less(tensors[1]).arraySync(); break; case 'ge': newValues = tensors[0].greaterEqual(tensors[1]).arraySync(); break; case 'le': newValues = tensors[0].lessEqual(tensors[1]).arraySync(); break; case 'eq': newValues = tensors[0].equal(tensors[1]).arraySync(); break; case 'ne': newValues = tensors[0].notEqual(tensors[1]).arraySync(); break; } var newData = utils.mapIntegersToBooleans(newValues, 2); return new DataFrame(newData, { index: __spreadArray([], this.index, true), columns: __spreadArray([], this.columns, true), dtypes: __spreadArray([], this.dtypes, true), config: __assign({}, this.config) }); }; DataFrame.prototype.$MathOps = function (tensors, operation, inplace) { var tensorResult; switch (operation) { case 'add': tensorResult = tensors[0].add(tensors[1]); break; case 'sub': tensorResult = tensors[0].sub(tensors[1]); break; case 'pow': tensorResult = tensors[0].pow(tensors[1]); break; case 'div': tensorResult = tensors[0].div(tensors[1]); break; case 'divNoNan': tensorResult = tensors[0].divNoNan(tensors[1]); break; case 'mul': tensorResult = tensors[0].mul(tensors[1]); break; case 'mod': tensorResult = tensors[0].mod(tensors[1]); break; } if (inplace) { var newData = tensorResult === null || tensorResult === void 0 ? void 0 : tensorResult.arraySync(); this.$setValues(newData); } else { return new DataFrame(tensorResult, { index: __spreadArray([], this.index, true), columns: __spreadArray([], this.columns, true), dtypes: __spreadArray([], this.dtypes, true), config: __assign({}, this.config) }); } }; /** * Purely integer-location based indexing for selection by position. * ``.iloc`` is primarily integer position based (from ``0`` to * ``length-1`` of the axis), but may also be used with a boolean array. * * @param rows Array of row indexes * @param columns Array of column indexes * * Allowed inputs are in rows and columns params are: * * - An array of single integer, e.g. ``[5]``. * - A list or array of integers, e.g. ``[4, 3, 0]``. * - A slice array string with ints, e.g. ``["1:7"]``. * - A boolean array. * - A ``callable`` function with one argument (the calling Series or * DataFrame) and that returns valid output for indexing (one of the above). * This is useful in method chains, when you don't have a reference to the * calling object, but would like to base your selection on some value. * * ``.iloc`` will raise ``IndexError`` if a requested indexer is * out-of-bounds. * * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B'] }) * const df2 = df.iloc({ rows: [1], columns: ["A"] }) * ``` */ DataFrame.prototype.iloc = function (_a) { var rows = _a.rows, columns = _a.columns; return (0, indexing_1._iloc)({ ndFrame: this, rows: rows, columns: columns }); }; /** * Access a group of rows and columns by label(s) or a boolean array. * ``loc`` is primarily label based, but may also be used with a boolean array. * * @param rows Array of row indexes * @param columns Array of column indexes * * Allowed inputs are: * * - A single label, e.g. ``["5"]`` or ``['a']``, (note that ``5`` is interpreted as a * *label* of the index, and **never** as an integer position along the index). * * - A list or array of labels, e.g. ``['a', 'b', 'c']``. * * - A slice object with labels, e.g. ``["a:f"]``. Note that start and the stop are included * * - A boolean array of the same length as the axis being sliced, * e.g. ``[True, False, True]``. * * - A ``callable`` function with one argument (the calling Series or * DataFrame) and that returns valid output for indexing (one of the above) * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B'] }) * const df2 = df.loc({ rows: [1], columns: ["A"] }) * ``` */ DataFrame.prototype.loc = function (_a) { var rows = _a.rows, columns = _a.columns; return (0, indexing_1._loc)({ ndFrame: this, rows: rows, columns: columns }); }; /** * Prints DataFrame to console as a formatted grid of row and columns. */ DataFrame.prototype.toString = function () { var maxRow = this.config.getMaxRow; var maxColToDisplayInConsole = this.config.getTableMaxColInConsole; // let data; var dataArr = []; var colLen = this.columns.length; var header = []; if (colLen > maxColToDisplayInConsole) { //truncate displayed columns to fit in the console var firstFourcolNames = this.columns.slice(0, 4); var lastThreecolNames = this.columns.slice(colLen - 3); //join columns with truncate ellipse in the middle header = __spreadArray(__spreadArray(__spreadArray([""], firstFourcolNames, true), ["..."], false), lastThreecolNames, true); var subIdx = void 0; var firstHalfValues = void 0; var lastHalfValueS = void 0; if (this.values.length > maxRow) { //slice Object to show [max_rows] var dfSubset1 = this.iloc({ rows: ["0:" + maxRow], columns: ["0:4"] }); var dfSubset2 = this.iloc({ rows: ["0:" + maxRow], columns: [colLen - 3 + ":"] }); subIdx = this.index.slice(0, maxRow); firstHalfValues = dfSubset1.values; lastHalfValueS = dfSubset2.values; } else { var dfSubset1 = this.iloc({ columns: ["0:4"] }); var dfSubset2 = this.iloc({ columns: [colLen - 3 + ":"] }); subIdx = this.index.slice(0, maxRow); firstHalfValues = dfSubset1.values; lastHalfValueS = dfSubset2.values; } // merge subset for (var i = 0; i < subIdx.length; i++) { var idx = subIdx[i]; var row = __spreadArray(__spreadArray(__spreadArray([idx], firstHalfValues[i], true), ["..."], false), lastHalfValueS[i], true); dataArr.push(row); } } else { //display all columns header = __spreadArray([""], this.columns, true); var subIdx = void 0; var values = void 0; if (this.values.length > maxRow) { //slice Object to show a max of [max_rows] var data = this.iloc({ rows: ["0:" + maxRow] }); subIdx = data.index; values = data.values; } else { values = this.values; subIdx = this.index; } // merge subset for (var i = 0; i < subIdx.length; i++) { var idx = subIdx[i]; var row = __spreadArray([idx], values[i], true); dataArr.push(row); } } var columnsConfig = {}; columnsConfig[0] = { width: 10 }; //set column width for index column for (var index = 1; index < header.length; index++) { columnsConfig[index] = { width: 17, truncate: 16 }; } var tableData = __spreadArray([header], dataArr, true); //Adds the column names to values before printing return (0, table_1.table)(tableData, __assign({ columns: columnsConfig }, this.config.getTableDisplayConfig)); }; /** * Returns the first n values in a DataFrame * @param rows The number of rows to return * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * const df2 = df.head(1) * ``` */ DataFrame.prototype.head = function (rows) { if (rows === void 0) { rows = 5; } if (rows <= 0) { throw new Error("ParamError: Number of rows cannot be less than 1"); } if (this.shape[0] <= rows) { return this.copy(); } if (this.shape[0] - rows < 0) { throw new Error("ParamError: Number of rows cannot be greater than available rows in data"); } return this.iloc({ rows: ["0:" + rows] }); }; /** * Returns the last n values in a DataFrame * @param rows The number of rows to return * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * const df2 = df.tail(1) * ``` */ DataFrame.prototype.tail = function (rows) { if (rows === void 0) { rows = 5; } if (rows <= 0) { throw new Error("ParamError: Number of rows cannot be less than 1"); } if (this.shape[0] <= rows) { return this.copy(); } if (this.shape[0] - rows < 0) { throw new Error("ParamError: Number of rows cannot be greater than available rows in data"); } rows = this.shape[0] - rows; return this.iloc({ rows: [rows + ":"] }); }; /** * Gets n number of random rows in a dataframe. Sample is reproducible if seed is provided. * @param num The number of rows to return. Default to 5. * @param options.seed An integer specifying the random seed that will be used to create the distribution. * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B'] }) * const df2 = await df.sample(1) * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B'] }) * const df2 = await df.sample(1, { seed: 1 }) * ``` */ DataFrame.prototype.sample = function (num, options) { if (num === void 0) { num = 5; } return __awaiter(this, void 0, void 0, function () { var seed, shuffledIndex, df; return __generator(this, function (_a) { switch (_a.label) { case 0: seed = __assign({ seed: 1 }, options).seed; if (num > this.shape[0]) { throw new Error("ParamError: Sample size cannot be bigger than number of rows"); } if (num <= 0) { throw new Error("ParamError: Sample size cannot be less than 1"); } return [4 /*yield*/, tensorflowlib_1.default.data.array(this.index).shuffle(num, "" + seed).take(num).toArray()]; case 1: shuffledIndex = _a.sent(); df = this.iloc({ rows: shuffledIndex }); return [2 /*return*/, df]; } }); }); }; DataFrame.prototype.add = function (other, options) { var _a = __assign({ inplace: false, axis: 1 }, options), inplace = _a.inplace, axis = _a.axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: add operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$MathOps(tensors, "add", inplace); }; DataFrame.prototype.sub = function (other, options) { var _a = __assign({ inplace: false, axis: 1 }, options), inplace = _a.inplace, axis = _a.axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: sub operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$MathOps(tensors, "sub", inplace); }; DataFrame.prototype.mul = function (other, options) { var _a = __assign({ inplace: false, axis: 1 }, options), inplace = _a.inplace, axis = _a.axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: mul operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$MathOps(tensors, "mul", inplace); }; DataFrame.prototype.div = function (other, options) { var _a = __assign({ inplace: false, axis: 1 }, options), inplace = _a.inplace, axis = _a.axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: div operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$MathOps(tensors, "div", inplace); }; DataFrame.prototype.divNoNan = function (other, options) { var _a = __assign({ inplace: false, axis: 1 }, options), inplace = _a.inplace, axis = _a.axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: div operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$MathOps(tensors, "divNoNan", inplace); }; DataFrame.prototype.pow = function (other, options) { var _a = __assign({ inplace: false, axis: 1 }, options), inplace = _a.inplace, axis = _a.axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: pow operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$MathOps(tensors, "pow", inplace); }; DataFrame.prototype.mod = function (other, options) { var _a = __assign({ inplace: false, axis: 1 }, options), inplace = _a.inplace, axis = _a.axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: mod operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$MathOps(tensors, "mod", inplace); }; /** * Return mean of DataFrame across specified axis. * @param options.axis 0 or 1. If 0, compute the mean column-wise, if 1, row-wise. Defaults to 1 * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B'] }) * df.mean().print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B'] }) * df.mean({ axis: 0 }).print() * ``` */ DataFrame.prototype.mean = function (options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: mean operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var newData = this.$getDataByAxisWithMissingValuesRemoved(axis); var resultArr = newData.map(function (arr) { return arr.reduce(function (a, b) { return a + b; }, 0) / arr.length; }); if (axis === 0) { return new series_1.default(resultArr, { index: this.columns }); } else { return new series_1.default(resultArr, { index: this.index }); } }; /** * Return median of DataFrame across specified axis. * @param options.axis 0 or 1. If 0, compute the median column-wise, if 1, row-wise. Defaults to 1 * @example * ``` * const df = new DataFrame([[1, 2, 4], [3, 4, 5], [6, 7, 8]], { columns: ['A', 'B', 'C'] }); * df.median().print() * ``` */ DataFrame.prototype.median = function (options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: median operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var newData = this.$getDataByAxisWithMissingValuesRemoved(axis); var resultArr = newData.map(function (arr) { return (0, mathjs_1.median)(arr); }); if (axis === 0) { return new series_1.default(resultArr, { index: this.columns }); } else { return new series_1.default(resultArr, { index: this.index }); } }; /** * Return mode of DataFrame across specified axis. * @param options.axis 0 or 1. If 0, compute the mode column-wise, if 1, row-wise. Defaults to 1 * @param options.keep If there are more than one modes, returns the mode at position [keep]. Defaults to 0 * @example * ``` * const df = new DataFrame([[1, 2, 4], [3, 4, 5], [6, 7, 8]], { columns: ['A', 'B', 'C'] }); * df.mode().print() * ``` * @example * ``` * const df = new DataFrame([[1, 2, 4], [3, 4, 5], [6, 7, 8]], { columns: ['A', 'B', 'C'] }); * df.mode({ keep: 1 }).print() * ``` */ DataFrame.prototype.mode = function (options) { var _a = __assign({ axis: 1, keep: 0 }, options), axis = _a.axis, keep = _a.keep; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: mode operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var newData = this.$getDataByAxisWithMissingValuesRemoved(axis); var resultArr = newData.map(function (arr) { var tempMode = (0, mathjs_1.mode)(arr); if (tempMode.length === 1) { return tempMode[0]; } else { return tempMode[keep]; } }); if (axis === 0) { return new series_1.default(resultArr, { index: this.columns }); } else { return new series_1.default(resultArr, { index: this.index }); } }; /** * Return minimum of values in a DataFrame across specified axis. * @param options.axis 0 or 1. If 0, compute the minimum value column-wise, if 1, row-wise. Defaults to 1 * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.min().print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.min({ axis: 0 }).print() * ``` */ DataFrame.prototype.min = function (options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: min operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var newData = this.$getDataByAxisWithMissingValuesRemoved(axis); var resultArr = newData.map(function (arr) { var smallestValue = arr[0]; for (var i = 0; i < arr.length; i++) { smallestValue = smallestValue < arr[i] ? smallestValue : arr[i]; } return smallestValue; }); if (axis === 0) { return new series_1.default(resultArr, { index: this.columns }); } else { return new series_1.default(resultArr, { index: this.index }); } }; /** * Return maximum of values in a DataFrame across specified axis. * @param options.axis 0 or 1. If 0, compute the maximum column-wise, if 1, row-wise. Defaults to 1 * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.max().print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.max({ axis: 0 }).print() * ``` */ DataFrame.prototype.max = function (options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: max operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var newData = this.$getDataByAxisWithMissingValuesRemoved(axis); var resultArr = newData.map(function (arr) { var biggestValue = arr[0]; for (var i = 0; i < arr.length; i++) { biggestValue = biggestValue > arr[i] ? biggestValue : arr[i]; } return biggestValue; }); if (axis === 0) { return new series_1.default(resultArr, { index: this.columns }); } else { return new series_1.default(resultArr, { index: this.index }); } }; /** * Return standard deviation of values in a DataFrame across specified axis. * @param options.axis 0 or 1. If 0, compute the standard deviation column-wise, if 1, row-wise. Defaults to 1 * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.std().print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.std({ axis: 0 }).print() * ``` */ DataFrame.prototype.std = function (options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: std operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var newData = this.$getDataByAxisWithMissingValuesRemoved(axis); var resultArr = newData.map(function (arr) { return (0, mathjs_1.std)(arr); }); if (axis === 0) { return new series_1.default(resultArr, { index: this.columns }); } else { return new series_1.default(resultArr, { index: this.index }); } }; /** * Return variance of values in a DataFrame across specified axis. * @param options.axis 0 or 1. If 0, compute the variance column-wise, if 1, add row-wise. Defaults to 1 * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.var().print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.var({ axis: 0 }).print() * ``` */ DataFrame.prototype.var = function (options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: var operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var newData = this.$getDataByAxisWithMissingValuesRemoved(axis); var resultArr = newData.map(function (arr) { return (0, mathjs_1.variance)(arr); }); if (axis === 0) { return new series_1.default(resultArr, { index: this.columns }); } else { return new series_1.default(resultArr, { index: this.index }); } }; /** * Get Less than of dataframe and other, element-wise (binary operator lt). * @param other DataFrame, Series, Array or Scalar number to compare with * @param options.axis 0 or 1. If 0, add column-wise, if 1, add row-wise * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.lt(2).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.lt([2, 3], { axis: 0 }).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * const sf = new Series([2, 3]) * df.lt(sf, { axis: 1 }).print() * ``` */ DataFrame.prototype.lt = function (other, options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: lt operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$logicalOps(tensors, "lt"); }; /** * Returns "greater than" of dataframe and other. * @param other DataFrame, Series, Array or Scalar number to compare with * @param options.axis 0 or 1. If 0, add column-wise, if 1, add row-wise * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.gt(2).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.gt([2, 3], { axis: 0 }).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * const sf = new Series([2, 3]) * df.gt(sf, { axis: 1 }).print() * ``` */ DataFrame.prototype.gt = function (other, options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: gt operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$logicalOps(tensors, "gt"); }; /** * Returns "equals to" of dataframe and other. * @param other DataFrame, Series, Array or Scalar number to compare with * @param options.axis 0 or 1. If 0, add column-wise, if 1, add row-wise * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.eq(2).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.eq([2, 3], { axis: 0 }).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * const sf = new Series([2, 3]) * df.eq(sf, { axis: 1 }).print() * ``` */ DataFrame.prototype.eq = function (other, options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: eq operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$logicalOps(tensors, "eq"); }; /** * Returns "not equal to" of dataframe and other. * @param other DataFrame, Series, Array or Scalar number to compare with * @param options.axis 0 or 1. If 0, add column-wise, if 1, add row-wise * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.ne(2).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.ne([2, 3], { axis: 0 }).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * const sf = new Series([2, 3]) * df.ne(sf, { axis: 1 }).print() * ``` */ DataFrame.prototype.ne = function (other, options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: ne operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$logicalOps(tensors, "ne"); }; /** * Returns "less than or equal to" of dataframe and other. * @param other DataFrame, Series, Array or Scalar number to compare with * @param options.axis 0 or 1. If 0, add column-wise, if 1, add row-wise * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.le(2).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.le([2, 3], { axis: 0 }).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * const sf = new Series([2, 3]) * df.le(sf, { axis: 1 }).print() * ``` */ DataFrame.prototype.le = function (other, options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: le operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$logicalOps(tensors, "le"); }; /** * Returns "greater than or equal to" between dataframe and other. * @param other DataFrame, Series, Array or Scalar number to compare with * @param options.axis 0 or 1. If 0, add column-wise, if 1, add row-wise * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.ge(2).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.ge([2, 3], { axis: 0 }).print() * ``` * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * const sf = new Series([2, 3]) * df.ge(sf, { axis: 1 }).print() * ``` */ DataFrame.prototype.ge = function (other, options) { var axis = __assign({ axis: 1 }, options).axis; if (this.$frameIsNotCompactibleForArithmeticOperation()) { throw Error("TypeError: ge operation is not supported for string dtypes"); } if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis); return this.$logicalOps(tensors, "ge"); }; /** * Return number of non-null elements in a Series * @param options.axis 0 or 1. If 0, count column-wise, if 1, add row-wise. Defaults to 1 * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.count().print() * ``` * * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.count({ axis: 0 }).print() * ``` */ DataFrame.prototype.count = function (options) { var axis = __assign({ axis: 1 }, options).axis; if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var newData = this.$getDataByAxisWithMissingValuesRemoved(axis); var resultArr = newData.map(function (arr) { return arr.length; }); if (axis === 0) { return new series_1.default(resultArr, { index: this.columns }); } else { return new series_1.default(resultArr, { index: this.index }); } }; /** * Return the sum of values across an axis. * @param options.axis 0 or 1. If 0, count column-wise, if 1, add row-wise. Defaults to 1 * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.sum().print() * ``` * * @example * ``` * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']}) * df.sum({ axis: 0 }).print() * ``` */ DataFrame.prototype.sum = function (options) { var axis = __assign({ axis: 1 }, options).axis; if ([0, 1].indexOf(axis) === -1) { throw Error("ParamError: Axis must be 0 or 1"); } var result = this.$getDataByAxisWithMissingValuesRemoved(axis); var sumArr = result.map(function (innerArr) { return innerArr.reduce(function (a, b) { return Number(a) + Number(b); }, 0); }); if (axis === 0) { return new series_1.default(sumArr, { index: __spreadArray([], this.columns, true) }); } else { return new series_1.default(sumArr, { index: __spreadArray([], this.index, true) });