diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml
index 5c36c43b..c74850d4 100644
--- a/.github/workflows/node.js.yml
+++ b/.github/workflows/node.js.yml
@@ -16,7 +16,7 @@ jobs:
strategy:
matrix:
- node-version: [12.x, 14.x]
+ node-version: [18.x, 20.x]
steps:
- uses: actions/checkout@v2
diff --git a/.gitignore b/.gitignore
index 211887f2..1a4e73cc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,7 @@
.editorconfig
-node_modules
\ No newline at end of file
+node_modules
+test/fixtures/*
+test/samples/*
+*.xlsx
+
+testsss
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 00000000..42b1f616
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,212 @@
+# Contributing Guide
+
+### Contributing to danfojs
+
+**Table of contents:**
+
+* [TL;DR](#tldr)
+* [Where to start?](#where-to-start)
+* [Project Structure](#project-structure)
+* [Development Setup](#development-setup)
+ * [Prerequisites](#prerequisites)
+ * [Installation](#installation)
+ * [Building](#building)
+ * [Testing](#testing)
+* [Working with Git](#working-with-git)
+* [Documentation Guidelines](#documentation-guidelines)
+* [Making Changes](#making-changes)
+* [Creating Pull Requests](#creating-pull-requests)
+
+## TL;DR
+
+All contributions, bug reports, bug fixes, documentation improvements, enhancements, and ideas are welcome.
+
+Quick setup for experienced contributors:
+
+```bash
+git clone https://github.com/javascriptdata/danfojs.git
+cd danfojs
+yarn install
+yarn build
+yarn test
+```
+
+## Where to start?
+
+For first-time contributors:
+
+1. Look for issues labeled ["good first issue"](https://github.com/javascriptdata/danfojs/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22)
+2. Read through our [Documentation](https://danfo.jsdata.org/getting-started)
+
+
+## Project Structure
+
+The project is organized into three main packages:
+
+- **danfojs-base**: Core functionality shared between browser and Node.js versions
+- **danfojs-browser**: Browser-specific implementation
+- **danfojs-node**: Node.js-specific implementation
+
+Most new features should be added to **danfojs-base** unless they are environment-specific.
+
+## Development Setup
+
+### Prerequisites
+
+- Node.js (v16.x or later)
+- Yarn package manager
+- Git
+
+### Installation
+
+1. Fork the repository on GitHub
+2. Clone your fork locally:
+ ```bash
+ git clone https://github.com/YOUR_USERNAME/danfojs.git
+ cd danfojs
+ ```
+
+3. Install dependencies:
+ ```bash
+ yarn install
+ ```
+
+### Building
+
+Build all packages:
+```bash
+yarn build
+```
+
+Build specific package:
+```bash
+cd src/danfojs-browser
+yarn build
+```
+
+Watch mode for development:
+```bash
+yarn dev
+```
+
+### Testing
+
+Run all tests:
+```bash
+yarn test
+```
+
+Run specific test file:
+```bash
+yarn test tests/core/frame.test.js
+```
+
+Run tests matching a pattern:
+```bash
+yarn test -g "DataFrame.add"
+```
+
+Run tests in watch mode:
+```bash
+yarn test --watch
+```
+
+## Working with Git
+
+1. Create a new branch:
+ ```bash
+ git checkout -b feature/your-feature-name
+ ```
+
+2. Make your changes and commit:
+ ```bash
+ git add .
+ git commit -m "feat: add new feature"
+ ```
+
+ We follow [Conventional Commits](https://www.conventionalcommits.org/) for commit messages:
+ - `feat:` for new features
+ - `fix:` for bug fixes
+ - `docs:` for documentation changes
+ - `test:` for adding tests
+ - `refactor:` for code refactoring
+
+3. Push to your fork:
+ ```bash
+ git push origin feature/your-feature-name
+ ```
+
+## Documentation Guidelines
+
+Good documentation includes:
+
+1. JSDoc comments for all public methods
+2. Clear parameter descriptions
+3. Return value documentation
+4. Usage examples
+
+Example:
+```javascript
+/**
+ * Add two series of the same length
+ * @param {Series} series1 - First series to add
+ * @param {Series} series2 - Second series to add
+ * @returns {Series} New series containing the sum
+ *
+ * @example
+ * const s1 = new Series([1, 2, 3])
+ * const s2 = new Series([4, 5, 6])
+ * const result = add_series(s1, s2)
+ * // result: Series([5, 7, 9])
+ */
+function add_series(series1, series2) {
+ // Implementation
+}
+```
+
+For methods with multiple options, use an options object:
+
+```javascript
+/**
+ * Join two or more dataframes
+ * @param {Object} options - Join options
+ * @param {DataFrame[]} options.df_list - Array of DataFrames to join
+ * @param {number} options.axis - Join axis (0: index, 1: columns)
+ * @param {string} options.by_column - Column to join on
+ * @returns {DataFrame} Joined DataFrame
+ */
+function join_df(options) {
+ // Implementation
+}
+```
+
+## Making Changes
+
+1. Write tests for new functionality
+2. Ensure all tests pass
+3. Update documentation if needed
+4. Add an entry to CHANGELOG.md
+5. Run linter: `yarn lint`
+
+## Creating Pull Requests
+
+1. Push your changes to your fork
+2. Go to the [danfojs repository](https://github.com/javascriptdata/danfojs)
+3. Click "Pull Request"
+4. Fill out the PR template:
+ - Clear description of changes
+ - Link to related issue
+ - Screenshots/examples if relevant
+ - Checklist of completed items
+
+Your PR will be reviewed by maintainers. Address any feedback and update your PR accordingly.
+
+---
+
+## Need Help?
+
+
+- Check our [Documentation](https://danfo.jsdata.org)
+- Ask in GitHub Issues
+
+Thank you for contributing to danfojs! 🎉
diff --git a/README.md b/README.md
index 962bb15f..280c8724 100644
--- a/README.md
+++ b/README.md
@@ -46,142 +46,155 @@ easy and intuitive. It is heavily inspired by [Pandas](https://pandas.pydata.org
- Robust data preprocessing functions like [OneHotEncoders](https://danfo.jsdata.org/api-reference/general-functions/danfo.onehotencoder), [LabelEncoders](https://danfo.jsdata.org/api-reference/general-functions/danfo.labelencoder), and scalers like [StandardScaler](https://danfo.jsdata.org/api-reference/general-functions/danfo.standardscaler) and [MinMaxScaler](https://danfo.jsdata.org/api-reference/general-functions/danfo.minmaxscaler) are supported on DataFrame and Series
+## Installation
+There are three ways to install and use Danfo.js in your application
+* For Nodejs applications, you can install the [__danfojs-node__]() version via package managers like yarn and/or npm:
-To use Danfo.js via script tags, copy and paste the CDN below to the body of your HTML file
+```bash
+npm install danfojs-node
+
+or
+
+yarn add danfojs-node
+```
+For client-side applications built with frameworks like React, Vue, Next.js, etc, you can install the [__danfojs__]() version:
+
+```bash
+npm install danfojs
+
+or
+
+yarn add danfojs
+```
+
+For use directly in HTML files, you can add the latest script tag from [JsDelivr](https://www.jsdelivr.com/package/npm/danfojs) to your HTML file:
```html
-
+
```
See all available versions [here](https://www.jsdelivr.com/package/npm/danfojs)
-### Example Usage in the Browser
-
-> See the example below in [Code Sandbox](https://codepen.io/risingodegua/pen/bGwPGMG)
+### Quick Examples
+* [Danfojs with HTML and vanilla JavaScript on CodePen](https://codepen.io/risingodegua/pen/bGpwyYW)
+* [Danfojs with React on Code Sandbox](https://codesandbox.io/s/using-danfojs-in-react-dwpv54?file=/src/App.js)
+* [Danfojs on ObservableHq](https://observablehq.com/@risingodegua/using-danfojs-on-observablehq)
+* [Danfojs in Nodejs on Replit](https://replit.com/@RisingOdegua/Danfojs-in-Nodejs)
+### Example Usage in the Browser
```html
-
-
-
-
-
+
+
+
+
Document
-
-
-
+
+
-
-
-
+
+
```
Output in Browser:

-## How to install
-Danfo.js is hosted on NPM, and can installed via package managers like npm and yarn
-
-```sh
-npm install danfojs-node
-```
-
### Example usage in Nodejs
```javascript
+const dfd = require("danfojs-node");
-const dfd = require("danfojs-node")
-
-const file_url = "https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv"
-dfd.read_csv(file_url)
- .then(df => {
- //prints the first five columns
- df.head().print()
+const file_url =
+ "https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv";
+dfd
+ .readCSV(file_url)
+ .then((df) => {
+ //prints the first five columns
+ df.head().print();
- // Calculate descriptive statistics for all numerical columns
- df.describe().print()
+ // Calculate descriptive statistics for all numerical columns
+ df.describe().print();
- //prints the shape of the data
- console.log(df.shape);
+ //prints the shape of the data
+ console.log(df.shape);
- //prints all column names
- console.log(df.columns);
+ //prints all column names
+ console.log(df.columns);
- // //prints the inferred dtypes of each column
- df.ctypes.print()
+ // //prints the inferred dtypes of each column
+ df.ctypes.print();
- //selecting a column by subsetting
- df['Name'].print()
+ //selecting a column by subsetting
+ df["Name"].print();
- //drop columns by names
- cols_2_remove = ['Age', 'Pclass']
- df_drop = df.drop({ columns: cols_2_remove, axis: 1 })
- df_drop.print()
+ //drop columns by names
+ let cols_2_remove = ["Age", "Pclass"];
+ let df_drop = df.drop({ columns: cols_2_remove, axis: 1 });
+ df_drop.print();
+ //select columns by dtypes
+ let str_cols = df_drop.selectDtypes(["string"]);
+ let num_cols = df_drop.selectDtypes(["int32", "float32"]);
+ str_cols.print();
+ num_cols.print();
- //select columns by dtypes
- let str_cols = df_drop.select_dtypes(["string"])
- let num_cols = df_drop.select_dtypes(["int32", "float32"])
- str_cols.print()
- num_cols.print()
+ //add new column to Dataframe
+ let new_vals = df["Fare"].round(1);
+ df_drop.addColumn("fare_round", new_vals, { inplace: true });
+ df_drop.print();
- //add new column to Dataframe
+ df_drop["fare_round"].round(2).print(5);
- let new_vals = df['Fare'].round(1)
- df_drop.addColumn({ column: "fare_round", values: new_vals, inplace: true })
- df_drop.print()
+ //prints the number of occurence each value in the column
+ df_drop["Survived"].valueCounts().print();
- df_drop['fare_round'].round(2).print(5)
+ //print the last ten elementa of a DataFrame
+ df_drop.tail(10).print();
- //prints the number of occurence each value in the column
- df_drop['Survived'].value_counts().print()
-
- //print the last ten elementa of a DataFrame
- df_drop.tail(10).print()
-
- //prints the number of missing values in a DataFrame
- df_drop.isna().sum().print()
-
- }).catch(err => {
- console.log(err);
- })
+ //prints the number of missing values in a DataFrame
+ df_drop.isNa().sum().print();
+ })
+ .catch((err) => {
+ console.log(err);
+ });
```
Output in Node Console:

-
-> If you want to use Danfo in frontend frameworks like React/Vue, read this [guide](https://danfo.jsdata.org/examples/using-danfojs-in-react)
-
## Notebook support
-* You can use Danfo.js on Dnotebooks playground [here](https://playnotebook.jsdata.org/demo)
* VsCode nodejs notebook extension now supports Danfo.js. See guide [here](https://marketplace.visualstudio.com/items?itemName=donjayamanne.typescript-notebook)
+* ObservableHQ Notebooks. See example notebook [here](https://observablehq.com/@risingodegua/using-danfojs-on-observablehq)
#### [See the Official Getting Started Guide](https://danfo.jsdata.org/getting-started)
@@ -190,10 +203,7 @@ The official documentation can be found [here](https://danfo.jsdata.org)
## Danfo.js Official Book
-
-
-
-We recently published a book titled "Building Data Driven Applications with Danfo.js". Read more about it [here](https://danfo.jsdata.org/building-data-driven-applications-with-danfo.js-book)
+We published a book titled "Building Data Driven Applications with Danfo.js". Read more about it [here](https://danfo.jsdata.org/building-data-driven-applications-with-danfo.js-book)
## Discussion and Development
Development discussions take place [here](https://github.com/opensource9ja/danfojs/discussions).
diff --git a/danfojs-browser/src/core/concat.js b/danfojs-browser/src/core/concat.js
deleted file mode 100644
index 0d3d28ad..00000000
--- a/danfojs-browser/src/core/concat.js
+++ /dev/null
@@ -1,219 +0,0 @@
-import DataFrame from './frame';
-// import { utils } from "../shared/utils";
-import Series from './series';
-
-
-export class Concat {
- constructor(kwargs) {
-
-
- // check if keys exist in kwargs
- // utils.__in_object(kwargs, "df_list", "df_list not found: specify the list of dataframe");
- // utils.__in_object(kwargs, "axis", "axis not found: specify the axis");
-
- let df_list = null; //set the df_list to null
- let axis = null; // set axis to null
- let indexes = null;
-
- //check if df_list is an array
- if (Array.isArray(kwargs["df_list"])) {
-
- df_list = kwargs["df_list"];
- } else {
- throw new Error("df_list must be an Array of dataFrames/Series");
- }
-
- //check if axis is int and is either 0 or 1
- if (typeof kwargs["axis"] === "number") {
-
- if (kwargs["axis"] == 0 || kwargs["axis"] == 1) {
-
- axis = kwargs["axis"];
- } else {
- axis = 1;
- // throw new Error("Invalid axis: axis must be 0 or 1")
- }
-
- } else {
- throw new Error("axis must be a number");
- }
-
-
- let df_object = Object.assign({}, df_list); // convert the array to object
-
- if (axis == 1) {
-
- let columns = [];
- let duplicate_col_count = {};
- let max_length = 0;
- let a_key = Object.keys(df_object)[0];
- indexes = df_object[a_key].index;
- for (let key in df_object) {
-
- let column = df_object[key].columns;
- let length = df_object[key].values.length;
-
- if (length > max_length) {
- max_length = length;
- }
-
- for (let index in column) {
-
- let col_name = column[index];
- if (col_name in duplicate_col_count) {
-
- let count = duplicate_col_count[col_name];
- let name = `${col_name}_${count + 1}`;
-
- columns.push(name);
-
- duplicate_col_count[col_name] = count + 1;
- } else {
-
- columns.push(col_name);
- duplicate_col_count[col_name] = 1;
- }
- }
-
-
- }
-
- let data = new Array(max_length);
-
- for (let key in df_list) {
-
- let values = df_list[key].values;
-
- for (let index = 0; index < values.length; index++) {
-
- let val = values[index];
- if (typeof data[index] === "undefined") {
-
- if (Array.isArray(val)){
- data[index] = val;
- } else {
- data[index] = [ val ];
- }
-
- } else {
- if (Array.isArray(val)){
- data[index].push(...val);
- } else {
- data[index].push(val);
- }
-
- }
- }
-
- if (values.length < max_length) {
- let column_length = df_list[key].columns.length;
- let null_array = Array(column_length);
-
- for (let col = 0; col < column_length; col++) {
- null_array[col] = NaN;
- }
-
- if (typeof data[max_length - 1] === "undefined") {
- data[max_length - 1] = null_array;
- } else {
- data[max_length - 1].push(...null_array);
- }
- }
- }
-
- let df = new DataFrame(data, { columns: columns, index: indexes }); //convert to dataframe
- return df;
- } else {
- //concatenate base on axis 0
- let columns = [];
- let row_indexes = [];
- let col_i = 0;
- for (let key in df_list) {
- let column = df_list[key].columns;
- columns.push(...column);
- indexes = df_list[key].index;
- let r_index = indexes.map((val) => {
- return `${val}_row${col_i}`;
- });
- row_indexes.push(...r_index);
- col_i += 1;
- }
-
- let column_set = new Set(columns);
-
- columns = Array.from(column_set);
-
- let data = [];
-
- for (let key in df_list) {
-
- let value = df_list[key].values;
-
- // let col_length = value[0].length
-
- let df_columns = df_list[key].columns;
-
- let not_exist = [];
- for (let col_index in columns) {
- let col_name = columns[col_index];
-
- let is_index = df_columns.indexOf(col_name);
-
- if (is_index == -1) {
- not_exist.push(col_name);
- }
- }
- if (not_exist.length > 0) {
- for (let i = 0; i < value.length; i++) {
- let row_value = value[i];
-
- let new_arr = Array(columns.length);
- for (let j = 0; j < columns.length; j++) {
-
- let col_name = columns[j];
- if (not_exist.includes(col_name)) {
-
- new_arr[j] = NaN;
- } else {
- let index = df_columns.indexOf(col_name);
- if (Array.isArray(row_value)){
- new_arr[j] = row_value[index];
- } else {
- new_arr[j] = row_value;
- }
-
- }
-
- }
- data.push(new_arr);
- }
- } else {
- data.push(...value);
- }
-
- }
-
- if (Array.isArray(data[0])){
- let df = new DataFrame(data, { columns: columns, index: row_indexes });
- return df;
- } else {
- let sf = new Series(data, { index: row_indexes });
- return sf;
- }
-
-
- }
-
- }
-}
-
-
-/**
-* Concatenate pandas objects along a particular axis with optional set logic along the other axes.
-* @param {kwargs} {df_list: List of DataFrame to concatenate together axis: 0 for row axis and 1 for index axis
-* @returns {DataFrame}
-*/
-export const concat = (kwargs) => {
- let concat_sf = new Concat(kwargs);
- return concat_sf;
-};
diff --git a/danfojs-browser/src/core/date_range.js b/danfojs-browser/src/core/date_range.js
deleted file mode 100644
index b1805173..00000000
--- a/danfojs-browser/src/core/date_range.js
+++ /dev/null
@@ -1,289 +0,0 @@
-import { utils } from "../shared/utils";
-
-/**
- * Generate date range between a specified set of date
- * @param {kwargs} kwargs {
- * start : string
- * end : string
- * period: int
- * freq : string
- * }
- * @returns Array
- */
-export class date_range {
- constructor(kwargs){
-
- this.offset = null;
-
- if (utils.keyInObject(kwargs, "start")){
- this.start = kwargs["start"];
- } else {
- this.start = null;
- }
-
- if (utils.keyInObject(kwargs, "end")){
- this.end = kwargs["end"];
- } else {
- this.end = null;
- }
-
- if (utils.keyInObject(kwargs, "period")){
- this.period = kwargs["period"];
- } else {
- this.period = null;
- }
-
- if (utils.keyInObject(kwargs, "freq")){
- this.freq = kwargs["freq"];
- } else {
- this.freq = "D";
- }
-
- this.freq_list = [ "M", "D", "s", "H", "m", "Y" ];
-
- if (this.freq.length == 1){
- if (!this.freq_list.includes(this.freq)){
- throw new Error(`invalid freq ${this.freq}`);
- }
- } else {
- this.offset = parseInt(this.freq.slice(0, -1));
- if (!Number.isFinite(this.offset)){
- throw new Error(`invalid freq offset ${this.freq.slice(0, -1)}`);
- }
- this.freq = this.freq.slice(-1);
- if (!this.freq_list.includes(this.freq)){
- throw new Error(`invalid freq ${this.freq}`);
- }
- }
-
- let rslt = this.range(this.start, this.end, this.period, this.offset);
- return rslt;
-
- }
-
- range(start, end, period, offset = null){
-
- let start_date = null;
- let end_date = null;
- let start_range = null;
- let end_range = null;
- if (start && end){
- start_date = new Date(start);
- start_range = this.freq_type(start_date, this.freq);
- end_date = new Date(end);
- end_range = this.freq_type(end_date, this.freq);
-
- //check if the end year is greater than start year
- let start_year = start_date.getFullYear();
- let end_year = end_date.getFullYear();
- if ((start_year < end_year)){
- // end_range = start_range + end_range
- if (this.freq == "M"){
- end_range = this.month_end(start_date, end_date);
- } else if (this.freq == "D"){
- end_range = this.day_end(start_date, end_date) - start_range;
-
- }
- }
-
- let range_array = utils.range(start_range, end_range);
-
- if (offset){
- range_array = this.offset_count(range_array, offset);
- }
-
- if (this.freq == "M"){
- range_array = this.month_range(range_array);
- }
-
- let date_range = range_array.map((x) => {
- return this.set_dateProps(start_date, this.freq, x);
- });
- date_range[date_range.length - 1] = end_date;
-
- let date_string = this.toLocalString(date_range);
-
- return date_string;
- } else if (start && !(end)){
- start_date = new Date(start);
- start_range = this.freq_type(start_date, this.freq);
- end_range = offset ? ((period * offset) - 1) : period - 1;
-
- if (start_range > end_range){
- end_range = end_range + start_range;
- }
- let range_array = utils.range(start_range, end_range);
-
-
- if (offset){
- range_array = this.offset_count(range_array, offset);
- }
-
- let date_range = range_array.map((x) => {
- return this.set_dateProps(start_date, this.freq, x);
- });
-
- let date_string = this.toLocalString(date_range);
- return date_string;
-
- } else if (end && !(start)){
- end_date = new Date(end);
- end_range = this.freq_type(end_date, this.freq);
- start_range = (end_range - period) + 1;
-
- let range_array = utils.range(start_range, end_range);
-
- if (offset){
- range_array = this.offset_count(range_array, offset);
- }
-
- let date_range = range_array.map((x) => {
- return this.set_dateProps(end_date, this.freq, x);
- });
-
- let date_string = this.toLocalString(date_range);
- return date_string;
- }
- }
-
- freq_type(date, ftype){
-
- let rslt = null;
- switch (ftype){
-
- case "M":
- rslt = date.getMonth();
- break;
- case "Y":
- rslt = date.getFullYear();
- break;
- case "s":
- rslt = date.getSeconds();
- break;
- case "D":
- rslt = date.getDate();
- break;
- case "H":
- rslt = date.getHours();
- break;
- case "m":
- rslt = date.getMinutes();
- break;
- }
- return rslt;
- }
-
- offset_count(d_array, offset){
-
- let r_array = [];
-
- for (let i = 0; i < d_array.length; i += offset){
- r_array.push(d_array[i]);
- }
- return r_array;
- }
-
- set_dateProps(date, ftype, val){
-
- let new_date = new Date(date.valueOf());
- switch (ftype){
-
- case "M":
- if (val.length == 2){
-
- new_date.setYear(new_date.getFullYear() + val[0]);
- new_date.setMonth(parseInt(val[1]));
- } else {
- new_date.setMonth(val);
- }
-
- break;
- case "Y":
- new_date.setYear(val);
- break;
- case "s":
- new_date.setSeconds(val);
- break;
- case "D":
- new_date.setDate(val);
- break;
- case "H":
- new_date.setHours(val);
- break;
- case "m":
- new_date.setMinutes(val);
- break;
- }
- return new_date;
- }
-
- toLocalString(d_array){
-
- let r_array = d_array.map((x) => {
-
- return x.toLocaleString();
- });
-
- return r_array;
- }
-
- month_end(start_date, end_date){
-
- let end_month = end_date.getMonth();
-
- let diff_year = end_date.getFullYear() - start_date.getFullYear();
-
- let end_range = (12 * diff_year) + end_month;
-
- return end_range;
- }
-
- month_range(range){
-
- let minus = null;
- let y_val = 0;
- let d_range = range.map((x) => {
-
- if (x > 11){
- if (x % 12 == 0){
- minus = x;
- y_val = x / 12;
- return [ y_val, (x - minus) ];
- } else {
- return [ y_val, (x - minus) ];
- }
-
- }
- return [ y_val, x ];
- });
-
- return d_range;
- }
-
- day_end(start_date, end_date){
-
- let month_end = this.month_end(start_date, end_date);
- let range = utils.range(start_date.getMonth(), month_end);
- let m_range = this.month_range(range);
-
- // let s_date = new Date(start_date.getFullYear(),start_date.getMonth(),0)
- let sum = 0;
- for (let i = 0; i < m_range.length; i++){
-
- let val = m_range[i];
-
- let d_date = null;
- if (i === m_range.length - 1) {
- d_date = new Date(start_date.getFullYear() + val[0], val[1], end_date.getDate()).getDate();
- } else {
- d_date = new Date(start_date.getFullYear() + val[0], val[1], 0).getDate();
-
- }
-
- sum += d_date;
-
- }
- return sum;
-
- }
-}
diff --git a/danfojs-browser/src/core/frame.js b/danfojs-browser/src/core/frame.js
deleted file mode 100644
index 6b2f6321..00000000
--- a/danfojs-browser/src/core/frame.js
+++ /dev/null
@@ -1,2392 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { variance, std, median, mode, mean } from 'mathjs';
-import { DATA_TYPES } from '../shared/defaults';
-import { scalar, tensor2d, data as tfData } from '@tensorflow/tfjs';
-import { _genericMathOp } from "./math.ops";
-import ErrorThrower from "../shared/errors";
-import { _iloc, _loc } from "./indexing";
-import { utils } from "../shared/utils";
-import NDframe from "./generic";
-import { table } from "table";
-import Series from './series';
-import dummyEncode from "./get_dummies";
-import { GroupBy } from "./groupby";
-import { Plot } from "../plotting/plot";
-
-
-// const utils = new Utils();
-
-/**
- * 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.
- */
-/* @ts-ignore */ //COMMENT OUT WHEN METHODS HAVE BEEN IMPLEMENTED
-export default class DataFrame extends NDframe {
- constructor(data, options) {
- const { index, columns, dtypes, config } = { index: undefined, columns: undefined, dtypes: undefined, config: undefined, ...options };
- super({ data, index, columns, dtypes, config, isSeries: false });
- this.$setInternalColumnDataProperty();
- }
-
- /**
- * Maps all column names to their corresponding data, and return them objects.
- * This makes column subsetting works. E.g this can work ==> `df["col1"]`
- * @param column Optional, a single column name to map
- */
- $setInternalColumnDataProperty(column) {
- const self = this;
- if (column && typeof column === "string") {
- Object.defineProperty(self, column, {
- get() {
- return self.$getColumnData(column);
- },
- set(arr) {
- self.$setColumnData(column, arr);
- }
- });
- } else {
- const columns = this.columns;
- for (let i = 0; i < columns.length; i++) {
- const column = columns[i];
- Object.defineProperty(this, column, {
- get() {
- return self.$getColumnData(column);
- },
- set(arr) {
- self.$setColumnData(column, arr);
- }
- });
- }
- }
-
- }
-
- /**
- * 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
- */
- $getColumnData(column, returnSeries = true) {
- const columnIndex = this.columns.indexOf(column);
-
- if (columnIndex == -1) {
- ErrorThrower.throwColumnNotFoundError(this);
- }
-
- const dtypes = [this.$dtypes[columnIndex]];
- const index = [...this.$index];
- const columns = [column];
- const config = { ...this.$config };
-
- if (this.$config.isLowMemoryMode) {
- const data = [];
- for (let i = 0; i < this.values.length; i++) {
- const row = this.values[i];
- data.push(row[columnIndex]);
- }
- if (returnSeries) {
- return new Series(data, {
- dtypes,
- index,
- columns,
- config
- });
- } else {
- return data;
- }
-
- } else {
- const data = this.$dataIncolumnFormat[columnIndex];
- if (returnSeries) {
- return new Series(data, {
- dtypes,
- index,
- columns,
- 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
- */
- $setColumnData(column, arr) {
-
- const 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. `);
- }
-
- let colunmValuesToAdd;
-
- if (arr instanceof Series) {
- 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]) {
- ErrorThrower.throwColumnLengthError(this, colunmValuesToAdd.length);
- }
-
- if (this.$config.isLowMemoryMode) {
- //Update row ($data) array
- for (let 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 (let 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
- */
- $getDataByAxisWithMissingValuesRemoved(axis) {
- const oldValues = this.$getDataArraysByAxis(axis);
- const cleanValues = [];
- for (let i = 0; i < oldValues.length; i++) {
- const 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
- */
- $getDataArraysByAxis(axis) {
- axis = axis === 0 ? 1 : 0;
- if (axis === 1) {
- return this.values;
- } else {
- let dfValues;
- 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
- **/
- $frameIsNotCompactibleForArithmeticOperation() {
- const dtypes = this.dtypes;
- const str = (element) => 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
- * */
- $getTensorsForArithmeticOperationByAxis(
- other,
- axis
- ) {
- axis = axis === 0 ? 1 : 0;
- if (typeof other === "number") {
- return [this.tensor, scalar(other)];
- } else if (other instanceof DataFrame) {
- return [this.tensor, other.tensor];
- } else if (other instanceof Series) {
- if (axis === 1) {
- return [this.tensor, tensor2d(other.values, [other.shape[0], 1])];
- } else {
- return [this.tensor, tensor2d(other.values, [other.shape[0], 1]).transpose()];
- }
- } else if (Array.isArray(other)) {
- if (axis === 1) {
- return [this.tensor, tensor2d(other, [other.length, 1])];
- } else {
- return [this.tensor, 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
- */
- $getColumnDtype(column) {
- const columnIndex = this.columns.indexOf(column);
- if (columnIndex === -1) {
- throw Error(`ColumnNameError: Column "${column}" does not exist`);
- }
- return this.dtypes[columnIndex];
- }
-
- $logicalOps(tensors, operation) {
- let 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;
-
- }
-
- const newData = utils.mapIntegersToBooleans(newValues, 2);
- return new DataFrame(
- newData,
- {
- index: [...this.index],
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
-
- $MathOps(tensors, operation, inplace) {
- let 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 'mul':
- tensorResult = tensors[0].mul(tensors[1]);
- break;
- case 'mod':
- tensorResult = tensors[0].mod(tensors[1]);
- break;
- }
-
- if (inplace) {
- const newData = tensorResult.arraySync();
- this.$setValues(newData);
- } else {
- return new DataFrame(
- tensorResult,
- {
- index: [...this.index],
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...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.
- */
- iloc(args) {
- const { rows, columns } = { rows: undefined, columns: undefined, ...args };
- return _iloc({ ndFrame: this, rows, 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)
- */
- loc(args) {
- const { rows, columns } = args;
- return _loc({ ndFrame: this, rows, columns });
- }
-
- /**
- * Prints DataFrame to console as a formatted grid of row and columns.
- */
- toString() {
- const maxRow = this.config.getMaxRow;
- const maxColToDisplayInConsole = this.config.getTableMaxColInConsole;
-
- // let data;
- const dataArr = [];
- const colLen = this.columns.length;
-
- let header = [];
-
- if (colLen > maxColToDisplayInConsole) {
- //truncate displayed columns to fit in the console
- let firstFourcolNames = this.columns.slice(0, 4);
- let lastThreecolNames = this.columns.slice(colLen - 4);
- //join columns with truncate ellipse in the middle
- header = ["", ...firstFourcolNames, "...", ...lastThreecolNames];
-
- let subIdx;
- let firstHalfValues;
- let lastHalfValueS;
-
- if (this.values.length > maxRow) {
- //slice Object to show [max_rows]
- let dfSubset1 = this.iloc({
- rows: [`0:${maxRow}`],
- columns: ["0:4"]
- });
-
- let dfSubset2 = this.iloc({
- rows: [`0:${maxRow}`],
- columns: [`${colLen - 4}:`]
- });
-
- subIdx = this.index.slice(0, maxRow);
- firstHalfValues = dfSubset1.values;
- lastHalfValueS = dfSubset2.values;
-
- } else {
- let dfSubset1 = this.iloc({ columns: ["0:4"] });
- let dfSubset2 = this.iloc({ columns: [`${colLen - 4}:`] });
-
- subIdx = this.index.slice(0, maxRow);
- firstHalfValues = dfSubset1.values;
- lastHalfValueS = dfSubset2.values;
- }
-
- // merge subset
- for (let i = 0; i < subIdx.length; i++) {
- const idx = subIdx[i];
- const row = [idx, ...firstHalfValues[i], "...", ...lastHalfValueS[i]];
- dataArr.push(row);
- }
-
- } else {
- //display all columns
- header = ["", ...this.columns];
- let subIdx;
- let values;
- if (this.values.length > maxRow) {
- //slice Object to show a max of [max_rows]
- let data = this.iloc({ rows: [`0:${maxRow}`] });
- subIdx = data.index;
- values = data.values;
- } else {
- values = this.values;
- subIdx = this.index;
- }
-
- // merge subset
- for (let i = 0; i < subIdx.length; i++) {
- const idx = subIdx[i];
- const row = [idx, ...values[i]];
- dataArr.push(row);
- }
- }
-
-
- const columnsConfig = {};
- columnsConfig[0] = { width: 10 }; //set column width for index column
-
- for (let index = 1; index < header.length; index++) {
- columnsConfig[index] = { width: 17, truncate: 16 };
- }
-
- const tableData = [header, ...dataArr]; //Adds the column names to values before printing
-
- return table(tableData, { columns: columnsConfig, ...this.config.getTableDisplayConfig });
- }
-
- /**
- * Returns the first n values in a DataFrame
- * @param rows The number of rows to return
- */
- head(rows = 5) {
- if (rows > this.shape[0]) {
- throw new Error("ParamError of rows cannot be greater than available rows in data");
- }
- if (rows <= 0) {
- throw new Error("ParamError of rows cannot be less than 1");
- }
-
- return this.iloc({ rows: [`0:${rows}`] });
- }
-
- /**
- * Returns the last n values in a DataFrame
- * @param rows The number of rows to return
- */
- tail(rows = 5) {
- if (rows > this.shape[0]) {
- throw new Error("ParamError of rows cannot be greater than available rows in data");
- }
- if (rows <= 0) {
- throw new Error("ParamError of rows cannot be less than 1");
- }
- 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.
- */
- async sample(num = 5, options) {
- const { seed } = { seed: 1, ...options };
-
- 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");
- }
-
- const shuffledIndex = await tfData.array(this.index).shuffle(num, `${seed}`).take(num).toArray();
- const df = this.iloc({ rows: shuffledIndex });
- return df;
- }
-
- /**
- * Return Addition of DataFrame and other, element-wise (binary operator add).
- * @param other DataFrame, Series, Array or Scalar number to add
- * @param options.axis 0 or 1. If 0, add column-wise, if 1, add row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- add(other, options) {
- const { inplace, axis } = { inplace: false, axis: 1, ...options };
-
- 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");
- }
-
- const tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis);
- return this.$MathOps(tensors, "add", inplace);
- }
-
- /**
- * Return substraction between DataFrame and other.
- * @param other DataFrame, Series, Array or Scalar number to substract from DataFrame
- * @param options.axis 0 or 1. If 0, compute the subtraction column-wise, if 1, row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- sub(other, options) {
- const { inplace, axis } = { inplace: false, axis: 1, ...options };
-
- 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");
- }
-
- const tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis);
- return this.$MathOps(tensors, "sub", inplace);
-
-
- }
- /**
- * Return multiplciation between DataFrame and other.
- * @param other DataFrame, Series, Array or Scalar number to multiply with.
- * @param options.axis 0 or 1. If 0, compute the multiplication column-wise, if 1, row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- mul(other, options) {
- const { inplace, axis } = { inplace: false, axis: 1, ...options };
-
- 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");
- }
- const tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis);
- return this.$MathOps(tensors, "mul", inplace);
-
-
- }
-
- /**
- * Return division of DataFrame with other.
- * @param other DataFrame, Series, Array or Scalar number to divide with.
- * @param options.axis 0 or 1. If 0, compute the division column-wise, if 1, row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- div(other, options) {
- const { inplace, axis } = { inplace: false, axis: 1, ...options };
-
- 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");
- }
-
- const tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis);
- return this.$MathOps(tensors, "div", inplace);
-
-
- }
-
- /**
- * Return DataFrame raised to the power of other.
- * @param other DataFrame, Series, Array or Scalar number to to raise to.
- * @param options.axis 0 or 1. If 0, compute the power column-wise, if 1, row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- pow(other, options) {
- const { inplace, axis } = { inplace: false, axis: 1, ...options };
-
- 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");
- }
-
- const tensors = this.$getTensorsForArithmeticOperationByAxis(other, axis);
- return this.$MathOps(tensors, "pow", inplace);
-
-
- }
-
- /**
- * Return modulus between DataFrame and other.
- * @param other DataFrame, Series, Array or Scalar number to modulus with.
- * @param options.axis 0 or 1. If 0, compute the mod column-wise, if 1, row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- mod(other, options) {
- const { inplace, axis } = { inplace: false, axis: 1, ...options };
-
- 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");
- }
-
- const 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
- */
- mean(options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
- const meanArr = newData.map((arr) => arr.reduce((a, b) => a + b, 0) / arr.length);
- if (axis === 1) {
- return new Series(meanArr, { index: this.columns });
- } else {
- return new Series(meanArr, { 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
- */
- median(options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
- const medianArr = newData.map((arr) => median(arr));
- if (axis === 1) {
- return new Series(medianArr, { index: this.columns });
- } else {
- return new Series(medianArr, { 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
- */
- mode(options) {
- const { axis, keep } = { axis: 1, keep: 0, ...options };
-
- 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");
- }
-
- const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
- const modeArr = newData.map((arr) => {
- const tempMode = mode(arr);
- if (tempMode.length === 1) {
- return tempMode[0];
- } else {
- return tempMode[keep];
- }
- });
- if (axis === 1) {
- return new Series(modeArr, { index: this.columns });
- } else {
- return new Series(modeArr, { 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
- */
- min(options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
-
- const minArr = newData.map((arr) => {
- let smallestValue = arr[0];
- for (let i = 0; i < arr.length; i++) {
- smallestValue = smallestValue < arr[i] ? smallestValue : arr[i];
- }
- return smallestValue;
- });
-
- if (axis === 1) {
- return new Series(minArr, { index: this.columns });
- } else {
- return new Series(minArr, { 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
- */
- max(options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
-
- const maxArr = newData.map((arr) => {
- let biggestValue = arr[0];
- for (let i = 0; i < arr.length; i++) {
- biggestValue = biggestValue > arr[i] ? biggestValue : arr[i];
- }
- return biggestValue;
- });
-
- if (axis === 1) {
- return new Series(maxArr, { index: this.columns });
- } else {
- return new Series(maxArr, { 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
- */
- std(options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
- const stdArr = newData.map((arr) => std(arr));
-
- if (axis === 1) {
- return new Series(stdArr, { index: this.columns });
- } else {
- return new Series(stdArr, { 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
- */
- var(options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
- const varArr = newData.map((arr) => variance(arr));
- if (axis === 1) {
- return new Series(varArr, { index: this.columns });
- } else {
- return new Series(varArr, { 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
- */
- lt(other, options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const 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
- */
- gt(other, options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const 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
- */
- eq(other, options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const 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
- */
- ne(other, options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const 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
- */
- le(other, options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const 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
- */
- ge(other, options) {
- const { axis } = { axis: 1, ...options };
-
- 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");
- }
-
- const 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
- */
- count(options) {
- const { axis } = { axis: 1, ...options };
-
- if ([0, 1].indexOf(axis) === -1) {
- throw Error("ParamError: Axis must be 0 or 1");
- }
-
- const newData = this.$getDataByAxisWithMissingValuesRemoved(axis);
- const countArr = newData.map((arr) => arr.length);
- if (axis === 1) {
- return new Series(countArr, { index: this.columns });
- } else {
- return new Series(countArr, { 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
- */
- sum(options) {
- const { axis } = { axis: 1, ...options };
-
- if ([0, 1].indexOf(axis) === -1) {
- throw Error("ParamError: Axis must be 0 or 1");
- }
-
- const result = this.$getDataByAxisWithMissingValuesRemoved(axis);
- const sumArr = result.map((innerArr) => {
- return innerArr.reduce((a, b) => Number(a) + Number(b), 0);
- });
- if (axis === 1) {
- return new Series(sumArr, {
- index: [...this.columns]
- });
- } else {
- return new Series(sumArr, {
- index: [...this.index]
- });
- }
-
- }
-
- /**
- * Return the absolute value of elements in a DataFrame.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- abs(options) {
- const { inplace } = { inplace: false, ...options };
-
- const newData = (this.values).map((arr) => arr.map((val) => Math.abs(val)));
- if (inplace) {
- this.$setValues(newData);
- } else {
- return new DataFrame(newData, {
- index: [...this.index],
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
- }
-
- /**
- * Rounds all element in the DataFrame to specified number of decimal places.
- * @param dp Number of decimal places to round to. Defaults to 1
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- round(dp = 1, options) {
- const { inplace } = { inplace: false, ...options };
-
- if (this.$frameIsNotCompactibleForArithmeticOperation()) {
- throw Error("TypeError: round operation is not supported for string dtypes");
- }
-
- if (typeof dp !== "number") {
- throw Error("ParamError: dp must be a number");
- }
-
- const newData = utils.round(this.values, dp, false);
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- return new DataFrame(
- newData,
- {
- index: [...this.index],
- columns: [...this.columns],
- config: { ...this.config }
- });
- }
- }
-
- /**
- * Returns cumulative product accross specified axis.
- * @param options.axis 0 or 1. If 0, count column-wise, if 1, add row-wise. Defaults to 1
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- cumprod(options) {
- const { axis, inplace } = { axis: 1, inplace: false, ...options };
- return this.cumOps("prod", axis, inplace);
- }
-
- /**
- * Returns cumulative sum accross specified axis.
- * @param options.axis 0 or 1. If 0, count column-wise, if 1, add row-wise. Defaults to 1
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- cumsum(options) {
- const { axis, inplace } = { axis: 1, inplace: false, ...options };
- return this.cumOps("sum", axis, inplace);
- }
-
- /**
- * Returns cumulative minimum accross specified axis.
- * @param options.axis 0 or 1. If 0, count column-wise, if 1, add row-wise. Defaults to 1
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- cummin(options) {
- const { axis, inplace } = { axis: 1, inplace: false, ...options };
- return this.cumOps("min", axis, inplace);
- }
-
- /**
- * Returns cumulative maximum accross specified axis.
- * @param options.axis 0 or 1. If 0, count column-wise, if 1, add row-wise. Defaults to 1
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- cummax(options) {
- const { axis, inplace } = { axis: 1, inplace: false, ...options };
- return this.cumOps("max", axis, inplace);
- }
-
- /**
- * Internal helper function for cumulative operation on DataFrame
- */
- cumOps(ops, axis, inplace) {
- if (this.dtypes.includes("string")) ErrorThrower.throwStringDtypeOperationError(ops);
-
- const result = this.$getDataByAxisWithMissingValuesRemoved(axis);
-
- let newData = result.map((sData) => {
- let tempval = sData[0];
- const data = [tempval];
-
- for (let i = 1; i < sData.length; i++) {
- let currVal = sData[i];
- switch (ops) {
- case "max":
- if (currVal > tempval) {
- data.push(currVal);
- tempval = currVal;
- } else {
- data.push(tempval);
- }
- break;
- case "min":
- if (currVal < tempval) {
- data.push(currVal);
- tempval = currVal;
- } else {
- data.push(tempval);
- }
- break;
- case "sum":
- tempval = (tempval) + (currVal);
- data.push(tempval);
- break;
- case "prod":
- tempval = (tempval) * (currVal);
- data.push(tempval);
- break;
-
- }
- }
- return data;
- });
-
- if (axis === 1) {
- newData = utils.transposeArray(newData);
- }
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- return new DataFrame(newData, {
- index: [...this.index],
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
- }
-
- /**
- * Generate descriptive statistics for all numeric columns
- * Descriptive statistics include those that summarize the central tendency,
- * dispersion and shape of a dataset’s distribution, excluding NaN values.
- * @returns {Series}
- */
- describe() {
- const numericColumnNames = this.columns.filter((name) => this.$getColumnDtype(name) !== "string");
- const index = ["count", "mean", "std", "min", "median", "max", "variance"];
- const statsObject = {};
- for (let i = 0; i < numericColumnNames.length; i++) {
- const colName = numericColumnNames[i];
- const $count = (this.$getColumnData(colName)).count();
- const $mean = mean(this.$getColumnData(colName, false));
- const $std = std(this.$getColumnData(colName, false));
- const $min = (this.$getColumnData(colName)).min();
- const $median = median(this.$getColumnData(colName, false));
- const $max = (this.$getColumnData(colName)).max();
- const $variance = variance(this.$getColumnData(colName, false));
-
- const stats = [$count, $mean, $std, $min, $median, $max, $variance];
- statsObject[colName] = stats;
-
- }
-
- const df = new DataFrame(statsObject, { index });
- return df;
- }
-
- /**
- * Drops all rows or columns with missing values (NaN)
- * @param axis 0 or 1. If 0, drop columns with NaNs, if 1, drop rows with NaNs
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- dropna(axis = 1, options) {
- const { inplace } = { inplace: false, ...options };
-
- if ([0, 1].indexOf(axis) === -1) {
- throw Error("ParamError: Axis must be 0 or 1");
- }
-
- const newIndex = [];
-
- if (axis == 0) {
- const newData = [];
-
- const dfValues = this.values;
- for (let i = 0; i < dfValues.length; i++) {
- const values = dfValues[i];
- if (!values.includes(NaN) && !values.includes(undefined) && !values.includes(null)) {
- newData.push(values);
- newIndex.push(this.index[i]);
- }
- }
-
- if (inplace) {
- this.$setValues(newData, false);
- this.$setIndex(newIndex);
- } else {
- return new DataFrame(
- newData,
- {
- index: newIndex,
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
-
- } else {
- const newColumnNames = [];
- const newDtypes = [];
- let dfValues = [];
-
- if (this.config.isLowMemoryMode) {
- dfValues = utils.transposeArray(this.values);
- } else {
- dfValues = this.$dataIncolumnFormat;
- }
- const tempColArr = [];
-
- for (let i = 0; i < dfValues.length; i++) {
- const values = dfValues[i];
- if (!values.includes(NaN)) {
- tempColArr.push(values);
- newColumnNames.push(this.columns[i]);
- newDtypes.push(this.dtypes[i]);
- }
- }
-
- const newData = utils.transposeArray(tempColArr);
-
- if (inplace) {
- this.$setValues(newData, false, false);
- this.$setColumnNames(newColumnNames);
- this.$setDtypes(newDtypes);
- } else {
- return new DataFrame(
- newData,
- {
- index: [...this.index],
- columns: newColumnNames,
- dtypes: newDtypes,
- config: { ...this.config }
- });
- }
- }
-
- }
-
- /**
- * Adds a new column to the DataFrame. If column exists, then the column values is replaced.
- * @param column The name of the column to add or replace.
- * @param values An array of values to be inserted into the DataFrame. Must be the same length as the columns
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- addColumn(options) {
- const { column, values, inplace } = { inplace: false, ...options };
-
- if (!column) {
- throw new Error("ParamError: column must be specified");
- }
-
- if (!values) {
- throw new Error("ParamError: values must be specified");
- }
-
- const columnIndex = this.$columns.indexOf(column);
-
- if (columnIndex === -1) {
- let colunmValuesToAdd;
-
- if (values instanceof Series) {
- colunmValuesToAdd = values.values;
- } else if (Array.isArray(values)) {
- colunmValuesToAdd = values;
- } 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]) {
- ErrorThrower.throwColumnLengthError(this, colunmValuesToAdd.length);
- }
-
- const newData = [];
- const oldValues = this.$data;
- for (let i = 0; i < oldValues.length; i++) {
- const innerArr = [...oldValues[i]];
- innerArr.push(colunmValuesToAdd[i]);
- newData.push(innerArr);
- }
-
- if (inplace) {
- this.$setValues(newData, true, false);
- this.$setColumnNames([...this.columns, column]);
- this.$setInternalColumnDataProperty(column);
-
- } else {
- const df = new DataFrame(newData, {
- index: [...this.index],
- columns: [...this.columns, column],
- dtypes: [...this.dtypes, utils.inferDtype(colunmValuesToAdd)[0]],
- config: { ...this.$config }
- });
- return df;
- }
- } else {
- this.$setColumnData(column, values);
- }
-
- }
-
- /**
- * Makes a new copy of a DataFrame
- */
- copy() {
- let df = new DataFrame([...this.$data], {
- columns: [...this.columns],
- index: [...this.index],
- dtypes: [...this.dtypes],
- config: { ...this.$config }
- });
- return df;
- }
-
- /**
- * Return a boolean same-sized object indicating where elements are empty (NaN, undefined, null).
- * NaN, undefined and null values gets mapped to true, and everything else gets mapped to false.
- */
- isna() {
- const newData = [];
- for (let i = 0; i < this.values.length; i++) {
- const valueArr = this.values[i];
- const tempData = valueArr.map((value) => {
- if (utils.isEmpty(value)) {
- return true;
- } else {
- return false;
- }
- });
- newData.push(tempData);
- }
-
- const df = new DataFrame(newData,
- {
- index: [...this.index],
- columns: [...this.columns],
- config: { ...this.config }
- });
- return df;
- }
-
- /**
- * Replace all empty elements with a specified value. Replace params expect columns array to map to values array.
- * @param columns The list of column names to be replaced
- * @param options.values The list of values to use for replacement.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- fillna(values, options) {
- let { columns, inplace } = { inplace: false, ...options };
-
- if (!values && typeof values !== "boolean") {
- throw Error('ParamError: value must be specified');
- }
-
- if (Array.isArray(values)) {
- if (!Array.isArray(columns)) {
- throw Error('ParamError: value is an array, hence columns must also be an array of same length');
- }
-
- if (values.length !== columns.length) {
- throw Error('ParamError: specified column and values must have the same length');
- }
-
- columns.forEach((col) => {
- if (!this.columns.includes(col)) {
- throw Error(
- `ValueError: Specified column "${col}" must be one of ${this.columns}`
- );
- }
- });
- }
-
- const newData = [];
- const oldValues = [...this.values];
-
- if (!columns) {
- //Fill all columns
- for (let i = 0; i < oldValues.length; i++) {
- const valueArr = [...oldValues[i]];
-
- const tempArr = valueArr.map((innerVal) => {
- if (utils.isEmpty(innerVal)) {
- const replaceWith = Array.isArray(values) ? values[i] : values;
- return replaceWith;
- } else {
- return innerVal;
- }
- });
- newData.push(tempArr);
- }
-
- } else {
- //Fill specific columns
- const tempData = [...this.values];
-
- for (let i = 0; i < tempData.length; i++) {
- const valueArr = tempData[i];
-
- for (let i = 0; i < columns.length; i++) { //B
- const columnIndex = this.columns.indexOf(columns[i]);
- const replaceWith = Array.isArray(values) ? values[i] : values;
- valueArr[columnIndex] = utils.isEmpty(valueArr[columnIndex]) ? replaceWith : valueArr[columnIndex];
- }
- newData.push(valueArr);
- }
- }
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- const df = new DataFrame(newData,
- {
- index: [...this.index],
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- return df;
- }
- }
-
- /**
- * Drop columns or rows with missing values. Missing values are NaN, undefined or null.
- * @param options.columns Array of column names to drop
- * @param options.index Array of index to drop
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- drop(options) {
- let { columns, index, inplace } = { inplace: false, ...options };
-
- if (!columns && !index) {
- throw Error('ParamError: Must specify one of columns or index');
- }
-
- if (columns && index) {
- throw Error('ParamError: Can only specify one of columns or index');
- }
-
- if (columns) {
- const columnIndices = [];
-
- if (typeof columns === "string") {
- columnIndices.push(this.columns.indexOf(columns));
- } else if (Array.isArray(columns)) {
- for (let column of columns) {
- if (this.columns.indexOf(column) === -1) {
- throw Error(`ParamError: specified column "${column}" not found in columns`);
- }
- columnIndices.push(this.columns.indexOf(column));
- }
-
- } else {
- throw Error('ParamError: columns must be an array of column names or a string of column name');
- }
-
- let newRowData = [];
- let newColumnNames = [];
- let newDtypes = [];
-
- for (let i = 0; i < this.values.length; i++) {
- const tempInnerArr = [];
- const innerArr = this.values[i];
- for (let j = 0; j < innerArr.length; j++) {
- if (!(columnIndices.includes(j))) {
- tempInnerArr.push(innerArr[j]);
- }
- }
- newRowData.push(tempInnerArr);
- }
-
- for (let i = 0; i < this.columns.length; i++) {
- const element = this.columns[i];
- if (!(columns.includes(element))) {
- newColumnNames.push(element);
- newDtypes.push(this.dtypes[i]);
- }
- }
-
- if (inplace) {
- this.$setValues(newRowData, true, false);
- this.$setColumnNames(newColumnNames);
- } else {
- const df = new DataFrame(newRowData,
- {
- index: [...this.index],
- columns: newColumnNames,
- dtypes: newDtypes,
- config: { ...this.config }
- });
- return df;
- }
-
- }
-
- if (index) {
- const rowIndices = [];
-
- if (typeof index === "string" || typeof index === "number" || typeof index === "boolean") {
- rowIndices.push(this.index.indexOf(index));
- } else if (Array.isArray(index)) {
- for (let indx of index) {
- if (this.index.indexOf(indx) === -1) {
- throw Error(`ParamError: specified index "${indx}" not found in indices`);
- }
- rowIndices.push(this.index.indexOf(indx));
- }
- } else {
- throw Error('ParamError: index must be an array of indices or a scalar index');
- }
-
- let newRowData = [];
- let newIndex = [];
-
- for (let i = 0; i < this.values.length; i++) {
- const innerArr = this.values[i];
- if (!(rowIndices.includes(i))) {
- newRowData.push(innerArr);
- }
- }
-
- for (let i = 0; i < this.index.length; i++) {
- const indx = this.index[i];
- if (!(index.includes(indx))) {
- newIndex.push(indx);
- }
- }
-
- if (inplace) {
- this.$setValues(newRowData, false);
- this.$setIndex(newIndex);
- } else {
- const df = new DataFrame(newRowData,
- {
- index: newIndex,
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- return df;
- }
- }
-
- }
-
- /**
- * Sorts a Dataframe by a specified column values
- * @param options.column Column name to sort by
- * @param options.ascending Whether to sort values in ascending order or not. Defaults to true
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- sort_values(options) {
- const { by, ascending, inplace } = { by: undefined, ascending: true, inplace: false, ...options };
-
- if (!by) {
- throw Error(`ParamError: must specify a column to sort by`);
- }
-
- if (this.columns.indexOf(by) === -1) {
- throw Error(`ParamError: specified column "${by}" not found in columns`);
- }
-
- const columnValues = this.$getColumnData(by, false);
- const index = [...this.index];
-
- const objToSort = columnValues.map((value, i) => {
- return { index: index[i], value };
- });
-
- const sortedObjectArr = utils.sortObj(objToSort, ascending);
- const sortedIndex = sortedObjectArr.map((obj) => obj.index);
-
- const newDf = _loc({ ndFrame: this, rows: sortedIndex });
-
- if (inplace) {
- this.$setValues(newDf.values);
- this.$setIndex(newDf.index);
- } else {
- return newDf;
- }
-
- }
-
- /**
- * Sets the index of the DataFrame to the specified index.
- * @param options.index An array of index values to set
- * @param options.column A column name to set the index to
- * @param options.drop Whether to drop the column whose index was set. Defaults to false
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- set_index(options) {
- const { index, column, drop, inplace } = { drop: false, inplace: false, ...options };
-
- if (!index && !column) {
- throw new Error("ParamError: must specify either index or column");
- }
-
- let newIndex = [];
-
- if (index) {
- if (!Array.isArray(index)) {
- throw Error(`ParamError: index must be an array`);
- }
-
- if (index.length !== this.values.length) {
- throw Error(`ParamError: index must be the same length as the number of rows`);
- }
- newIndex = index;
- }
-
- if (column) {
- if (this.columns.indexOf(column) === -1) {
- throw Error(`ParamError: column not found in column names`);
- }
-
- newIndex = this.$getColumnData(column, false);
- }
-
- if (drop) {
- const dfDropped = this.drop({ columns: [column] });
-
- const newData = dfDropped.values;
- const newColumns = dfDropped.columns;
- const newDtypes = dfDropped.dtypes;
-
- if (inplace) {
- this.$setValues(newData, true, false);
- this.$setIndex(newIndex);
- this.$setColumnNames(newColumns);
- } else {
- const df = new DataFrame(newData,
- {
- index: newIndex,
- columns: newColumns,
- dtypes: newDtypes,
- config: { ...this.config }
- });
- return df;
- }
- } else {
- if (inplace) {
- this.$setIndex(newIndex);
- } else {
- const df = new DataFrame(this.values,
- {
- index: newIndex,
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- return df;
- }
- }
-
- }
-
- /**
- * Resets the index of the DataFrame to the default index.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- reset_index(options) {
- const { inplace } = { inplace: false, ...options };
-
- if (inplace) {
- this.$resetIndex();
- } else {
- const df = new DataFrame(this.values,
- {
- index: this.index.map((_, i) => i),
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- return df;
- }
-
- }
-
- /**
- * Apply a function along an axis of the DataFrame. To apply a function element-wise, use `applyMap`.
- * Objects passed to the function are Series values whose
- * index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1)
- * @param callable Function to apply to each column or row
- * @param options.axis 0 or 1. If 0, compute the power column-wise, if 1, row-wise
- */
- apply(callable, options) {
- const { axis } = { axis: 1, ...options };
-
- if ([0, 1].indexOf(axis) === -1) {
- throw Error(`ParamError: axis must be 0 or 1`);
- }
-
- const valuesForFunc = this.$getDataByAxisWithMissingValuesRemoved(axis);
-
- const result = valuesForFunc.map((row) => {
- return callable(row);
- });
-
- if (axis === 1) {
- if (utils.is1DArray(result)) {
- return new Series(result, {
- index: [...this.columns]
- });
- } else {
- return new DataFrame(result, {
- index: [...this.columns],
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
- } else {
- if (utils.is1DArray(result)) {
- return new Series(result, {
- index: [...this.index]
- });
- } else {
- return new DataFrame(result, {
- index: [...this.index],
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
- }
-
- }
-
- /**
- * Apply a function to a Dataframe values element-wise.
- * @param callable Function to apply to each column or row
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- apply_map(callable, options) {
- const { inplace } = { inplace: false, ...options };
-
- const newData = this.values.map((row) => {
- const tempData = row.map((val) => {
- return callable(val);
- });
- return tempData;
- });
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- return new DataFrame(newData,
- {
- index: [...this.index],
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
- }
-
- /**
- * Returns the specified column data as a Series object.
- * @param column The name of the column to return
- */
- column(column) {
- return this.$getColumnData(column);
- }
-
- /**
- * Return a subset of the DataFrame’s columns based on the column dtypes.
- * @param include An array of dtypes or strings to be included.
- */
- select_dtypes(include) {
- const supportedDtypes = ["float32", "int32", "string", "boolean", 'undefined'];
-
- if (Array.isArray(include) === false) {
- throw Error(`ParamError: include must be an array`);
- }
-
- include.forEach((dtype) => {
- if (supportedDtypes.indexOf(dtype) === -1) {
- throw Error(`ParamError: include must be an array of valid dtypes`);
- }
- });
- const newColumnNames = [];
-
- for (let i = 0; i < this.dtypes.length; i++) {
- if (include.includes(this.dtypes[i])) {
- newColumnNames.push(this.columns[i]);
- }
- }
- return this.loc({ columns: newColumnNames });
-
- }
-
- /**
- * Returns the transposes the DataFrame.
- **/
- transpose(options) {
- const { inplace } = { inplace: false, ...options };
- const newData = utils.transposeArray(this.values);
- const newColNames = [...this.index.map((i) => i.toString())];
-
- if (inplace) {
- this.$setValues(newData, false, false);
- this.$setIndex([...this.columns]);
- this.$setColumnNames(newColNames);
- } else {
- return new DataFrame(newData, {
- index: [...this.columns],
- columns: newColNames,
- config: { ...this.config }
- });
- }
- }
-
- /**
- * Returns the Transpose of the DataFrame. Similar to `transpose`, but does not change the original DataFrame.
- **/
- get T() {
- const newData = utils.transposeArray(this.values);
- return new DataFrame(newData, {
- index: [...this.columns],
- columns: [...this.index.map((i) => i.toString())],
- config: { ...this.config }
- });
- }
-
- /**
- * Replace all occurence of a value with a new value
- * @param oldValue The value you want to replace
- * @param newValue The new value you want to replace the old value with
- * @param options.columns An array of column names you want to replace. If not provided replace accross all columns.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- replace(
- oldValue,
- newValue,
- options
- ) {
- const { columns, inplace } = { inplace: false, ...options };
-
- if (!oldValue && typeof oldValue !== 'boolean') {
- throw Error(`Params Error: Must specify param 'oldValue' to replace`);
- }
-
- if (!newValue && typeof newValue !== 'boolean') {
- throw Error(`Params Error: Must specify param 'newValue' to replace with`);
- }
-
- let newData = [];
-
- if (columns) {
- if (!Array.isArray(columns)) {
- throw Error(`Params Error: column must be an array of column(s)`);
- }
- const columnIndex = [];
-
- columns.forEach((column) => {
- const _indx = this.columns.indexOf(column);
- if (_indx === -1) {
- throw Error(`Params Error: column not found in columns`);
- }
- columnIndex.push(_indx);
- });
-
- newData = (this.values).map(([...row]) => {
- for (const colIndx of columnIndex) {
- if (row[colIndx] === oldValue) {
- row[colIndx] = newValue;
- }
- }
- return row;
- });
- } else {
- newData = (this.values).map(([...row]) => {
- return row.map(((cell) => {
- if (cell === oldValue) {
- return newValue;
- } else {
- return cell;
- }
- }));
- });
- }
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- return new DataFrame(newData, {
- index: [...this.index],
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
- }
-
-
- /**
- * Cast the values of a column to specified data type
- * @param column The name of the column to cast
- * @param dtype Data type to cast to. One of [float32, int32, string, boolean]
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- astype(options) {
- const { inplace, column, dtype } = { inplace: false, ...options };
- const columnIndex = this.columns.indexOf(column);
-
- if (columnIndex === -1) {
- throw Error(`Params Error: column not found in columns`);
- }
-
- if (!(DATA_TYPES.includes(dtype))) {
- throw Error(`dtype ${dtype} not supported. dtype must be one of ${DATA_TYPES}`);
- }
-
- const data = this.values;
-
- const newData = data.map((row) => {
- if (dtype === "float32") {
- row[columnIndex] = Number(row[columnIndex]);
- return row;
- } else if (dtype === "int32") {
- row[columnIndex] = parseInt(row[columnIndex]);
- return row;
- } else if (dtype === "string") {
- row[columnIndex] = row[columnIndex].toString();
- return row;
- } else if (dtype === "boolean") {
- row[columnIndex] = Boolean(row[columnIndex]);
- return row;
- }
- });
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- const newDtypes = [...this.dtypes];
- newDtypes[columnIndex] = dtype;
-
- return new DataFrame(newData, {
- index: [...this.index],
- columns: [...this.columns],
- dtypes: newDtypes,
- config: { ...this.config }
- });
- }
- }
-
- /**
- * Return the number of unique elements in a across the specified axis.
- * To get the values use `.unique()` instead.
- * @param axis The axis to count unique elements across. Defaults to 1
- */
- nunique(axis = 1) {
- if ([0, 1].indexOf(axis) === -1) {
- throw Error(`ParamError: axis must be 0 or 1`);
- }
- const data = this.$getDataArraysByAxis(axis);
- const newData = data.map((row) => new Set(row).size);
-
- if (axis === 0) {
- return new Series(newData, {
- index: [...this.columns],
- dtypes: ["int32"]
- });
- } else {
- return new Series(newData, {
- index: [...this.index],
- dtypes: ["int32"]
- });
- }
- }
-
- /**
- * Renames a column or index.
- * @param mapper An object that maps each column or index in the DataFrame to a new value
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- * @param options.axis The axis to perform the operation on. Defaults to 1
- */
- rename(options) {
- const { mapper, axis, inplace } = { axis: 1, inplace: false, ...options };
-
- if ([0, 1].indexOf(axis) === -1) {
- throw Error(`ParamError: axis must be 0 or 1`);
- }
-
- if (axis === 1) {
- const colsAdded = [];
- const newColumns = this.columns.map((col) => {
- if (mapper[col] !== undefined) {
- colsAdded.push(mapper[col]);
- return mapper[col];
- } else {
- return col;
- }
- });
-
- if (inplace) {
- this.$setColumnNames(newColumns);
- for (const col of colsAdded) {
- this.$setInternalColumnDataProperty(col);
- }
- } else {
- return new DataFrame([...this.values], {
- index: [...this.index],
- columns: newColumns,
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
- } else {
- const newIndex = this.index.map((col) => {
- if (mapper[col] !== undefined) {
- return mapper[col];
- } else {
- return col;
- }
- });
-
- if (inplace) {
- this.$setIndex(newIndex);
- } else {
- return new DataFrame([...this.values], {
- index: newIndex,
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
- }
-
- }
-
- /**
- * Sorts the Dataframe by the index.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- * @param options.ascending Whether to sort values in ascending order or not. Defaults to true
- */
- sort_index(options) {
- const { ascending, inplace } = { ascending: true, inplace: false, ...options };
-
- const indexPosition = utils.range(0, this.index.length - 1);
- const index = [...this.index];
-
- const objToSort = index.map((idx, i) => {
- return { index: indexPosition[i], value: idx };
- });
-
- const sortedObjectArr = utils.sortObj(objToSort, ascending);
- let sortedIndex = sortedObjectArr.map((obj) => obj.index);
- const newData = sortedIndex.map((i) => (this.values)[i]);
- sortedIndex = sortedIndex.map((i) => index[i]);
-
- if (inplace) {
- this.$setValues(newData);
- this.$setIndex(sortedIndex);
- } else {
- return new DataFrame(newData, {
- index: sortedIndex,
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
-
- }
-
- /**
- * Add new rows to the end of the DataFrame.
- * @param newValues Array, Series or DataFrame to append to the DataFrame
- * @param index The new index value(s) to append to the Series. Must contain the same number of values as `newValues`
- * as they map `1 - 1`.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- append(
- newValues,
- index,
- options
- ) {
- const { inplace } = { inplace: false, ...options };
-
- if (!newValues) {
- throw Error(`ParamError: newValues must be a Series, DataFrame or Array`);
- }
-
- if (!index) {
- throw Error(`ParamError: index must be specified`);
- }
-
- let rowsToAdd = [];
- if (newValues instanceof Series) {
-
- if (newValues.values.length !== this.shape[1]) {
- throw Error(`ValueError: length of newValues must be the same as the number of columns.`);
- }
- rowsToAdd = [newValues.values];
-
- } else if (newValues instanceof DataFrame) {
-
- if (newValues.shape[1] !== this.shape[1]) {
- throw Error(`ValueError: length of newValues must be the same as the number of columns.`);
- }
- rowsToAdd = newValues.values;
-
- } else if (Array.isArray(newValues)) {
-
- if (utils.is1DArray(newValues)) {
- rowsToAdd = [newValues];
- } else {
- rowsToAdd = newValues;
- }
-
- if ((rowsToAdd[0]).length !== this.shape[1]) {
- throw Error(`ValueError: length of newValues must be the same as the number of columns.`);
- }
-
- } else {
- throw Error(`ValueError: newValues must be a Series, DataFrame or Array`);
- }
-
-
- let indexInArrFormat = [];
- if (!Array.isArray(index)) {
- indexInArrFormat = [index];
- } else {
- indexInArrFormat = index;
- }
-
- if (rowsToAdd.length !== indexInArrFormat.length) {
- throw Error(`ParamError: index must contain the same number of values as newValues`);
- }
-
- const newData = [...this.values];
- const newIndex = [...this.index];
-
- rowsToAdd.forEach((row, i) => {
- newData.push(row);
- newIndex.push(indexInArrFormat[i]);
- });
-
- if (inplace) {
- this.$setValues(newData);
- this.$setIndex(newIndex);
- } else {
- return new DataFrame(newData, {
- index: newIndex,
- columns: [...this.columns],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- }
-
- }
-
- /**
- * Queries the DataFrame for rows that meet the boolean criteria.
- * @param options
- * - `column` A column name to query with.
- * - `is` A logical operator. Can be one of the following: [">", "<", "<=", ">=", "==", "!="]
- * - `to` A value to query with.
- * - `condition` An array of boolean mask, one for each row in the DataFrame. Rows where the value are true will be returned.
- * If specified, then other parameters are ignored.
- * - `inplace` Boolean indicating whether to perform the operation inplace or not. Defaults to false
- **/
- query(options) {
- const { inplace, condition, column, is, to } = { inplace: false, ...options };
-
- if (condition) {
- const result = _iloc({
- ndFrame: this,
- rows: condition
- });
-
- if (inplace) {
- this.$setValues(result.values, false, false);
- this.$setIndex(result.index);
- return;
- } else {
- return result;
- }
-
- }
-
- const operators = [">", "<", "<=", ">=", "==", "!="];
-
- let columnIndex, operator, value;
-
- if (column) {
- if (this.columns.includes(column)) {
- columnIndex = this.columns.indexOf(column);
- } else {
- throw new Error(`ParamError: column ${column} not found in column names`);
- }
- } else {
- throw new Error(`ParamError: specify a column name to query`);
- }
-
- if (is) {
- if (operators.includes(is)) {
- operator = is;
- } else {
- throw new Error(`ParamError: specified operato ${is} is not a supported. operator must be one of ${operators}`);
- }
- } else {
- throw new Error(`ParamError: specify an operator to apply. operator must be one of ${operators}`);
- }
-
- if (to) {
- value = to;
- } else {
- throw new Error("ParamError: specify a value to query by");
- }
-
- let data = this.values;
- let index = this.index;
- let newData = [];
- let newIndex = [];
-
- for (var i = 0; i < data.length; i++) {
- let dataValue = data[i];
- let elem = dataValue[columnIndex];
- //use eval function for easy operation
- //eval() takes in a string expression e.g eval('2>5')
- if (eval(`elem${operator}value`)) {
- newData.push(dataValue);
- newIndex.push(index[i]);
- }
- }
-
- if (inplace) {
- this.$setValues(newData, false, false);
- this.$setIndex(newIndex);
- return;
- } else {
- return new DataFrame(newData, {
- index: newIndex,
- columns: this.columns,
- config: { ...this.config }
- });
- }
- }
-
- /**
- * Returns the data types for each column as a Series.
- */
- get ctypes() {
- return new Series(this.dtypes, { index: this.columns });
- }
-
- /**
- * One-hot encode specified columns in the DataFrame. If columns are not specified, all columns of dtype string will be encoded.
- * @param options Options for the operation. The following options are available:
- * - `columns`: A single column name or an array of column names to encode. Defaults to all columns of dtype string.
- * - `prefix`: Prefix to add to the column names. Defaults to unique labels.
- * - `prefixSeparator`: Separator to use for the prefix. Defaults to '_'.
- * - `inplace` indicating whether to perform the operation inplace or not. Defaults to false
- * @returns A DataFrame with the one-hot encoded columns.
- * @example
- * df.getDummies({ columns: ['a', 'b'] })
- * df.getDummies({ columns: ['a', 'b'], prefix: 'cat' })
- * df.getDummies({ columns: ['a', 'b'], prefix: 'cat', prefixSeparator: '-' })
- * df.getDummies({ columns: ['a', 'b'], prefix: 'cat', prefixSeparator: '-', inplace: true })
- * df.getDummies({ columns: ['a', 'b'], prefix: ['col1', 'col2'], prefixSeparator: '-', inplace: true })
- */
- get_dummies(options) {
- const { inplace } = { inplace: false, ...options };
-
- const encodedDF = dummyEncode(this, options);
- if (inplace) {
- this.$setValues(encodedDF.values, false, false);
- this.$setColumnNames(encodedDF.columns);
- } else {
- return encodedDF;
- }
-
- }
-
- /**
- * Make plots of Series or DataFrame.
- * Uses the Plotly as backend, so supports Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @returns {Class} Plot class that expoese different plot type
- */
- plot(div) {
- const plt = new Plot(this, div);
- return plt;
- }
-
- /**
- *
- * @param {col} col is a list of columns
- */
- groupby(col) {
- const len = this.shape[0];
- const columns = this.columns;
- const col_index = col.map((val) => columns.indexOf(val));
- const col_dtype = this.dtypes.filter((val, index) => {
- return col_index.includes(index);
- });
-
- const self = this;
- const data = col.map(
- (column_name) => {
- if (!(columns.includes(column_name)))
- throw new Error(`column ${column_name} does not exist`);
- const column_data = self.column(column_name).values;
- return column_data;
- }
- );
-
- const unique_columns = data.map((column_data) => utils.unique(column_data));
-
- function getRecursiveDict(uniq_columns) {
- const first_uniq_columns = uniq_columns[0];
- const remaining_columns = uniq_columns.slice(1);
- const c_dict = {};
- if (!remaining_columns.length)
- first_uniq_columns.forEach((col_value) => c_dict[col_value] = []);
- else
- first_uniq_columns.forEach((col_value) => c_dict[col_value] = getRecursiveDict(remaining_columns));
- return c_dict;
- }
- const col_dict = getRecursiveDict(unique_columns);
-
- return new GroupBy(
- col_dict,
- col,
- this.values,
- columns,
- col_dtype
- ).group();
- }
-
-}
diff --git a/danfojs-browser/src/core/generic.js b/danfojs-browser/src/core/generic.js
deleted file mode 100644
index 2c0e6c6d..00000000
--- a/danfojs-browser/src/core/generic.js
+++ /dev/null
@@ -1,451 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-
-import { utils } from "../shared/utils";
-import Configs from "../shared/config";
-import ErrorThrower from '../shared/errors';
-import { BASE_CONFIG, DATA_TYPES } from '../shared/defaults';
-import { toCSV, toJSON, toExcel } from "../io";
-import { Tensor, tensor1d, tensor2d } from "@tensorflow/tfjs";
-
-// const utils = new Utils();
-
-/**
- * N-Dimension data structure. Stores multi-dimensional
- * data in a size-mutable, labeled data structure. Analogous to the Python Pandas DataFrame.
- *
- * @param Object
- *
- * data: 1D or 2D Array, JSON, Tensor, Block of data.
- *
- * index: Array of numeric or string names for subseting array. If not specified, indexes are auto generated.
- *
- * columns: Array of column names. If not specified, column names are auto generated.
- *
- * dtypes: Array of data types for each the column. If not specified, dtypes inferred.
- *
- * config: General configuration object for NDframe
- *
- * @returns NDframe
- */
-export default class NDframe {
-
- constructor({ data, index, columns, dtypes, config, isSeries }) {
- this.$isSeries = isSeries;
- if (config) {
- this.$config = new Configs({ ...BASE_CONFIG, ...config });
- } else {
- this.$config = new Configs(BASE_CONFIG);
- }
-
- if (data instanceof Tensor) {
- data = data.arraySync();
- }
-
- if (data === undefined || (Array.isArray(data) && data.length === 0)) {
- this.loadArrayIntoNdframe({ data: [], index: [], columns: [], dtypes: [] });
- } else if (utils.is1DArray(data)) {
- this.loadArrayIntoNdframe({ data, index, columns, dtypes });
- } else {
-
- if (Array.isArray(data) && utils.isObject(data[0])) {
- this.loadObjectIntoNdframe({ data, type: 1, index, columns, dtypes });
-
- } else if (utils.isObject(data)) {
- this.loadObjectIntoNdframe({ data, type: 2, index, columns, dtypes });
-
- } else if (
- Array.isArray((data)[0]) ||
- utils.isNumber((data)[0]) ||
- utils.isString((data)[0])
- ) {
- this.loadArrayIntoNdframe({ data, index, columns, dtypes });
- } else {
- throw new Error("File format not supported!");
- }
- }
- }
-
- /**
- * Internal function to load array of data into NDFrame
- * @param data The array of data to load into NDFrame
- * @param index Array of numeric or string names for subsetting array.
- * @param columns Array of column names.
- * @param dtypes Array of data types for each the column.
- */
- loadArrayIntoNdframe({ data, index, columns, dtypes }) {
- // this.$data = utils.replaceUndefinedWithNaN(data, this.$isSeries);
- this.$data = data;
- if (!this.$config.isLowMemoryMode) {
- //In NOT low memory mode, we transpose the array and save in column format.
- //This makes column data retrieval run in constant time
- this.$dataIncolumnFormat = utils.transposeArray(data);
- }
- this.$setIndex(index);
- this.$setDtypes(dtypes);
- this.$setColumnNames(columns);
- }
-
- /**
- * Internal function to format and load a Javascript object or object of arrays into NDFrame.
- * @param data Object or object of arrays.
- * @param type The type of the object. There are two recognized types:
- *
- * - type 1 object are in JSON format `[{a: 1, b: 2}, {a: 30, b: 20}]`.
- *
- * - type 2 object are of the form `{a: [1,2,3,4], b: [30,20, 30, 20}]}`
- * @param index Array of numeric or string names for subsetting array.
- * @param columns Array of column names.
- * @param dtypes Array of data types for each the column.
- */
- loadObjectIntoNdframe({ data, type, index, columns, dtypes }) {
- if (type === 1 && Array.isArray(data)) {
- const _data = (data).map((item) => {
- return Object.values(item);
- });
-
- let _columnNames;
-
- if (columns) {
- _columnNames = columns;
- } else {
- _columnNames = Object.keys((data)[0]);
- }
-
- this.loadArrayIntoNdframe({ data: _data, index, columns: _columnNames, dtypes });
-
- } else {
- const [_data, _colNames] = utils.getRowAndColValues(data);
- let _columnNames;
-
- if (columns) {
- _columnNames = columns;
- } else {
- _columnNames = _colNames;
- }
- this.loadArrayIntoNdframe({ data: _data, index, columns: _columnNames, dtypes });
- }
- }
-
- /**
- * Converts and returns the data in the NDframe as a Tensorflow.js Tensor.
- */
- get tensor() {
- if (this.$isSeries) {
- return tensor1d(this.$data);
- } else {
- return tensor2d(this.$data);
- }
- }
-
- /**
- * Returns the dtypes of the columns
- */
- get dtypes() {
- return this.$dtypes;
- }
-
- /**
- * Internal function to set the Dtypes of the NDFrame from an array. This function
- * performs the necessary checks.
- */
- $setDtypes(dtypes) {
- if (this.$isSeries) {
- if (dtypes) {
- if (this.$data.length != 0 && dtypes.length != 1) {
- ErrorThrower.throwDtypesLengthError(this, dtypes);
- }
-
- if (!(DATA_TYPES.includes(`${dtypes[0]}`))) {
- ErrorThrower.throwDtypeNotSupportedError(dtypes[0]);
- }
-
- this.$dtypes = dtypes;
- } else {
- this.$dtypes = utils.inferDtype(this.$data);
- }
-
- } else {
- if (dtypes) {
- if (this.$data.length != 0 && dtypes.length != this.shape[1]) {
- ErrorThrower.throwDtypesLengthError(this, dtypes);
- }
-
- if (this.$data.length == 0 && dtypes.length == 0) {
- this.$dtypes = dtypes;
- } else {
- dtypes.forEach((dtype) => {
- if (!(DATA_TYPES.includes(dtype))) {
- ErrorThrower.throwDtypeNotSupportedError(dtype);
- }
- });
-
- this.$dtypes = dtypes;
-
- }
-
- } else {
- this.$dtypes = utils.inferDtype(this.$data);
- }
- }
- }
-
- /**
- * Returns the dimension of the data. Series have a dimension of 1,
- * while DataFrames have a dimension of 2.
- */
- get ndim() {
- if (this.$isSeries) {
- return 1;
- } else {
- return 2;
- }
- }
-
- /**
- * Returns the axis labels of the NDFrame.
- */
- get axis() {
- return {
- index: this.$index,
- columns: this.$columns
- };
- }
-
- /**
- * Returns the configuration object of the NDFrame.
- */
- get config() {
- return this.$config;
-
- }
-
- /**
- * Internal function to set the configuration of the ndframe
- */
- $setConfig(config) {
- this.$config = config;
- }
-
- /**
- * Returns the indices of the NDFrame
- */
- get index() {
- return this.$index;
-
- }
-
- /**
- * Internal function to set the index of the NDFrame with the specified
- * array of indices. Performs all necessary checks to ensure that the
- * index is valid.
- */
- $setIndex(index) {
- if (index) {
-
- if (this.$data.length != 0 && index.length != this.shape[0]) {
- ErrorThrower.throwIndexLengthError(this, index);
- }
- if (Array.from(new Set(index)).length !== this.shape[0]) {
- ErrorThrower.throwIndexDuplicateError();
- }
-
- this.$index = index;
- } else {
- this.$index = utils.range(0, this.shape[0] - 1); //generate index
- }
- }
-
- /**
- * Internal function to reset the index of the NDFrame using a range of indices.
- */
- $resetIndex() {
- this.$index = utils.range(0, this.shape[0] - 1);
- }
-
- /**
- * Returns the column names of the NDFrame
- */
- get columns() {
- return this.$columns;
- }
-
- /**
- * Internal function to set the column names for the NDFrame. This function
- * performs a check to ensure that the column names are unique, and same length as the
- * number of columns in the data.
- */
- $setColumnNames(columns) {
-
- if (this.$isSeries) {
- if (columns) {
- if (this.$data.length != 0 && columns.length != 1 && typeof columns != 'string') {
- ErrorThrower.throwColumnNamesLengthError(this, columns);
- }
- this.$columns = columns;
- } else {
- this.$columns = ["0"];
- }
- } else {
- if (columns) {
-
- if (this.$data.length != 0 && columns.length != this.shape[1]) {
-
- ErrorThrower.throwColumnNamesLengthError(this, columns);
- }
- if (Array.from(new Set(columns)).length !== this.shape[1]) {
- ErrorThrower.throwColumnDuplicateError();
- }
-
- this.$columns = columns;
- } else {
- this.$columns = (utils.range(0, this.shape[1] - 1)).map((val) => `${val}`); //generate columns
- }
- }
- }
-
- /**
- * Returns the shape of the NDFrame. Shape is determined by [row lenght, column length]
- */
- get shape() {
- if (this.$data.length === 0) return [0, 0];
- if (this.$isSeries) {
- return [this.$data.length, 1];
- } else {
- const rowLen = (this.$data).length;
- const colLen = (this.$data[0]).length;
- return [rowLen, colLen];
- }
-
- }
-
- /**
- * Returns the underlying data in Array row format.
- */
- get values() {
- return this.$data;
- }
-
- /**
- * Updates the internal $data property to the specified value
- * @param values An array of values to set
- * @param checkLength Whether to check the length of the new values and the existing row length
- * @param checkColumnLength Whether to check the length of the new values and the existing column length
- * */
- $setValues(values, checkLength = true, checkColumnLength = true) {
- if (this.$isSeries) {
- if (checkLength && values.length != this.shape[0]) {
- ErrorThrower.throwRowLengthError(this, values.length);
- }
-
- this.$data = values;
- this.$dtypes = utils.inferDtype(values); //Dtype may change depeneding on the value set
-
- if (!this.$config.isLowMemoryMode) {
- this.$dataIncolumnFormat = values;
- }
-
- } else {
- if (checkLength && values.length != this.shape[0]) {
- ErrorThrower.throwRowLengthError(this, values.length);
- }
-
- if (checkColumnLength) {
- values.forEach((value) => {
- if ((value).length != this.shape[1]) {
- ErrorThrower.throwColumnLengthError(this, values.length);
- }
- });
- }
-
- this.$data = values;
- this.$dtypes = utils.inferDtype(values);
-
- if (!this.$config.isLowMemoryMode) {
- this.$dataIncolumnFormat = utils.transposeArray(values);
- }
-
- }
-
- }
-
- /**
- * Returns the underlying data in Array column format.
- */
- get getColumnData() {
- if (this.config.isLowMemoryMode) {
- return utils.transposeArray(this.values);
- } else {
- return this.$dataIncolumnFormat;
- }
- }
-
- /**
- * Returns the size of the NDFrame object
- *
- */
- get size() {
- return this.shape[0] * this.shape[1];
- }
-
- /**
- * Converts a DataFrame or Series to CSV.
- * @param options Configuration object. Supports the following options:
- * - `filePath`: Local file path to write the CSV file. If not specified, the CSV will be returned as a string.
- * - `header`: Boolean indicating whether to include a header row in the CSV file.
- * - `sep`: Character to be used as a separator in the CSV file.
- */
- to_csv(options) {
- return toCSV(this, options);
- }
-
- /**
- * Converts a DataFrame or Series to JSON.
- * @param options Configuration object. Supported options:
- * - `fileName`: The file path to write the JSON to. If not specified, the JSON object is returned.
- * - `format`: The format of the JSON. Defaults to `'column'`. E.g for using `column` format:
- * ```
- * [{ "a": 1, "b": 2, "c": 3, "d": 4 },
- * { "a": 5, "b": 6, "c": 7, "d": 8 }]
- * ```
- * and `row` format:
- * ```
- * { "a": [1, 5, 9],
- * "b": [2, 6, 10]
- * }
- * ```
- */
- to_json(options) {
- return toJSON(this, options);
- }
-
-
- /**
- * Converts a DataFrame or Series to Excel Sheet.
- * @param options Configuration object. Supported options:
- * - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
- * - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`.
- */
- to_excel(options) {
- return toExcel(this, options);
- }
-
- /**
- * Pretty prints a DataFrame or Series to the console
- */
- print() {
- console.log(this + "");
- }
-}
diff --git a/danfojs-browser/src/core/get_dummies.js b/danfojs-browser/src/core/get_dummies.js
deleted file mode 100644
index dd4297e1..00000000
--- a/danfojs-browser/src/core/get_dummies.js
+++ /dev/null
@@ -1,151 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import DataFrame from "./frame";
-import Series from "./series";
-import { utils } from "../shared/utils";
-
-
-/**
- * Generate one-hot encoding for categorical columns in an Array, Series or Dataframe.
- * @param data Series or Dataframe
- * @param columns Columns to encode
- * @param prefix Prefix for the new columns
- * @param prefixSeparator Separator for the prefix and the column name
- * @returns Encoded Dataframe
- * @example
- * import { DataFrame, DummyEncoder }from 'danfojs';
- * const df = new DataFrame([[1,2,3], [4,5,6]], { columns: ['a', 'b', 'c'] });
- * const df2 = new DummyEncoder({data: df, columns: ['a', 'b'], prefix: 'enc', prefixSeparator: '#'}).encode();
- * df2.print();
- */
-function dummyEncode(data, options) {
- let { columns, prefix, prefixSeparator } = { prefixSeparator: "_", ...options };
-
- if (!data) {
- throw new Error('ParamError: data must be one of Array, Series or DataFrame');
- }
-
- if (data instanceof Series || data instanceof DataFrame) {
- if (!columns) {
- const colsWithStringDtype = [];
- data.dtypes.forEach((dtype, index) => {
- if (dtype === "string") {
- colsWithStringDtype.push(data.columns[index]);
- }
- });
- columns = colsWithStringDtype;
- }
- } else {
- throw new Error('ParamError: data must be one of Array, Series or DataFrame');
- }
-
-
- if (typeof columns === "string") {
- columns = [columns];
- if (Array.isArray(prefix) && prefix.length === 1) {
- prefix = prefix;
- } else if (typeof prefix === "string") {
- prefix = [prefix];
- } else {
- throw new Error('ParamError: prefix must be a string, or an array of same length as columns');
- }
-
- if (Array.isArray(prefixSeparator) && prefixSeparator.length === 1) {
- prefixSeparator = prefixSeparator;
- } else if (typeof prefixSeparator === "string") {
- prefixSeparator = [prefixSeparator];
- } else {
- throw new Error('ParamError: prefix must be a string, or an array of same length as columns');
- }
- } else if (Array.isArray(columns)) {
- if (prefix) {
- if (Array.isArray(prefix) && prefix.length !== columns.length) {
- throw new Error(`ParamError: prefix and data array must be of the same length. If you need to use the same prefix, then pass a string param instead. e.g {prefix: "${prefix}"}`);
- }
-
- if (typeof prefix === "string") {
- prefix = columns.map((_) => prefix);
- }
- }
-
- if (prefixSeparator) {
- if (Array.isArray(prefixSeparator) && prefixSeparator.length !== columns.length) {
- throw new Error(`ParamError: prefixSeparator and data array must be of the same length. If you need to use the same prefix separator, then pass a string param instead. e.g {prefixSeparator: "${prefixSeparator}"}`);
- }
-
- if (typeof prefixSeparator === "string") {
- prefixSeparator = columns.map((_) => prefixSeparator);
- }
- }
-
- } else {
- throw new Error('ParamError: columns must be a string or an array of strings');
- }
-
- if (data instanceof Series) {
- const colData = data.values;
- const newColumnNames = [];
- const uniqueValues = Array.from(new Set(colData));
- const oneHotArr = utils.zeros(colData.length, uniqueValues.length);
-
- for (let i = 0; i < colData.length; i++) {
- const index = uniqueValues.indexOf(colData[i]);
- oneHotArr[i][index] = 1;
- }
-
- for (let i = 0; i < uniqueValues.length; i++) {
- const prefixToAdd = prefix ? prefix[0] : i;
- newColumnNames.push(`${prefixToAdd}${prefixSeparator[0]}${uniqueValues[i]}`);
-
- }
-
- return new DataFrame(oneHotArr, { columns: newColumnNames });
-
- } else {
-
- const dfWithSelectedColumnsDropped = data.drop({ columns });
- let newData = dfWithSelectedColumnsDropped.values;
- const newColumnNames = dfWithSelectedColumnsDropped.columns;
- for (let i = 0; i < columns.length; i++) {
- const column = columns[i];
- const colData = data.column(column).values;
-
- const uniqueValues = Array.from(new Set(colData));
- const oneHotArr = utils.zeros(colData.length, uniqueValues.length);
-
- for (let j = 0; j < colData.length; j++) {
- const index = uniqueValues.indexOf(colData[j]);
- oneHotArr[j][index] = 1;
- const prefixToAdd = prefix ? prefix[i] : column;
- const newColName = `${prefixToAdd}${prefixSeparator[i]}${colData[j]}`;
-
- if (!newColumnNames.includes(newColName)) {
- newColumnNames.push(newColName);
- }
- }
-
- for (let k = 0; k < newData.length; k++) {
- newData[k] = [...newData[k], ...oneHotArr[k]];
-
- }
-
- }
-
- return new DataFrame(newData, { columns: newColumnNames });
- }
-
-}
-
-export default dummyEncode;
diff --git a/danfojs-browser/src/core/groupby.js b/danfojs-browser/src/core/groupby.js
deleted file mode 100644
index 2718319c..00000000
--- a/danfojs-browser/src/core/groupby.js
+++ /dev/null
@@ -1,349 +0,0 @@
-import DataFrame from "./frame";
-import { utils } from "../shared/utils";
-import Series from "./series";
-import { concat } from "./concat";
-
-/**
- * The class performs all groupby operation on a dataframe
- * involveing all aggregate funciton
- * @param {col_dict} col_dict Object of unique keys in the group by column
- * @param {key_col} key_col Array contains the column names
- * @param {data} Array the dataframe data
- * @param {column_name} Array of all column name in the dataframe.
- */
-export class GroupBy {
- constructor(col_dict, key_col, data, column_name, col_dtype) {
-
- this.key_col = key_col;
- this.col_dict = col_dict;
- this.data = data;
- this.column_name = column_name;
- this.data_tensors = {}; //store the tensor version of the groupby data
- this.col_dtype = col_dtype;
-
- }
-
- /**
- * Group the dataframe by the column by
- * creating an object to store the grouping
- * @returns Groupby data structure
- */
- group(){
- for (const value of this.data){
- const col_indexes = this.key_col.map((key) => this.column_name.indexOf(key));
- const col_values = col_indexes.map((idx) => value[idx]);
-
- let sub_col_dict = this.col_dict;
- for (const col_value of col_values){
- if (!(col_value in sub_col_dict))
- break;
- if (col_value === col_values[col_values.length - 1])
- sub_col_dict[col_value].push(value);
- else
- sub_col_dict = sub_col_dict[col_value];
- }
- }
-
- const self = this;
- function dfs(sub_col_dict, sub_data_tensors){
- for (const [ key, value ] of Object.entries(sub_col_dict)){
- if (Array.isArray(value)) {
- if (value.length === 0)
- delete sub_col_dict[key];
- else
- sub_data_tensors[key] = new DataFrame(value, { columns:self.column_name });
- } else {
- if (!(key in sub_data_tensors))
- sub_data_tensors[key] = {};
- dfs(value, sub_data_tensors[key]);
- }
- }
- }
- dfs(this.col_dict, this.data_tensors);
-
- return this;
- }
-
- /**
- * obtain the column for each group
- * @param {col_name} col_name [Array]--> array of column names
- * @return Groupby data structure
- */
- col(col_names){
- this.selected_column = col_names; // store col_names for use later in .apply
- if (Array.isArray(col_names)){
-
- for (let i = 0; i < col_names.length; i++){
-
- let col_name = col_names[i];
- if (!this.column_name.includes(col_name)){
- throw new Error(`Column ${col_name} does not exist in groups`);
- }
- }
- } else {
- throw new Error(`Col_name must be an array of column`);
- }
-
- const group_col = {};
- function dfs(sub_data_tensors, sub_group_col) {
- for (const [ key, value ] of Object.entries(sub_data_tensors)){
- if (value instanceof DataFrame) {
- sub_group_col[key] = col_names.map((col_name) => value.column(col_name));
- } else {
- sub_group_col[key] = {};
- dfs(value, sub_group_col[key]);
- }
- }
- }
- dfs(this.data_tensors, group_col);
-
- const gp = new GroupBy(
- null,
- this.key_col,
- null,
- col_names,
- this.col_dtype
- );
- gp.group_col = group_col;
- gp.group_col_name = col_names;
- return gp;
- }
-
-
- /**
- * Basic root of all column arithemetic in groups
- * @param {operation} operation String
- */
- arithemetic(operation){
- const ops_name = [ "mean", "sum", "count", "mode", "std", "var", "cumsum", "cumprod",
- "cummax", "cummin" ];
- const ops_map = {
- "mean": "mean()",
- "sum": "sum()",
- "mode": "mode()",
- "count": "count()",
- "std" : "std()",
- "var" : "var()",
- "cumsum" : "cumsum().values",
- "cumprod": "cumprod().values",
- "cummax" : "cummax().values",
- "cummin" : "cummin().values"
- };
- const is_array_operation = Array.isArray(operation);
- const count_group = {};
-
- //the local variable to store variables to be used in eval
- // this seems not to be needed in Node version, since local
- //variable are easily accessed in the eval function
- let local = null;
- function dfs(sub_count_group, sub_group_col) {
- for (const [ key, value ] of Object.entries(sub_group_col)){
- if (Array.isArray(value)) {
- sub_count_group[key] = [];
- let data;
- if (is_array_operation) {
- for (let i = 0; i < value.length; i++){
- const op = operation[i];
- if (!ops_name.includes(op)){
- throw new Error("operation does not exist");
- }
- local = value[i];
- data = eval(`local.${ops_map[op]}`);
- sub_count_group[key].push(data);
- }
- } else {
- value.forEach((v) => {
- local = v;
- data = eval(`local.${operation}`);
- sub_count_group[key].push(data);
- });
- }
- } else {
- sub_count_group[key] = {};
- dfs(sub_count_group[key], value);
- }
- }
- }
-
- dfs(count_group, this.group_col);
- return count_group;
- }
-
- operations(ops, name) {
- if (!this.group_col) {
- let column = this.column_name.filter((val) => !this.key_col.includes(val));
- let col_gp = this.col(column);
- let value = col_gp.arithemetic(ops);
- let df = col_gp.to_DataFrame(col_gp.key_col, col_gp.group_col_name, value, name);
- return df;
- } else {
- let value = this.arithemetic(ops);
- let df = this.to_DataFrame(this.key_col, this.group_col_name, value, name);
- return df;
- }
- }
- count(){
- return this.operations("count()", "count");
- }
-
- sum(){
- return this.operations("sum()", "sum");
- }
-
- std(){
- return this.operations("std()", "std");
- }
-
- var(){
- return this.operations("var()", "var");
- }
-
- mean(){
- return this.operations("mean()", "mean");
- }
-
- cumsum(){
- return this.operations("cumsum().values", "cumsum");
- }
- cummax(){
- return this.operations("cummax().values", "cummax");
- }
-
- cumprod(){
- return this.operations("cumprod().values", "cumprod");
- }
-
- cummin(){
- return this.operations("cummin().values", "cummin");
- }
-
- max(){
- return this.operations("max()", "max");
- }
-
- min(){
- return this.operations("min()", "min");
- }
-
- /**
- * returns dataframe of a group
- * @param {*} key [Array]
- */
- get_groups(key){
- if (this.key_col.length < 2)
- return this.data_tensors[key];
-
- if (key.length !== this.key_col.length)
- throw new Error("specify the group by column");
-
- utils.isObject(this.data_tensors, key[0], `Key Error: ${key[0]} not in object`);
- const last_key = key[key.length - 1];
- let sub_data_tensors = this.data_tensors;
- for (const k of key) {
- if (k === last_key)
- return sub_data_tensors[k];
- else
- sub_data_tensors = sub_data_tensors[k];
- }
- }
-
- /**
- * Map every column to an operation
- * @param {kwargs} kwargs {column name: math operation}
- * @example .agg({"A": "mean","B": "sum","C":"count"})
- */
- agg(kwargs = {}){
-
- let columns = Object.keys(kwargs);
- let operations = columns.map((x) => { return kwargs[x].toLocaleLowerCase(); });
-
- let col_gp = this.col(columns);
-
- let data = col_gp.arithemetic(operations);
- let df = this.to_DataFrame(col_gp.key_col, col_gp.group_col_name, data, operations);
-
- return df;
- }
-
- to_DataFrame(key_col, col, data, ops){
- const df_data = [];
-
- function concatPathAndNode(path, node, col_dtype) {
- if (Array.isArray(node)) {
- if (Array.isArray(node[0])) {
- if (ops != "apply" ) {
- const transposed_node = node[0].map((_, colIndex) => node.map((row) => row[colIndex]));
- for (const n_array of transposed_node)
- df_data.push(path.concat(n_array));
- } else {
- for (const n_array of node)
- df_data.push(path.concat(n_array));
- }
-
- } else
- df_data.push(path.concat(node));
- } else {
- for (const [ k, child ] of Object.entries(node)) {
- const sanitized_k = col_dtype[0] === "string" ? k : parseInt(k);
- concatPathAndNode(path.concat([ sanitized_k ]), child, col_dtype.slice(1));
- }
- }
- }
-
- concatPathAndNode([], data, this.col_dtype);
-
- const column = [ ...key_col ];
- const group_col = col.slice().map((x, i) => {
- if (Array.isArray(ops)){
- return `${x}_${ops[i]}`;
- }
- return `${x}_${ops}`;
- });
- column.push(...group_col);
-
- return new DataFrame(df_data, { columns: column });
- }
-
- apply(callable){
- let df_data;
- let column;
- if (!this.group_col) {
- column = this.column_name.filter((val) => !this.key_col.includes(val));
- const col_gp = this.col(column);
- df_data = col_gp.group_col;
- } else {
- column = this.group_col_name;
- df_data = this.group_col;
- }
- const count_group = {};
-
- function recursiveCount(sub_df_data, sub_count_group) {
- for (const [ key, value ] of Object.entries(sub_df_data)) {
- if (Array.isArray(value)) {
- let callable_value;
- if (value.length > 1) {
- callable_value = concat({ df_list: value, axis: 1 });
- } else {
- callable_value = value[0];
- }
- const callable_rslt = callable(callable_value);
- if (callable_rslt instanceof DataFrame) {
- column = callable_rslt.columns;
- sub_count_group[key] = callable_rslt.values;
- } else if (callable_rslt instanceof Series) {
- sub_count_group[key] = callable_rslt.values;
- } else
- sub_count_group = callable_rslt;
- } else {
- sub_count_group[key] = {};
- recursiveCount(value, sub_count_group[key]);
- }
- }
- }
-
- recursiveCount(df_data, count_group);
-
- return this.to_DataFrame(this.key_col, column, count_group, "apply");
- }
-
-}
diff --git a/danfojs-browser/src/core/indexing.js b/danfojs-browser/src/core/indexing.js
deleted file mode 100644
index e2b5e04d..00000000
--- a/danfojs-browser/src/core/indexing.js
+++ /dev/null
@@ -1,411 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { utils } from "../shared/utils";
-import Series from "./series";
-import DataFrame from "./frame";
-
-// const utils = new Utils();
-
-/**
-* Slice a Series/DataFrame by specified string location based labels
-* @param Object
-*/
-export function _iloc({ ndFrame, rows, columns }) {
-
- let _rowIndexes;
- let _columnIndexes;
-
- const _data = ndFrame.values;
- const _index = ndFrame.index;
-
- if (rows instanceof Series) {
- rows = rows.values;
- }
-
- if (rows !== undefined && !Array.isArray(rows)) {
- throw new Error(`rows parameter must be an Array. For example: rows: [1,2] or rows: ["0:10"]`);
- }
-
- if (columns !== undefined && !Array.isArray(columns)) {
- throw new Error(`columns parameter must be an Array. For example: columns: [1,2] or columns: ["0:10"]`);
- }
-
- if (!rows) {
- _rowIndexes = utils.range(0, ndFrame.shape[0] - 1);
-
- } else if (rows.length == 1 && typeof rows[0] == "string") {
- const rowSplit = rows[0].split(":");
-
- if (rowSplit.length != 2) {
- throw new Error(`Invalid row split parameter: If using row split string, it must be of the form; rows: ["start:end"]`);
- }
- if (isNaN(parseInt(rowSplit[0])) && rowSplit[0] != "") {
- throw new Error(`Invalid row split parameter. Split parameter must be a number`);
- }
-
- if (isNaN(parseInt(rowSplit[1])) && rowSplit[1] != "") {
- throw new Error(`Invalid row split parameter. Split parameter must be a number`);
- }
-
- const start = rowSplit[0] == "" ? 0 : parseInt(rowSplit[0]);
- const end = rowSplit[1] == "" ? ndFrame.shape[0] : parseInt(rowSplit[1]);
-
- if (start < 0) {
- throw new Error(`row slice [start] index cannot be less than 0`);
- }
-
- if (end > ndFrame.shape[0]) {
- throw new Error(`row slice [end] index cannot be bigger than ${ndFrame.shape[0]}`);
- }
- _rowIndexes = utils.range(start, end - 1);
- } else {
- const _formatedRows = [];
- for (let i = 0; i < rows.length; i++) {
- let _indexToUse = rows[i];
- if (_indexToUse > ndFrame.shape[0]) {
- throw new Error(`Invalid row parameter: Specified index ${_indexToUse} cannot be bigger than index length ${ndFrame.shape[0]}`);
- }
-
- if (typeof _indexToUse !== "number" && typeof _indexToUse !== "boolean") {
- throw new Error(`Invalid row parameter: row index ${_indexToUse} must be a number or boolean`);
- }
-
- if (typeof _indexToUse === "boolean" && _indexToUse === true) {
- _formatedRows.push(_index[i]);
- }
-
- if (typeof _indexToUse === "number") {
- _formatedRows.push(_indexToUse);
- }
- }
-
- _rowIndexes = _formatedRows;
- }
-
- if (!columns) {
- _columnIndexes = utils.range(0, ndFrame.shape[1] - 1);
-
- } else if (columns.length == 1 && typeof columns[0] == "string") {
- const columnSplit = columns[0].split(":");
-
- if (columnSplit.length != 2) {
- throw new Error(`Invalid column split parameter: If using column split string, it must be of the form; columns: ["start:end"]`);
- }
- if (isNaN(parseInt(columnSplit[0])) && columnSplit[0] != "") {
- throw new Error(`Invalid column split parameter. Split parameter must be a number`);
- }
-
- if (isNaN(parseInt(columnSplit[1])) && columnSplit[1] != "") {
- throw new Error(`Invalid column split parameter. Split parameter must be a number`);
- }
-
- const start = columnSplit[0] == "" ? 0 : parseInt(columnSplit[0]);
- const end = columnSplit[1] == "" ? ndFrame.shape[1] : parseInt(columnSplit[1]);
-
- if (start < 0) {
- throw new Error(`column slice [start] index cannot be less than 0`);
- }
-
- if (end > ndFrame.shape[1]) {
- throw new Error(`column slice [end] index cannot be bigger than ${ndFrame.shape[1]}`);
- }
- _columnIndexes = utils.range(start, end - 1);
- } else {
-
- for (let i = 0; i < columns.length; i++) {
- const _indexToUse = columns[i];
- if (_indexToUse > ndFrame.shape[1]) {
- throw new Error(`Invalid column parameter: Specified index ${_indexToUse} cannot be bigger than index length ${ndFrame.shape[1]}`);
- }
-
- if (typeof _indexToUse != "number") {
- throw new Error(`Invalid column parameter: column index ${_indexToUse} must be a number`);
- }
-
- }
-
- _columnIndexes = columns;
- }
-
- if (ndFrame instanceof Series) {
- const newData = [];
- const newIndex = [];
-
- for (let i = 0; i < _rowIndexes.length; i++) {
- const rowIndx = _rowIndexes[i];
- newData.push(_data[rowIndx]);
- newIndex.push(_index[rowIndx]);
- }
- const sf = new Series(
- newData,
- {
- index: newIndex,
- columns: ndFrame.columns,
- dtypes: ndFrame.dtypes,
- config: ndFrame.config
- });
-
- return sf;
- } else {
- const newData = [];
- const newIndex = [];
- const newColumnNames = [];
- const newDtypes = [];
-
- for (let i = 0; i < _rowIndexes.length; i++) {
- const rowIndx = _rowIndexes[i];
- const rowData = _data[rowIndx];
- const newRowDataWithRequiredCols = [];
-
- for (let j = 0; j < _columnIndexes.length; j++) {
- const colIndx = _columnIndexes[j];
- newRowDataWithRequiredCols.push(rowData[colIndx]);
- }
- newData.push(newRowDataWithRequiredCols);
- newIndex.push(_index[rowIndx]);
- }
-
- for (let i = 0; i < _columnIndexes.length; i++) {
- const colIndx = _columnIndexes[i];
- newColumnNames.push(ndFrame.columns[colIndx]);
- newDtypes.push(ndFrame.dtypes[colIndx]);
-
- }
-
- const df = new DataFrame(
- newData,
- {
- index: newIndex,
- columns: newColumnNames,
- dtypes: newDtypes,
- config: ndFrame.config
- });
-
- return df;
-
- }
-
-}
-
-/**
-* Slice a Series/DataFrame by specified string location based labels
-* @param Object
-*/
-export function _loc({ ndFrame, rows, columns }) {
-
- let _rowIndexes;
- let _columnIndexes;
-
- const _data = ndFrame.values;
- const _index = ndFrame.index;
-
- if (rows instanceof Series) {
- rows = rows.values;
- }
-
- if (rows !== undefined && !Array.isArray(rows)) {
- throw new Error(`rows parameter must be an Array. For example: rows: [1,2] or rows: ["0:10"]`);
- }
-
- if (columns !== undefined && !Array.isArray(columns)) {
- throw new Error(`columns parameter must be an Array. For example: columns: ["a","b"] or columns: ["a:c"]`);
- }
-
- if (!rows) {
- _rowIndexes = _index.map((indexValue) => _index.indexOf(indexValue)); // Return all row index
-
- } else if (rows.length === 1 && typeof rows[0] === "string") {
-
- if (rows[0].indexOf(":") === -1) { // Input type ==> ["1"] or [`"1"`]
- let temp;
- if (rows[0].startsWith(`"`) || rows[0].startsWith(`'`) || rows[0].startsWith("`")) {
- temp = _index.indexOf(rows[0].replace(/['"`]/g, ''));
- } else if (typeof rows[0] === "string") {
- temp = _index.indexOf(rows[0]);
- } else {
- temp = _index.indexOf(Number(rows[0]));
- }
-
- if (temp === -1) {
- throw new Error(`IndexError: Specified index (${rows[0]}) not found`);
- }
-
- _rowIndexes = [temp];
-
- } else {
- // Input type ==> ["1:2"] or [`"1":"4"`]
- const rowSplit = rows[0].split(":");
-
- if (rowSplit.length != 2) {
- throw new Error(`Invalid row split parameter: If using row split string, it must be of the form; rows: ["start:end"]`);
- }
-
- let start;
- let end;
-
- if (rowSplit[0] === "") {
- start = _index.indexOf(_index[0]);
- } else {
- if (rowSplit[0].startsWith(`"`) || rowSplit[0].startsWith(`'`) || rowSplit[0].startsWith("`")) {
- start = _index.indexOf(rowSplit[0].replace(/['"`]/g, ''));
- } else {
- start = _index.indexOf(Number(rowSplit[0]));
- }
- }
-
- if (rowSplit[1] === "") {
- end = _index.indexOf(_index[_index.length - 1]) + 1;
- } else {
- if (rowSplit[1].startsWith(`"`) || rowSplit[1].startsWith(`'`) || rowSplit[1].startsWith("`")) {
- end = _index.indexOf(rowSplit[1].replace(/['"`]/g, ''));
- } else {
- end = _index.indexOf(Number(rowSplit[1]));
- }
-
- }
-
- if (start === -1) {
- throw new Error(`IndexError: Specified start index not found`);
- }
-
- if (end === -1) {
- throw new Error(`IndexError: Specified end index not found`);
- }
-
- _rowIndexes = _index.slice(start, end).map((indexValue) => _index.indexOf(indexValue));
- }
-
- } else {
- // Input type ==> ["1", "2"] or [1, 5] or [true, false]
- const rowsIndexToUse = [];
- for (let i = 0; i < rows.length; i++) {
- const isBoolean = typeof rows[i] === "boolean";
- if (isBoolean && rows[i]) {
- rowsIndexToUse.push(_index.indexOf(_index[i]));
- }
-
- if (!isBoolean) {
- const rowIndex = _index.indexOf(rows[i]);
- if (rowIndex === -1) {
- throw new Error(`IndexError: Specified index (${rows[i]}) not found`);
- }
- rowsIndexToUse.push(rowIndex);
- }
- }
-
- _rowIndexes = rowsIndexToUse;
- }
-
- const _columnNames = ndFrame.columns;
-
- if (!columns) {
- _columnIndexes = _columnNames.map((columnName) => _columnNames.indexOf(columnName));// Return all column index
-
- } else if (columns.length == 1) {
- if (typeof columns[0] !== "string") {
- throw new Error(`ColumnIndexError: columns parameter must be an array of a string name. For example: columns: ["b"]`);
- }
-
- if (columns[0].indexOf(":") == -1) { // Input type ==> ["A"]
- _columnIndexes = [_columnNames.indexOf(columns[0])];
-
- } else { // Input type ==> ["a:b"] or [`"col1":"col5"`]
- const columnSplit = columns[0].split(":");
-
- if (columnSplit.length != 2) {
- throw new Error(`ColumnIndexError: Invalid row split parameter. If using row split string, it must be of the form; rows: ["start:end"]`);
- }
-
- const start = columnSplit[0] == "" ? _columnNames.indexOf(_columnNames[0]) : _columnNames.indexOf(columnSplit[0]);
- const end = columnSplit[1] == "" ? _columnNames.indexOf(_columnNames[_columnNames.length - 1]) : _columnNames.indexOf(columnSplit[1]);
-
- if (start === -1) {
- throw new Error(`ColumnIndexError: Specified start index not found`);
- }
-
- if (end === -1) {
- throw new Error(`ColumnIndexError: Specified end index not found`);
- }
- _columnIndexes = _columnNames.slice(start, end + 1).map((columnName) => _columnNames.indexOf(columnName));
- _columnIndexes.pop(); //Remove the last element
-
- }
- } else { // Input type ==> ["A", "B"] or ["col1", "col2"]
- for (let i = 0; i < columns.length; i++) {
- if (_columnNames.indexOf(columns[i]) === -1) {
- throw new Error(`ColumnIndexError: Specified column (${columns[i]}) not found`);
- }
- }
- _columnIndexes = columns.map((columnName) => _columnNames.indexOf(columnName));
- }
-
- if (ndFrame instanceof Series) {
- const newData = [];
- const newIndex = [];
-
- for (let i = 0; i < _rowIndexes.length; i++) {
- const rowIndx = _rowIndexes[i];
- newData.push(_data[rowIndx]);
- newIndex.push(_index[rowIndx]);
- }
- const sf = new Series(
- newData,
- {
- index: newIndex,
- columns: ndFrame.columns,
- dtypes: ndFrame.dtypes,
- config: ndFrame.config
- });
-
- return sf;
- } else {
- const newData = [];
- const newIndex = [];
- const newColumnNames = [];
- const newDtypes = [];
-
- for (let i = 0; i < _rowIndexes.length; i++) {
- const rowIndx = _rowIndexes[i];
- const rowData = _data[rowIndx];
- const newRowDataWithRequiredCols = [];
-
- for (let j = 0; j < _columnIndexes.length; j++) {
- const colIndx = _columnIndexes[j];
- newRowDataWithRequiredCols.push(rowData[colIndx]);
- }
- newData.push(newRowDataWithRequiredCols);
- newIndex.push(_index[rowIndx]);
- }
-
- for (let i = 0; i < _columnIndexes.length; i++) {
- const colIndx = _columnIndexes[i];
- newColumnNames.push(ndFrame.columns[colIndx]);
- newDtypes.push(ndFrame.dtypes[colIndx]);
-
- }
-
- const df = new DataFrame(
- newData,
- {
- index: newIndex,
- columns: newColumnNames,
- dtypes: newDtypes,
- config: ndFrame.config
- });
- return df;
-
- }
-
-}
diff --git a/danfojs-browser/src/core/math.ops.js b/danfojs-browser/src/core/math.ops.js
deleted file mode 100644
index b6b3f26f..00000000
--- a/danfojs-browser/src/core/math.ops.js
+++ /dev/null
@@ -1,163 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import Series from "./series";
-import { utils } from "../shared/utils";
-
-// const utils = new Utils();
-
-/**
- * Generic function for performing add, sub, pow, mul and mod operation on a series
- * @param object
- *
- * ndframe ==> The current Series
- *
- * other ==> The Series or number to perform math operation with
- *
- * operation ==> The type of operation to perform
-*/
-export function _genericMathOp({ ndFrame, other, operation }) {
- if (typeof other === 'number') {
- //broadcast operation
- let newData;
- switch (operation) {
- case 'add':
- newData = (ndFrame.values).map(((ele) => ele + other));
- return newData;
-
- case 'sub':
- newData = (ndFrame.values).map(((ele) => ele - other));
- return newData;
-
- case 'mul':
- newData = (ndFrame.values).map(((ele) => ele * other));
- return newData;
-
- case 'div':
- newData = (ndFrame.values).map(((ele) => ele / other));
- return newData;
-
- case 'mod':
- newData = (ndFrame.values).map(((ele) => ele % other));
- return newData;
-
- case 'pow':
- newData = (ndFrame.values).map(((ele) => ele ** other));
- return newData;
-
- case 'minimum':
- newData = (ndFrame.values).map(((ele) => Math.min(ele, other)));
- return newData;
-
- case 'maximum':
- newData = (ndFrame.values).map(((ele) => Math.max(ele, other)));
- return newData;
-
- default:
- throw new Error(`${operation} is not implemented`);
-
- }
- } else if (other instanceof Series) {
- utils.checkSeriesOpCompactibility({ firstSeries: ndFrame, secondSeries: other, operation });
-
- let newData;
- switch (operation) {
- case 'add':
-
- newData = (ndFrame.values).map((ele, index) => { return ele + (other.values)[index]; });
- return newData;
-
- case 'sub':
-
- newData = (ndFrame.values).map((ele, index) => { return ele - (other.values)[index]; });
- return newData;
-
- case 'mul':
-
- newData = (ndFrame.values).map((ele, index) => { return ele * (other.values)[index]; });
- return newData;
-
- case 'div':
-
- newData = (ndFrame.values).map((ele, index) => { return ele / (other.values)[index]; });
- return newData;
-
- case 'mod':
-
- newData = (ndFrame.values).map((ele, index) => { return ele % (other.values)[index]; });
- return newData;
-
- case 'pow':
- newData = (ndFrame.values).map((ele, index) => { return ele ** (other.values)[index]; });
- return newData;
-
- case 'minimum':
- newData = (ndFrame.values).map((ele, index) => { return Math.min(ele, (other.values)[index]); });
- return newData;
-
- case 'maximum':
- newData = (ndFrame.values).map((ele, index) => { return Math.max(ele, (other.values)[index]); });
- return newData;
-
- default:
- throw new Error(`${operation} is not implemented`);
- }
- } else if (Array.isArray(other)) {
- if (other.length !== ndFrame.values.length){
- throw new Error(`ParamError: Length of array must be equal to length of Series`);
- }
- let newData;
- switch (operation) {
- case 'add':
-
- newData = (ndFrame.values).map((ele, index) => { return ele + (other)[index]; });
- return newData;
-
- case 'sub':
-
- newData = (ndFrame.values).map((ele, index) => { return ele - (other)[index]; });
- return newData;
-
- case 'mul':
-
- newData = (ndFrame.values).map((ele, index) => { return ele * (other)[index]; });
- return newData;
-
- case 'div':
-
- newData = (ndFrame.values).map((ele, index) => { return ele / (other)[index]; });
- return newData;
-
- case 'mod':
-
- newData = (ndFrame.values).map((ele, index) => { return ele % (other)[index]; });
- return newData;
-
- case 'pow':
- newData = (ndFrame.values).map((ele, index) => { return ele ** (other)[index]; });
- return newData;
-
- case 'minimum':
- newData = (ndFrame.values).map((ele, index) => { return Math.min(ele, (other)[index]); });
- return newData;
-
- case 'maximum':
- newData = (ndFrame.values).map((ele, index) => { return Math.max(ele, (other)[index]); });
- return newData;
-
- }
- } else {
- throw new Error("ParamError: value for other not supported. It must be either a scalar, Array or Series");
- }
-}
diff --git a/danfojs-browser/src/core/merge.js b/danfojs-browser/src/core/merge.js
deleted file mode 100644
index 61e8c503..00000000
--- a/danfojs-browser/src/core/merge.js
+++ /dev/null
@@ -1,328 +0,0 @@
-import DataFrame from './frame';
-import { utils } from "../shared/utils";
-
-export class Merge {
-
- constructor(kwargs) {
-
- //check if keys exist in kwargs
- utils.keyInObject(kwargs, "left");
- utils.keyInObject(kwargs, "right");
- utils.keyInObject(kwargs, "on");
- // utils.keyInObject(kwargs, "how")
- if (!utils.keyInObject(kwargs, "how")){
- this.how = 'inner';
- kwargs['how'] = 'inner';
- } else {
- this.how = kwargs['how'];
- }
-
- this.left = null;
- this.right = null;
- this.on = null;
- // this.how = null;
-
- let how_keys = [ "outer", "inner", "left", "right" ];
-
- if ((kwargs["left"] instanceof DataFrame) && (kwargs["right"] instanceof DataFrame)) {
- this.left = kwargs["left"];
- this.right = kwargs["right"];
- } else {
- throw new Error("The left and right key value must be a dataFrame");
- }
-
- if (Array.isArray(kwargs["on"])) {
- this.on = kwargs["on"];
- } else {
- throw new Error("key 'on' must be a list");
- }
-
- if (how_keys.includes(kwargs["how"])) {
-
- this.how = kwargs["how"];
- } else {
- throw new Error(`${kwargs["how"]} specify in keyword how is not recognise`);
- }
-
- this.left_col_index = [];
- this.right_col_index = [];
-
- //find the index of the columns
- for (let i = 0; i < this.on.length; i++) {
-
- if (this.left.columns.includes(this.on[i]) && this.right.columns.includes(this.on[i])) {
-
- let left_index = this.left.columns.indexOf(this.on[i]);
- let right_index = this.right.columns.indexOf(this.on[i]);
-
- this.left_col_index.push(left_index);
- this.right_col_index.push(right_index);
- }
- }
-
- this.left_key_dict = {};
- this.right_key_dict = {};
-
- let left_values = this.left.values;
- let right_values = this.right.values;
-
-
- /**
- * Create a dictionary for both left and right dataframe
- containing the key combination of columns used as keys, and
- the value of such keys combination are the rows having this
- keys combination.
- */
- for (let i = 0; i < left_values.length; i++) {
- let left_value = left_values[i];
- let left_key_comb_values = [];
-
- //get the value in the column index
- for (let j = 0; j < this.left_col_index.length; j++) {
- let index = this.left_col_index[j];
-
- left_key_comb_values.push(left_value[index]);
- }
- //combine into single hashable string
- let left_key_comb = left_key_comb_values.join('_');
-
- let self = this; // assign the this scope to self
- //filter out the value that are not the column key
- let left_value_filter = left_value.filter(function (val, index) {
- return !self.left_col_index.includes(index);
- });
-
- //check if the key combination already exist or not
- //before storing the key combination and the value
- //associated with it
- if (utils.keyInObject(this.left_key_dict, left_key_comb)) {
- this.left_key_dict[left_key_comb].filters.push(left_value_filter);
- } else {
- this.left_key_dict[left_key_comb] = {
- filters: [left_value_filter],
- comb_values: left_key_comb_values
- };
- }
-
- }
- for (let i = 0; i < right_values.length; i++) {
- let right_value = right_values[i];
- let right_key_comb_values = [];
-
- for (let j = 0; j < this.right_col_index.length; j++) {
- let index = this.right_col_index[j];
-
- right_key_comb_values.push(right_value[index]);
- }
- let right_key_comb = right_key_comb_values.join('_');
-
- let self = this;
- let right_value_filter = right_value.filter(function (val, index) {
- return !self.right_col_index.includes(index);
- });
-
- if (utils.keyInObject(this.right_key_dict, right_key_comb)) {
- this.right_key_dict[right_key_comb].filters.push(right_value_filter);
- } else {
- this.right_key_dict[right_key_comb] = {
- filters: [right_value_filter],
- comb_values: right_key_comb_values
- };
- }
- }
-
-
- //create column
- this.__create_columns();
-
- let data = null;
- switch (this.how) {
-
- case "outer":
- data = this.outer();
- break;
- case "inner":
- data = this.inner();
- break;
- case "left":
- data = this.left_merge();
- break;
- case "right":
- data = this.right_merge();
- break;
- }
-
- // eslint-disable-next-line no-self-assign
- let df = new DataFrame(data = data, { columns: this.columns });
-
- return df;
- }
-
- __create_columns() {
-
- let self = this;
- self.left_col = self.left.columns.filter((val, index) => {
- return !self.left_col_index.includes(index);
- });
-
- self.right_col = self.right.columns.filter((val, index) => {
- return !self.right_col_index.includes(index);
- });
-
- self.columns = [ ...self.on ];
- let column_duplicate = {};
-
- let temp_column = [ ...self.left_col ];
- temp_column.push(...self.right_col);
-
- for (let i = 0; i < temp_column.length; i++) {
-
- let col = temp_column[i];
- if (utils.keyInObject(column_duplicate, col)) {
-
- let col_name = `${col}_${column_duplicate[col]}`;
- self.columns.push(col_name);
-
- column_duplicate[col] += 1;
- } else {
- self.columns.push(col);
- column_duplicate[col] = 1;
- }
- }
- }
-
- outer() {
-
- let keys = Object.keys(this.left_key_dict); // obtain the keys of the left dataframe
-
- keys.push(...Object.keys(this.right_key_dict));
-
- keys = Array.from(new Set(keys)); // obtain the unique keys
-
- let data = this.basic(keys);
-
- return data;
-
- }
-
- inner() {
-
- let left_keys = Object.keys(this.left_key_dict);
- let right_keys = Object.keys(this.right_key_dict);
-
- let keys = left_keys.filter((val) => {
- return right_keys.includes(val);
- });
-
- let data = this.basic(keys);
-
- return data;
- }
-
- left_merge() {
-
- let keys = Object.keys(this.left_key_dict);
- let data = this.basic(keys);
-
- return data;
-
- }
-
- right_merge() {
- let keys = Object.keys(this.right_key_dict);
- let data = this.basic(keys);
-
- return data;
- }
-
- basic(keys) {
-
- let data = [];
-
- for (let i = 0; i < keys.length; i++) {
- let key = keys[i];
-
- let key_array = key.split("_").filter((val) => {
- return val != "";
- });
-
- if (utils.keyInObject(this.left_key_dict, key)) {
- let left_row = this.left_key_dict[key].filters;
- let key_array = this.left_key_dict[key].comb_values;
-
-
- for (let left_i = 0; left_i < left_row.length; left_i++) {
-
- let left_row_row = left_row[left_i];
-
- if (utils.keyInObject(this.right_key_dict, key)) {
-
- let right_row = this.right_key_dict[key].filters;
-
- for (let r_i = 0; r_i < right_row.length; r_i++) {
-
- let right_row_row = right_row[r_i];
-
-
- let inner_data = key_array.slice(0);
- inner_data.push(...left_row_row);
- inner_data.push(...right_row_row);
-
-
- data.push(inner_data);
- }
- } else {
- let nan_array = Array(this.right_col.length);
-
- for (let i = 0; i < this.right_col.length; i++) {
- nan_array[i] = NaN;
- }
-
- let inner_data = key_array.slice(0);
- inner_data.push(...left_row_row);
- inner_data.push(...nan_array);
- data.push(inner_data);
- }
- }
- } else {
-
- let right_row = this.right_key_dict[key].filters;
- let key_array = this.right_key_dict[key].comb_values;
-
- for (let i = 0; i < right_row.length; i++) {
-
- let right_row_row = right_row[i];
-
- let nan_array = Array(this.left_col.length);
-
- for (let j = 0; j < nan_array.length; j++) {
- nan_array[j] = NaN;
- }
-
- let inner_data = key_array.slice(0);
- inner_data.push(...nan_array);
- inner_data.push(...right_row_row);
- data.push(inner_data);
- }
- }
- }
- return data;
-
- }
-}
-
-
-/**
-* Merge DataFrame or named Series objects with a database-style join.
-* The join is done on columns or indexes. If joining columns on columns, the DataFrame indexes will be ignored. Otherwise
-* if joining indexes on indexes or indexes on a column or columns, the index will be passed on.
-* @param {kwargs}{left: DataFrame;
-* right: DataFrame | Named Series, Object to merge with;
-* on: label, Column or index level names to join on. These must be found in both DataFrames;
-* how: {‘left’, ‘right’, ‘outer’, ‘inner’}. Type of merge to be performed.
-* @returns {DataFrame}
-*/
-export const merge = (kwargs) => {
- let merge = new Merge(kwargs);
- return merge;
-};
diff --git a/danfojs-browser/src/core/series.js b/danfojs-browser/src/core/series.js
deleted file mode 100644
index 94e40313..00000000
--- a/danfojs-browser/src/core/series.js
+++ /dev/null
@@ -1,1362 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { variance, std, median, mode } from 'mathjs';
-import { _genericMathOp } from "./math.ops";
-import { DATA_TYPES } from '../shared/defaults';
-import ErrorThrower from "../shared/errors";
-import { _iloc, _loc } from "./indexing";
-import { utils } from "../shared/utils";
-import NDframe from "./generic";
-import { table } from "table";
-import Str from './strings';
-import Dt from './datetime';
-import dummyEncode from "./get_dummies";
-import { Plot } from "../plotting/plot";
-import { data as tfData } from "@tensorflow/tfjs";
-
-// const utils = new Utils();
-
-/**
- * One-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.
- */
-export default class Series extends NDframe {
-
- constructor(data = [], options) {
- const { index, columns, dtypes, config } = { index: undefined, columns: undefined, dtypes: undefined, config: undefined, ...options };
- if (Array.isArray(data[0]) || utils.isObject(data[0])) {
- data = utils.convert2DArrayToSeriesArray(data);
- super({
- data,
- index,
- columns,
- dtypes,
- config,
- isSeries: true
- });
- } else {
- super({
- data,
- index,
- columns,
- dtypes,
- config,
- isSeries: true
- });
- }
- }
-
- /**
- * 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
- *
- * 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.
- */
- iloc(rows) {
- return _iloc({ ndFrame: this, rows });
- }
-
- /**
- * Access a group of rows 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
- *
- * 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)
- */
- loc(rows) {
- return _loc({ ndFrame: this, rows });
- }
-
- /**
- * Returns the first n values in a Series
- * @param rows The number of rows to return
- */
- head(rows = 5) {
- return this.iloc([`0:${rows}`]);
- }
-
- /**
- * Returns the last n values in a Series
- * @param rows The number of rows to return
- */
- tail(rows = 5) {
- const startIdx = this.shape[0] - rows;
- return this.iloc([`${startIdx}:`]);
- }
-
- /**
- * Returns specified number of random rows in a Series
- * @param num The number of rows to return
- * @param options.seed An integer specifying the random seed that will be used to create the distribution.
- */
- async sample(num = 5, options) {
- const { seed } = { seed: 1, ...options };
-
- if (num > this.shape[0]) {
- throw new Error("Sample size n cannot be bigger than size of dataset");
- }
- if (num < -1 || num == 0) {
- throw new Error("Sample size cannot be less than -1 or be equal to 0");
- }
- num = num === -1 ? this.shape[0] : num;
-
- const shuffledIndex = await tfData.array(this.index).shuffle(num, `${seed}`).take(num).toArray();
- const sf = this.iloc(shuffledIndex);
- return sf;
- }
-
- /**
- * Return Addition of series and other, element-wise (binary operator add).
- * Equivalent to series + other
- * @param other Series, Array of same length or scalar number to add
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- add(other, options) {
- const { inplace } = { inplace: false, ...options };
-
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError("add");
-
- const newData = _genericMathOp({ ndFrame: this, other, operation: "add" });
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- return utils.createNdframeFromNewDataWithOldProps({ ndFrame: this, newData, isSeries: true });
- }
- }
-
- /**
- * Returns the subtraction between a series and other, element-wise (binary operator subtraction).
- * Equivalent to series - other
- * @param other Number to subtract
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- sub(other, options) {
- const { inplace } = { inplace: false, ...options };
-
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError("sub");
-
- const newData = _genericMathOp({ ndFrame: this, other, operation: "sub" });
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- return utils.createNdframeFromNewDataWithOldProps({ ndFrame: this, newData, isSeries: true });
- }
-
- }
-
- /**
- * Return Multiplication of series and other, element-wise (binary operator mul).
- * Equivalent to series * other
- * @param other Number to multiply with.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- mul(other, options) {
- const { inplace } = { inplace: false, ...options };
-
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError("mul");
-
- const newData = _genericMathOp({ ndFrame: this, other, operation: "mul" });
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- return utils.createNdframeFromNewDataWithOldProps({ ndFrame: this, newData, isSeries: true });
- }
- }
-
- /**
- * Return division of series and other, element-wise (binary operator div).
- * Equivalent to series / other
- * @param other Series or number to divide with.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- div(other, options) {
- const { inplace } = { inplace: false, ...options };
-
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError("div");
-
- const newData = _genericMathOp({ ndFrame: this, other, operation: "div" });
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- return utils.createNdframeFromNewDataWithOldProps({ ndFrame: this, newData, isSeries: true });
- }
- }
-
- /**
- * Return Exponential power of series and other, element-wise (binary operator pow).
- * Equivalent to series ** other
- * @param other Number to multiply with.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- pow(other, options) {
- const { inplace } = { inplace: false, ...options };
-
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError("pow");
-
- const newData = _genericMathOp({ ndFrame: this, other, operation: "pow" });
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- return utils.createNdframeFromNewDataWithOldProps({ ndFrame: this, newData, isSeries: true });
- }
- }
-
- /**
- * Return Modulo of series and other, element-wise (binary operator mod).
- * Equivalent to series % other
- * @param other Number to modulo with
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- mod(other, options) {
- const { inplace } = { inplace: false, ...options };
-
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError("mod");
-
- const newData = _genericMathOp({ ndFrame: this, other, operation: "mod" });
-
- if (inplace) {
- this.$setValues(newData);
- } else {
- return utils.createNdframeFromNewDataWithOldProps({ ndFrame: this, newData, isSeries: true });
- }
- }
-
- /**
- * Checks if the array value passed has a compatible dtype, removes NaN values, and if
- * boolean values are present, converts them to integer values.
- * */
- $checkAndCleanValues(values, operation) {
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError(operation);
- values = utils.removeMissingValuesFromArray(values);
-
- if (this.dtypes[0] == "boolean") {
- values = (utils.mapBooleansToIntegers(values, 1));
- }
- return values;
- }
-
- /**
- * Returns the mean of elements in Series
- */
- mean() {
- const values = this.$checkAndCleanValues(this.values, "mean");
- return (values.reduce((a, b) => a + b) / values.length);
- }
-
-
- /**
- * Returns the median of elements in Series
- */
- median() {
- const values = this.$checkAndCleanValues(this.values, "median");
- return median(values);
- }
-
- /**
- * Returns the modal value of elements in Series
- */
- mode() {
- const values = this.$checkAndCleanValues(this.values, "mode");
- return mode(values);
- }
-
- /**
- * Returns the minimum value in a Series
- */
- min() {
- const values = this.$checkAndCleanValues(this.values, "min");
- let smallestValue = values[0];
- for (let i = 0; i < values.length; i++) {
- smallestValue = smallestValue < values[i] ? smallestValue : values[i];
- }
- return smallestValue;
- }
-
- /**
- * Returns the maximum value in a Series
- * @returns {Number}
- */
- max() {
- const values = this.$checkAndCleanValues(this.values, "max");
- let biggestValue = values[0];
- for (let i = 0; i < values.length; i++) {
- biggestValue = biggestValue > values[i] ? biggestValue : values[i];
- }
- return biggestValue;
- }
-
- /**
- * Return the sum of the values in a series.
- */
- sum() {
- const values = this.$checkAndCleanValues(this.values, "sum");
- return values.reduce((sum, value) => sum + value, 0);
- }
-
- /**
- * Return number of non-null elements in a Series
- */
- count() {
- const values = utils.removeMissingValuesFromArray(this.values);
- return values.length;
- }
-
- /**
- * Return maximum of series and other.
- * @param other Series or number to check against
- */
- maximum(other) {
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError("maximum");
-
- const newData = _genericMathOp({ ndFrame: this, other, operation: "maximum" });
- return new Series(newData, {
- columns: this.columns,
- index: this.index
- });
- }
-
- /**
- * Return minimum of series and other.
- * @param other Series, Numbers to check against
- */
- minimum(other) {
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError("maximum");
-
- const newData = _genericMathOp({ ndFrame: this, other, operation: "minimum" });
- return new Series(newData, {
- columns: this.columns,
- index: this.index
- });
- }
-
- /**
- * Round each value in a Series to the specified number of decimals.
- * @param dp Number of Decimal places to round to
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- round(dp = 1, options) {
- const { inplace } = { inplace: false, ...options };
-
- const newValues = utils.round(this.values, dp, true);
-
- if (inplace) {
- this.$setValues(newValues);
- } else {
- return utils.createNdframeFromNewDataWithOldProps({
- ndFrame: this,
- newData: newValues,
- isSeries: true
- });
- }
-
- }
-
- /**
- * Return sample standard deviation of elements in Series
- */
- std() {
- const values = this.$checkAndCleanValues(this.values, "max");
- return std(values);
- }
-
- /**
- * Return unbiased variance of elements in a Series.
- */
- var() {
- const values = this.$checkAndCleanValues(this.values, "max");
- return variance(values);
- }
-
- /**
- * Return a boolean same-sized object indicating where elements are NaN.
- * NaN and undefined values gets mapped to true, and everything else gets mapped to false.
- */
- isna() {
- const newData = this.values.map((value) => {
- if (isNaN(value) && typeof value != "string") {
- return true;
- } else {
- return false;
- }
- });
- const sf = new Series(newData,
- {
- index: this.index,
- dtypes: ["boolean"],
- config: this.config
- });
- return sf;
- }
-
- /**
- * Replace all NaN with a specified value
- * @param value The value to replace NaN with
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- fillna(options) {
- const { value, inplace } = { value: undefined, inplace: false, ...options };
-
- if (!value && typeof value !== 'boolean') {
- throw Error('Value Error: Must specify value to replace with');
- }
-
- const newValues = [];
- (this.values).forEach((val) => {
- if (isNaN(val) && typeof val != "string") {
- newValues.push(value);
- } else {
- newValues.push(val);
- }
- });
-
- if (inplace) {
- this.$setValues(newValues);
- } else {
- return utils.createNdframeFromNewDataWithOldProps({
- ndFrame: this,
- newData: newValues,
- isSeries: true
- });
- }
- }
-
-
- /**
- * Sort a Series in ascending or descending order by some criterion.
- * @param options Method options
- * @param ascending Whether to return sorted values in ascending order or not. Defaults to true
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- sort_values(options) {
- const { ascending, inplace } = { inplace: false, ascending: true, ...options };
-
- let sortedValues = [];
- const rangeIdx = utils.range(0, this.index.length - 1);
- let sortedIdx = utils.sortArrayByIndex(rangeIdx, this.values, this.dtypes[0]);
-
- for (let indx of sortedIdx) {
- sortedValues.push(this.values[indx]);
- }
-
- if (ascending) {
- sortedValues = sortedValues.reverse();
- sortedIdx = sortedIdx.reverse();
- }
-
- if (inplace) {
- this.$setValues(sortedValues);
- this.$setIndex(sortedIdx);
- } else {
- const sf = new Series(sortedValues, {
- index: sortedIdx,
- dtypes: this.dtypes,
- config: this.config
- });
- return sf;
-
- }
- }
-
-
- /**
- * Makes a deep copy of a Series
- */
- copy() {
- const sf = new Series([...this.values], {
- columns: [...this.columns],
- index: [...this.index],
- dtypes: [...this.dtypes],
- config: { ...this.config }
- });
- return sf;
- }
-
-
- /**
- * Generate descriptive statistics.
- * Descriptive statistics include those that summarize the central tendency,
- * dispersion and shape of a dataset’s distribution, excluding NaN values.
- */
- describe() {
- if (this.dtypes[0] == "string") {
- throw new Error("DType Error: Cannot generate descriptive statistics for Series with string dtype");
- } else {
-
- const index = ['count', 'mean', 'std', 'min', 'median', 'max', 'variance'];
- const count = this.count();
- const mean = this.mean();
- const std = this.std();
- const min = this.min();
- const median = this.median();
- const max = this.max();
- const variance = this.var();
-
- const data = [count, mean, std, min, median, max, variance];
- const sf = new Series(data, { index: index });
- return sf;
-
- }
- }
-
-
- /**
- * Returns Series with the index reset.
- * This is useful when index is meaningless and needs to be reset to the default before another operation.
- */
- reset_index(options) {
- const { inplace } = { inplace: false, ...options };
-
- if (inplace) {
- this.$resetIndex();
- } else {
- const sf = this.copy();
- sf.$resetIndex();
- return sf;
- }
- }
-
- /**
- * Set the Series index (row labels) using an array of the same length.
- * @param index Array of new index values,
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- set_index(options) {
- const { index, inplace } = { index: undefined, inplace: false, ...options };
-
- if (!index) {
- throw Error('Param Error: Must specify index array');
- }
-
- if (inplace) {
- this.$setIndex(index);
- } else {
- const sf = this.copy();
- sf.$setIndex(index);
- return sf;
- }
- }
-
-
- /**
- * map all the element in a column to a variable or function
- * @param callable callable can either be a funtion or an object
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- map(callable, options) {
- const { inplace } = { inplace: false, ...options };
-
- const isCallable = utils.isFunction(callable);
-
- const data = this.values.map((val) => {
- if (isCallable) {
- return callable(val);
- } else if (utils.isObject(callable)) {
- if (val in callable) {
- return callable[val];
- } else {
- return NaN;
- }
- } else {
- throw new Error("Param Error: callable must either be a function or an object");
- }
- });
-
- if (inplace) {
- this.$setValues(data);
- } else {
- const sf = this.copy();
- sf.$setValues(data);
- return sf;
- }
- }
-
- /**
- * Applies a function to each element of a Series
- * @param callable Function to apply to each element of the series
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- apply(callable, options) {
- const { inplace } = { inplace: false, ...options };
-
- const isCallable = utils.isFunction(callable);
- if (!isCallable) {
- throw new Error("Param Error: callable must be a function");
- }
-
- const data = this.values.map((val) => {
- return callable(val);
- });
-
- if (inplace) {
- this.$setValues(data);
- } else {
- const sf = this.copy();
- sf.$setValues(data);
- return sf;
- }
- }
-
- /**
- * Returns a Series with only the unique value(s) in the original Series
- */
- unique() {
- const newValues = new Set(this.values);
- let series = new Series(Array.from(newValues));
- return series;
- }
-
- /**
- * Return the number of unique elements in a Series
- */
- nunique() {
- return (new Set(this.values)).size;
- }
-
- /**
- * Returns unique values and their counts in a Series
- */
- value_counts() {
- const sData = this.values;
- const dataDict = {};
- for (let i = 0; i < sData.length; i++) {
- const val = sData[i];
- if (`${val}` in dataDict) {
- dataDict[`${val}`] = dataDict[`${val}`] + 1;
- } else {
- dataDict[`${val}`] = 1;
- }
- }
-
- const index = Object.keys(dataDict).map((x) => {
- return parseInt(x) ? parseInt(x) : x;
- });
- const data = Object.values(dataDict);
-
- const series = new Series(data, { index: index });
- return series;
-
- }
-
- /**
- * Returns the absolute values in Series
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- abs(options) {
- const { inplace } = { inplace: false, ...options };
-
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError("abs");
- let newValues;
-
-
- newValues = this.values.map((val) => Math.abs(val));
-
- if (inplace) {
- this.$setValues(newValues);
- } else {
- const sf = this.copy();
- sf.$setValues(newValues);
- return sf;
- }
- }
-
- /**
- * Returns the cumulative sum over a Series
- */
- cumsum(options) {
- const ops = { inplace: false, ...options };
- return this.cumOps("sum", ops);
- }
-
- /**
- * Returns cumulative minimum over a Series
- */
- cummin(options) {
- const ops = { inplace: false, ...options };
- return this.cumOps("min", ops);
- }
-
-
- /**
- * Returns cumulative maximum over a Series
- */
- cummax(options) {
- const ops = { inplace: false, ...options };
- return this.cumOps("max", ops);
- }
-
- /**
- * Returns cumulative product over a Series
- */
- cumprod(options) {
- const ops = { inplace: false, ...options };
- return this.cumOps("prod", ops);
- }
-
- /**
- * perform cumulative operation on series data
- */
- cumOps(ops, options) {
- if (this.dtypes[0] == "string") ErrorThrower.throwStringDtypeOperationError(ops);
- const { inplace } = options;
-
- const sData = this.values;
- let tempval = sData[0];
- const data = [tempval];
-
- for (let i = 1; i < sData.length; i++) {
- let currVal = sData[i];
- switch (ops) {
- case "max":
- if (currVal > tempval) {
- data.push(currVal);
- tempval = currVal;
- } else {
- data.push(tempval);
- }
- break;
- case "min":
- if (currVal < tempval) {
- data.push(currVal);
- tempval = currVal;
- } else {
- data.push(tempval);
- }
- break;
- case "sum":
- tempval = (tempval) + (currVal);
- data.push(tempval);
- break;
- case "prod":
- tempval = (tempval) * (currVal);
- data.push(tempval);
- break;
-
- }
- }
-
- if (inplace) {
- this.$setValues(data);
- } else {
- return new Series(data, {
- index: this.index,
- config: { ...this.config }
- });
- }
- }
-
-
- /**
- * Returns less than of series and other. Supports element wise operations
- * @param other Series or number to compare against
- */
- lt(other) {
- return this.boolOps(other, "lt");
- }
-
- /**
- * Returns Greater than of series and other. Supports element wise operations
- * @param {other} Series, Scalar
- * @return {Series}
- */
- gt(other) {
- return this.boolOps(other, "gt");
- }
-
- /**
- * Returns Less than or Equal to of series and other. Supports element wise operations
- * @param {other} Series, Scalar
- * @return {Series}
- */
- le(other) {
- return this.boolOps(other, "le");
- }
-
- /**
- * Returns Greater than or Equal to of series and other. Supports element wise operations
- * @param {other} Series, Scalar
- * @return {Series}
- */
- ge(other) {
- return this.boolOps(other, "ge");
- }
-
- /**
- * Returns Not Equal to of series and other. Supports element wise operations
- * @param {other} Series, Scalar
- * @return {Series}
- */
- ne(other) {
- return this.boolOps(other, "ne");
- }
-
- /**
- * Returns Equal to of series and other. Supports element wise operations
- * @param {other} Series, Scalar
- * @return {Series}
- */
- eq(other) {
- return this.boolOps(other, "eq");
- }
-
- /**
- * Perform boolean operations on bool values
- * @param other Other Series or number to compare with
- * @param bOps Name of operation to perform [ne, ge, le, gt, lt, eq]
- */
- boolOps(other, bOps) {
- const data = [];
- const lSeries = this.values;
- let rSeries;
-
- if (typeof other == "number") {
- rSeries = Array(this.values.length).fill(other); //create array of repeated value for broadcasting
- } else if (typeof other == "string" && ["eq", "ne"].includes(bOps)) {
- rSeries = Array(this.values.length).fill(other);
- } else if (other instanceof Series) {
- rSeries = other.values;
- } else if (Array.isArray(other)) {
- rSeries = other;
- } else {
- throw new Error("ParamError: value for other not supported. It must be either a scalar, Array or Series");
- }
-
- if (!(lSeries.length === rSeries.length)) {
- throw new Error("LengthError: Lenght of other must be equal to length of Series");
- }
-
-
- for (let i = 0; i < lSeries.length; i++) {
- let lVal = lSeries[i];
- let rVal = rSeries[i];
- let bool = null;
- switch (bOps) {
- case "lt":
- bool = lVal < rVal ? true : false;
- data.push(bool);
- break;
- case "gt":
- bool = lVal > rVal ? true : false;
- data.push(bool);
- break;
- case "le":
- bool = lVal <= rVal ? true : false;
- data.push(bool);
- break;
- case "ge":
- bool = lVal >= rVal ? true : false;
- data.push(bool);
- break;
- case "ne":
- bool = lVal !== rVal ? true : false;
- data.push(bool);
- break;
- case "eq":
- bool = lVal === rVal ? true : false;
- data.push(bool);
- break;
- }
- }
-
- return new Series(data, {
- index: this.index,
- config: { ...this.config }
- });
-
- }
-
- /**
- * Replace all occurence of a value with a new value
- * @param oldValue The value you want to replace
- * @param newValue The new value you want to replace the old value with
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- replace(options) {
- const { oldValue, newValue, inplace } = { oldValue: undefined, newValue: undefined, inplace: false, ...options };
-
- if (!oldValue && typeof oldValue !== 'boolean') {
- throw Error(`Params Error: Must specify param 'oldValue' to replace`);
- }
-
- if (!newValue && typeof newValue !== 'boolean') {
- throw Error(`Params Error: Must specify param 'newValue' to replace with`);
- }
-
- const newArr = [...this.values].map((val) => {
- if (val === oldValue) {
- return newValue;
- } else {
- return val;
- }
- });
-
- if (inplace) {
- this.$setValues(newArr);
- } else {
- const sf = this.copy();
- sf.$setValues(newArr);
- return sf;
- }
-
- }
-
- /**
- * Drops all missing values (NaN) from a Series.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- dropna(options) {
- const { inplace } = { inplace: false, ...options };
-
- const oldValues = this.values;
- const oldIndex = this.index;
- const newValues = [];
- const newIndex = [];
- const isNaVals = this.isna().values;
-
- isNaVals.forEach((val, i) => {
- if (!val) {
- newValues.push((oldValues)[i]);
- newIndex.push(oldIndex[i]);
- }
- });
-
- if (inplace) {
- this.$setValues(newValues, false);
- this.$setIndex(newIndex);
- } else {
- const sf = this.copy();
- sf.$setValues(newValues, false);
- sf.$setIndex(newIndex);
- return sf;
- }
-
- }
-
- /**
- * Return the integer indices that would sort the Series.
- * @param ascending boolean true: will sort the Series in ascending order, false: will sort in descending order
- */
- argsort(ascending = true) {
- const sortedIndex = this.sort_values(ascending);
- const sf = new Series(sortedIndex.index);
- return sf;
- }
-
- /**
- * Return int position of the largest value in the Series.
- */
- argmax() {
- return this.tensor.argMax().arraySync();
- }
-
-
- /**
- * Return int position of the smallest value in the Series.
- */
- argmin() {
- return this.tensor.argMin().arraySync();
- }
-
- /**
- * Remove duplicate values from a Series
- * @param keep "first" | "last", which dupliate value to keep. Defaults to "first".
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- drop_duplicates(options) {
- const { keep, inplace } = { keep: "first", inplace: false, ...options };
-
- if (!(["first", "last"].includes(keep))) {
- throw Error(`Params Error: Keep must be one of 'first' or 'last'`);
- }
-
- let dataArr;
- let newArr = [];
- let oldIndex;
- let newIndex = [];
-
- if (keep === "last") {
- dataArr = (this.values).reverse();
- oldIndex = this.index.reverse();
- } else {
- dataArr = (this.values);
- oldIndex = this.index;
- }
-
- dataArr.forEach((val, i) => {
- if (!newArr.includes(val)) {
- newIndex.push(oldIndex[i]);
- newArr.push(val);
- }
- });
-
- if (keep === "last") {
- //re-reversed the array and index to its true order
- newArr = newArr.reverse();
- newIndex = newIndex.reverse();
- }
-
- if (inplace) {
- this.$setValues(newArr, false);
- this.$setIndex(newIndex);
- } else {
- const sf = this.copy();
- sf.$setValues(newArr, false);
- sf.$setIndex(newIndex);
- return sf;
- }
-
- }
-
- /**
- * Cast Series to specified data type
- * @param dtype Data type to cast to. One of [float32, int32, string, boolean, undefined]
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- astype(dtype, options) {
- const { inplace } = { inplace: false, ...options };
-
- if (!dtype) {
- throw Error("Param Error: Please specify dtype to cast to");
- }
-
- if (!(DATA_TYPES.includes(dtype))) {
- throw Error(`dtype ${dtype} not supported. dtype must be one of ${DATA_TYPES}`);
- }
-
- const oldValues = [...this.values];
- const newValues = [];
-
- switch (dtype) {
- case "float32":
- oldValues.forEach((val) => {
- newValues.push(Number(val));
- });
- break;
- case "int32":
- oldValues.forEach((val) => {
- newValues.push(parseInt(val));
- });
- break;
- case "string":
- oldValues.forEach((val) => {
- newValues.push(String(val));
- });
- break;
- case "boolean":
- oldValues.forEach((val) => {
- newValues.push(Boolean(val));
- });
- break;
- case "undefined":
- oldValues.forEach((_) => {
- newValues.push(NaN);
- });
- break;
- default:
- break;
- }
-
- if (inplace) {
- this.$setValues(newValues, false);
- this.$setDtypes([dtype]);
- } else {
- const sf = this.copy();
- sf.$setValues(newValues, false);
- sf.$setDtypes([dtype]);
- return sf;
- }
-
- }
-
- /**
- * Add a new value or values to the end of a Series
- * @param newValues Single value | Array | Series to append to the Series
- * @param index The new index value(s) to append to the Series. Must contain the same number of values as `newValues`
- * as they map `1 - 1`.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- append(
- newValue,
- index,
- options
- ) {
- const { inplace } = { inplace: false, ...options };
-
- if (!newValue && typeof newValue !== "boolean") {
- throw Error("Param Error: newValues cannot be null or undefined");
- }
-
- if (!index) {
- throw Error("Param Error: index cannot be null or undefined");
- }
-
- const newData = [...this.values];
- const newIndx = [...this.index];
-
- if (Array.isArray(newValue) && Array.isArray(index)) {
-
- if (newValue.length !== index.length) {
- throw Error("Param Error: Length of new values and index must be the same");
- }
-
- newValue.forEach((el, i) => {
- newData.push(el);
- newIndx.push(index[i]);
- });
-
- } else if (newValue instanceof Series) {
- const _value = newValue.values;
-
- if (!Array.isArray(index)) {
- throw Error("Param Error: index must be an array");
- }
-
- if (index.length !== _value.length) {
- throw Error("Param Error: Length of new values and index must be the same");
- }
-
- _value.forEach((el, i) => {
- newData.push(el);
- newIndx.push(index[i]);
- });
- } else {
- newData.push(newValue);
- newIndx.push(index);
- }
-
- if (inplace) {
- this.$setValues(newData, false);
- this.$setIndex(newIndx);
- } else {
- const sf = new Series(
- newData,
- {
- index: newIndx,
- columns: this.columns,
- dtypes: this.dtypes,
- config: this.config
- });
-
- return sf;
- }
- }
-
- /**
- * Returns dtype of Series
- */
- get dtype() {
- return this.dtypes[0];
- }
-
- /**
- * Exposes numerous string methods to manipulate Series of type string
- */
- get str() {
- if (this.dtypes[0] == "string") {
- return new Str(this);
- } else {
- throw new Error("Cannot call accessor str on non-string type");
- }
- }
-
- /**
- * Returns time class that exposes different date time method
- */
- get dt() {
- if (this.dtypes[0] == "string") {
- return new Dt(this);
- } else {
- throw new Error("Cannot call accessor dt on non-string type");
- }
- }
-
- /**
- * Prints Series to console as a grid of row and columns.
- */
- toString() {
- const maxRow = this.$config.getMaxRow;
- let indx;
- let values = [];
-
- if (this.shape[0] > maxRow) {
- //slice rows to show [max_rows] rows
- const sfSlice = this.iloc([`0:${maxRow}`]);
-
- indx = sfSlice.index;
- values = sfSlice.values;
-
- } else {
- indx = this.index;
- values = this.values;
- }
-
- const tabledata = values.map((x, i) => [indx[i], x]);
- return table(tabledata);
- }
-
- /**
- * Returns the logical AND between Series and other. Supports element wise operations and broadcasting.
- * @param other Series, Scalar, Array of Scalars
- */
- and(other) {
-
- if (other === undefined) {
- throw new Error("Param Error: other cannot be undefined");
- }
- const newValues = [];
-
- if (other instanceof Series) {
- if (this.dtypes[0] !== other.dtypes[0]) {
- throw new Error("Param Error must be of same dtype");
- }
-
- if (this.shape[0] !== other.shape[0]) {
- throw new Error("Param Error must be of same shape");
- }
- this.values.forEach((val, i) => {
- newValues.push(Boolean(val) && Boolean(other.values[i]));
- });
-
- } else if (Array.isArray(other)) {
-
- this.values.forEach((val, i) => {
- newValues.push(Boolean(val) && Boolean(other[i]));
- });
-
- } else {
-
- this.values.forEach((val) => {
- newValues.push(Boolean(val) && Boolean(other));
- });
-
- }
-
- return new Series(newValues, {
- index: this.index,
- config: { ...this.config }
- });
- }
-
- /**
- * Returns the logical OR between Series and other. Supports element wise operations and broadcasting.
- * @param other Series, Scalar, Array of Scalars
- */
- or(other) {
-
- if (other === undefined) {
- throw new Error("Param Error: other cannot be undefined");
- }
- const newValues = [];
-
- if (other instanceof Series) {
- if (this.dtypes[0] !== other.dtypes[0]) {
- throw new Error("Param Error must be of same dtype");
- }
-
- if (this.shape[0] !== other.shape[0]) {
- throw new Error("Param Error must be of same shape");
- }
-
- this.values.forEach((val, i) => {
- newValues.push(Boolean(val) || (other.values[i]));
- });
-
- } else if (typeof other === "boolean") {
-
- this.values.forEach((val) => {
- newValues.push(Boolean(val) || (other));
- });
-
- } else if (Array.isArray(other)) {
-
- this.values.forEach((val, i) => {
- newValues.push(Boolean(val) || (other[i]));
- });
-
- } else {
- throw new Error("Param Error: other must be a Series, Scalar, or Array of Scalars");
- }
-
- return new Series(newValues, {
- index: this.index,
- config: { ...this.config }
- });
- }
-
- /**
- * One-hot encode values in the Series.
- * @param options Options for the operation. The following options are available:
- * - `prefix`: Prefix to add to the new column. Defaults to unique labels.
- * - `prefixSeparator`: Separator to use for the prefix. Defaults to '_'.
- * @returns A DataFrame with the one-hot encoded columns.
- * @example
- * sf.get_dummies()
- * sf.get_dummies({prefix: 'cat' })
- * sf.get_dummies({ prefix: 'cat', prefixSeparator: '-' })
- */
- get_dummies(options) {
- return dummyEncode(this, options);
- }
-
- /**
- * Make plots of Series or DataFrame.
- * Uses the Plotly as backend, so supports Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @returns {Class} Plot class that expoese different plot type
- */
- plot(div) {
- const plt = new Plot(this, div);
- return plt;
- }
-}
diff --git a/danfojs-browser/src/core/strings.js b/danfojs-browser/src/core/strings.js
deleted file mode 100644
index 5ecb28ff..00000000
--- a/danfojs-browser/src/core/strings.js
+++ /dev/null
@@ -1,739 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-
-/**
- * Exposes numerous String methods. All methods are applied Element-wise
- */
-export default class Str {
- constructor(series) {
- this.series = series;
- this.values = (series.values);
- }
-
- /**
- * Converts all characters to lowercase.
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["GooD", "Bad", "CrAzy"])
- * const newSf = sf.str.toLowerCase()
- * console.log(newSf.values)
- * // ["good", "bad", "crazy"]
- * ```
- */
- toLowerCase(options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.toLowerCase());
- }
-
- });
-
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
-
- }
-
- /**
- * Converts all characters to uppercase.
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["GooD", "Bad", "CrAzy"])
- * const newSf = sf.str.toUpperCase()
- * console.log(newSf.values)
- * // ["GOOD", "BAD", "CRAZY"]
- * ```
- */
- toUpperCase(options){
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.toUpperCase());
- }
-
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
-
- }
- }
-
- /**
- * Capitalize first string
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.capitalize()
- * console.log(newSf.values)
- * // ["Good", "Bad", "Crazy"]
- * ```
- */
- capitalize(options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- let firstChar = `${val}`.slice(0, 1);
- let leftChar = `${val}`.slice(1);
- let newStr = `${firstChar.toUpperCase()}${leftChar.toLowerCase()}`;
- newArr.push(newStr);
- }
-
- });
-
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
-
- }
-
- /**
- * Returns the character at the specified index (position)
- * @param index position of character
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.charAt(1)
- * console.log(newSf.values)
- * // ["o", "a", "r"]
- * ```
- */
- charAt(index = 0, options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.charAt(index));
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Joins specified `other` with values in the Series.
- * @param other string|values to concatenate with.
- * @param position where to concat the string from. O concats from the start, 1 concats from the end. Defaults to 1.
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.concat("_new")
- * console.log(newSf.values)
- * // ["Good_new", "bad_new", "crazy_new"
- * ```
- */
- concat(other, position = 1, options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
-
- if (Array.isArray(other)) {
- for (let i = 0; i < other.length; i++) {
- let leftStr = `${this.values[i]}`;
- let rightStr = `${other[i]}`;
- if (position == 1) {
- newArr.push(leftStr.concat(rightStr));
- } else {
- newArr.push(rightStr.concat(leftStr));
- }
-
- }
- } else {
- this.values.map((val) => {
- if (position == 1) {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.concat(`${other}`));
- }
-
- } else {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(other.concat(`${val}`));
- }
- }
- });
- }
-
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
-
- }
-
-
- /**
- * Checks whether a string begins with specified characters
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.startsWith("G")
- * console.log(newSf.values)
- * // [true, false, false]
- * ```
- */
- startsWith(str = "", options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.forEach((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.startsWith(str));
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Checks whether a string ends with specified characters
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.endsWith("d")
- * console.log(newSf.values)
- * // [true, true, false]
- * ```
- */
- endsWith(str = "", options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.endsWith(str));
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Checks whether a string contains the specified string/characters
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.includes("d")
- * console.log(newSf.values)
- * // [true, true, false]
- * ```
- */
- includes(str = "", options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.includes(str));
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Returns the position of the first occurrence of a specified value in a string.
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.indexOf("d")
- * console.log(newSf.values)
- * // [3, 2, -1]
- * ```
- */
- indexOf(str = "", options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.indexOf(str));
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Returns the position of the last found occurrence of a specified value in a string
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "crazy"])
- * const newSf = sf.str.lastIndexOf("d")
- * console.log(newSf.values)
- * // [3, 2, -1]
- * ```
- */
- lastIndexOf(str = "", options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.lastIndexOf(str));
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
-
- /**
- * Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced
- * @param searchValue String | Character value to replace
- * @param replaceValue String | Character string to replace with
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "crazy"])
- * const newSf = sf.str.replace("d", 7)
- * console.log(newSf.values)
- * // ["Goo7", "o77", "crazy"]
- * ```
- */
- replace(searchValue = "", replaceValue = "", options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.replace(searchValue, replaceValue));
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Returns a new string with a specified number of copies of an existing string
- * @param num Number of times to repeat
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "crazy"])
- * const newSf = sf.str.replace(2)
- * console.log(newSf.values)
- * // ["GoodGood", "oddodd", "crazycrazy"]
- * ```
- */
- repeat(num = 1, options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.repeat(num));
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
-
- /**
- * Searches a string for a specified value, or regular expression, and returns the position of the match
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "crazy"])
- * const newSf = sf.str.search("d")
- * console.log(newSf.values)
- * ```
- */
- search(str = "", options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.search(str));
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Extracts a part of a string and returns a new string
- * @param startIndex index position of start character
- * @param endIndex index position of last character
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "crazy"])
- * const newSf = sf.str.slice(0,1)
- * console.log(newSf.values)
- * // ["G", "o", "c"]
- * ```
- */
- slice(startIndex = 0, endIndex = 1, options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.slice(startIndex, endIndex));
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Splits a string into an values of substrings
- * @param splitVal string or character to split at
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "grade"])
- * const newSf = sf.str.split(d)
- * console.log(newSf.values)
- * ```
- */
- split(splitVal = " ", options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${String(val).split(splitVal)}`);
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Extracts the characters from a string, beginning at a specified start position, and through the specified number of character
- * @param startIndex index position of start character
- * @param num number of characters to return
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "grade"])
- * const newSf = sf.str.substr(d)
- * ```
- */
- substr(startIndex = 0, num = 1, options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${String(val).substr(startIndex, num)}`);
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Extracts the characters from a string, between two specified indices
- * @param startIndex index position of start character
- * @param endIndex index position of last character
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "grade"])
- * const newSf = sf.str.substring(d)
- * ```
- */
- substring(startIndex = 0, endIndex = 1, options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${String(val).substring(startIndex, endIndex)}`);
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Removes whitespace from both ends of a string
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series([" Good", "odd ", " grade "])
- * const newSf = sf.str.trim(d)
- * ["Good", "odd", "grade"]
- * ```
- */
- trim(options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.trim());
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Joins strings to specified value
- * @param valToJoin string value to join to the values
- * @param joinChar Character to Join with
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "grade"])
- * const newSf = sf.str.join("new", "_")
- * // ["Good_new", "odd_new", "grade_new"]
- * ```
- */
- join(valToJoin = "", joinChar = " ", options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- let leftChar = val;
- let rightChar = valToJoin;
- let new_char = `${leftChar}${joinChar}${rightChar}`;
- newArr.push(new_char);
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
- /**
- * Counts the number of characters in string
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "grade"])
- * const newSf = sf.str.len(d)
- * // [4,3,5]
- * ```
- */
- len(options) {
- const { inplace } = { inplace: false, ...options };
- const newArr = [];
- this.values.map((val) => {
- if (isNaN(val) && typeof val != "string") {
- newArr.push(NaN);
- } else {
- newArr.push(`${val}`.length);
- }
- });
- if (inplace) {
- this.series.$setValues(newArr);
- this.series.print();
- } else {
- const sf = this.series.copy();
- sf.$setValues(newArr);
- return sf;
- }
- }
-
-}
diff --git a/danfojs-browser/src/index.js b/danfojs-browser/src/index.js
deleted file mode 100644
index a5826c87..00000000
--- a/danfojs-browser/src/index.js
+++ /dev/null
@@ -1,49 +0,0 @@
-import * as tf from "@tensorflow/tfjs";
-import NDframe from "./core/generic";
-import { utils } from './shared/utils';
-import Series from "./core/series";
-import DataFrame from "./core/frame";
-import { concat } from "./core/concat";
-import { merge } from "./core/merge";
-import { LabelEncoder, OneHotEncoder } from "./preprocessing/encodings";
-import { MinMaxScaler, StandardScaler } from "./preprocessing/scalers";
-import { date_range } from "./core/date_range";
-import get_dummies from "./core/get_dummies";
-import Str from "./core/strings";
-import Dt from "./core/datetime";
-import { toDateTime } from "./core/datetime";
-import {
- readCSV as read_csv,
- toCSV as to_csv,
- readJSON as read_json,
- toJSON as to_json,
- readExcel as read_excel,
- toExcel as to_excel } from "./io";
-
-
-export {
- tf,
- date_range,
- toDateTime,
- concat,
- merge,
- NDframe,
- utils,
- Str,
- Dt,
- Series,
- DataFrame,
- read_csv,
- to_csv,
- read_json,
- to_json,
- read_excel,
- to_excel,
- MinMaxScaler,
- StandardScaler,
- LabelEncoder,
- OneHotEncoder,
- get_dummies
-};
-
-export const _version = "0.3.4";
diff --git a/danfojs-browser/src/io/io.csv.js b/danfojs-browser/src/io/io.csv.js
deleted file mode 100644
index 0fb5306d..00000000
--- a/danfojs-browser/src/io/io.csv.js
+++ /dev/null
@@ -1,137 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import Papa from 'papaparse';
-import { DataFrame } from '../index';
-
-
-/**
- * Reads a CSV file from local or remote location into a DataFrame.
- * @param filePath URL or local file path to CSV file. `readCSV` uses PapaParse to parse the CSV file,
- * hence all PapaParse options are supported.
- * @param options Configuration object. Supports all Papaparse parse config options. See https://papaparse.com/docs#config.
- * @returns DataFrame containing the parsed CSV file.
- * @example
- * ```
- * import { readCSV } from "danfojs-node"
- * const df = await readCSV("https://raw.githubusercontent.com/test.csv")
- * ```
- * @example
- * ```
- * import { readCSV } from "danfojs-node"
- * const df = await readCSV("https://raw.githubusercontent.com/test.csv", {
- * delimiter: ",",
- * headers: {
- * Accept: "text/csv",
- * Authorization: "Bearer YWRtaW46YWRtaW4="
- * }
- * })
- * ```
- * @example
- * ```
- * import { readCSV } from "danfojs-node"
- * const df = await readCSV("./data/sample.csv")
- * ```
- */
-const $readCSV = async (filePath, options) => {
- options = { header: true, dynamicTyping: true, ...options };
- return new Promise((resolve) => {
- Papa.parse(filePath, {
- ...options,
- download: true,
- complete: (results) => {
- const df = new DataFrame(results.data);
- resolve(df);
- }
- });
- });
-};
-
-
-/**
- * Converts a DataFrame or Series to CSV.
- * @param df DataFrame or Series to be converted to CSV.
- * @param options Configuration object. Supports the following options:
- * - `filePath`: Local file path to write the CSV file. If not specified, the CSV will be returned as a string.
- * - `header`: Boolean indicating whether to include a header row in the CSV file.
- * - `sep`: Character to be used as a separator in the CSV file.
- * @example
- * ```
- * import { toCSV } from "danfojs-node"
- * const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
- * const csv = toCSV(df)
- * ```
- * @example
- * ```
- * import { toCSV } from "danfojs-node"
- * const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
- * toCSV(df, {
- * filePath: "./data/sample.csv",
- * header: true,
- * sep: "+"
- * })
- * ```
- */
-const $toCSV = (df, options) => {
- let { fileName, download, sep, header } = { fileName: "output.csv", sep: ",", header: true, download: true, ...options };
-
- if (df.$isSeries) {
- const csv = df.values.join(sep);
-
- if (download) {
- if (!(fileName.endsWith(".csv"))) {
- fileName = fileName + ".csv";
- }
- $downloadFileInBrowser(csv, fileName);
- } else {
- return csv;
- }
- } else {
- const rows = df.values;
- let csvStr = header === true ? `${df.columns.join(sep)}\n` : "";
-
- for (let i = 0; i < rows.length; i++) {
- const row = `${rows[i].join(sep)}\n`;
- csvStr += row;
- }
-
- if (download) {
- if (!(fileName.endsWith(".csv"))) {
- fileName = fileName + ".csv";
- }
- $downloadFileInBrowser(csvStr, fileName);
- } else {
- return csvStr;
- }
- }
-};
-
-/**
- * Function to download a CSV file in the browser.
- * @param content A string of CSV file contents
- * @param fileName The name of the file to be downloaded
- */
-const $downloadFileInBrowser = (content, fileName) => {
- var hiddenElement = document.createElement('a');
- hiddenElement.href = 'data:text/csv;charset=utf-8,' + encodeURI(content);
- hiddenElement.target = '_blank';
- hiddenElement.download = fileName;
- hiddenElement.click();
-};
-
-
-export {
- $readCSV,
- $toCSV
-};
diff --git a/danfojs-browser/src/io/io.excel.js b/danfojs-browser/src/io/io.excel.js
deleted file mode 100644
index 2063b80c..00000000
--- a/danfojs-browser/src/io/io.excel.js
+++ /dev/null
@@ -1,120 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { DataFrame, NDframe, Series } from '../index';
-import XLSX from 'xlsx';
-
-/**
- * Reads a JSON file from local or remote location into a DataFrame.
- * @param file URL or local file path to JSON file.
- * @param options Configuration object. Supported options:
- * - `method`: The HTTP method to use. Defaults to `'GET'`.
- * - `headers`: Additional headers to send with the request. Supports the `node-fetch` [HeadersInit]
- * @example
- * ```
- * import { readExcel } from "danfojs-node"
- * const df = await readExcel("https://raw.githubusercontent.com/test.xlsx")
- * ```
- * @example
- * ```
- * import { readExcel } from "danfojs-node"
- * const df = await readExcel("https://raw.githubusercontent.com/test.xlsx", {
- * method: "GET",
- * headers: {
- * Accept: "text/csv",
- * Authorization: "Bearer YWRtaW46YWRtaW4="
- * }
- * })
- * ```
- */
-const $readExcel = async (file, options) => {
- const { sheet, method, headers } = { sheet: 0, method: "GET", headers: {}, ...options };
-
- if (typeof file === "string" && file.startsWith("http")) {
-
- return new Promise((resolve) => {
- fetch(file, { method, headers }).then((response) => {
- if (response.status !== 200) {
- throw new Error(`Failed to load ${file}`);
- }
- response.arrayBuffer().then((arrBuf) => {
- const arrBufInt8 = new Uint8Array(arrBuf);
- const workbook = XLSX.read(arrBufInt8, { type: "array" });
- const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
- const data = XLSX.utils.sheet_to_json(worksheet);
- const df = new DataFrame(data);
- resolve(df);
- });
- }).catch((err) => {
- throw new Error(err);
- });
- });
-
- } else if (file instanceof File) {
- const arrBuf = await file.arrayBuffer();
- const arrBufInt8 = new Uint8Array(arrBuf);
- const workbook = XLSX.read(arrBufInt8, { type: "array" });
- const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
- const data = XLSX.utils.sheet_to_json(worksheet);
- const df = new DataFrame(data);
- return df;
- } else {
- throw new Error("ParamError: File not supported. file must be a url or an input File object");
- }
-};
-
-/**
- * Converts a DataFrame or Series to Excel Sheet.
- * @param df DataFrame or Series to be converted to JSON.
- * @param options Configuration object. Supported options:
- * - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
- * - `file`: The file to be written to. Defaults to `'./output.xlsx'`.
- * @example
- * ```
- * import { toExcel } from "danfojs-node"
- * const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
- * toExcel(df, {
- * file: "./data/sample.xlsx",
- * sheetName: "MySheet",
- * })
- * ```
- */
-const $toExcel = (df, options) => {
- let { fileName, sheetName } = { fileName: "./output.xlsx", sheetName: "Sheet1", ...options };
-
- if (!(fileName.endsWith(".xlsx"))) {
- fileName = fileName + ".xlsx";
- }
- let data;
-
- if (df.$isSeries) {
- const row = df.values;
- const col = df.columns;
- data = [ col, ...(row.map((x) => [ x ])) ];
- } else {
- const row = df.values;
- const cols = df.columns;
- data = [ cols, ...row ];
- }
-
- const worksheet = XLSX.utils.aoa_to_sheet(data);
- const wb = XLSX.utils.book_new();
- XLSX.utils.book_append_sheet(wb, worksheet, sheetName);
- XLSX.writeFile(wb, `${fileName}`);
-};
-
-export {
- $readExcel,
- $toExcel
-};
diff --git a/danfojs-browser/src/io/io.json.js b/danfojs-browser/src/io/io.json.js
deleted file mode 100644
index f8b3d929..00000000
--- a/danfojs-browser/src/io/io.json.js
+++ /dev/null
@@ -1,167 +0,0 @@
-import { DataFrame, NDframe, Series } from '../index';
-
-/**
- * Reads a JSON file from local or remote location into a DataFrame.
- * @param fileName URL or local file path to JSON file.
- * @param options Configuration object. Supported options:
- * - `method`: The HTTP method to use. Defaults to `'GET'`.
- * - `headers`: Additional headers to send with the request. Supports the `node-fetch` [HeadersInit]
- * @example
- * ```
- * import { readJSON } from "danfojs-node"
- * const df = await readJSON("https://raw.githubusercontent.com/test.json")
- * ```
- * @example
- * ```
- * import { readJSON } from "danfojs-node"
- * const df = await readJSON("https://raw.githubusercontent.com/test.json", {
- * headers: {
- * Accept: "text/json",
- * Authorization: "Bearer YWRtaW46YWRtaW4="
- * }
- * })
- * ```
- * @example
- * ```
- * import { readJSON } from "danfojs-node"
- * const df = await readJSON("./data/sample.json")
- * ```
- */
-const $readJSON = async (file, options) => {
- const { method, headers } = { method: "GET", headers: {}, ...options };
-
- if (typeof file === "string" && file.startsWith("http")) {
-
- return new Promise((resolve) => {
- fetch(file, { method, headers }).then((response) => {
- if (response.status !== 200) {
- throw new Error(`Failed to load ${file}`);
- }
- response.json().then((json) => {
- resolve(new DataFrame(json));
- });
- }).catch((err) => {
- throw new Error(err);
- });
- });
-
- } else if (file instanceof File) {
- return new Promise((resolve) => {
- const reader = new FileReader();
- reader.readAsText(file);
- reader.onload = (event) => {
- const jsonObj = JSON.parse(event.target.result);
- resolve(new DataFrame(jsonObj));
- };
- });
- } else {
- throw new Error("ParamError: File not supported. file must be a url or an input File object");
- }
-};
-
-
-/**
- * Converts a DataFrame or Series to JSON.
- * @param df DataFrame or Series to be converted to JSON.
- * @param options Configuration object. Supported options:
- * - `fileName`: The file path to write the JSON to. If not specified, the JSON object is returned.
- * - `format`: The format of the JSON. Defaults to `'column'`. E.g for using `column` format:
- * ```
- * [{ "a": 1, "b": 2, "c": 3, "d": 4 },
- * { "a": 5, "b": 6, "c": 7, "d": 8 }]
- * ```
- * and `row` format:
- * ```
- * { "a": [1, 5, 9],
- * "b": [2, 6, 10]
- * }
- * ```
- * @example
- * ```
- * import { toJSON } from "danfojs-node"
- * const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
- * const json = toJSON(df)
- * ```
- * @example
- * ```
- * import { toJSON } from "danfojs-node"
- * const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
- * toJSON(df, {
- * fileName: "./data/sample.json",
- * format: "row"
- * })
- * ```
- */
-const $toJSON = (df, options) => {
- let { fileName, format, download } = { fileName: "output.json", download: true, format: "column", ...options };
-
- if (df.$isSeries) {
- const obj = {};
- obj[df.columns[0]] = df.values;
-
- if (download) {
- if (!fileName.endsWith(".json")) {
- fileName = fileName + ".json";
- }
- $downloadFileInBrowser(obj, fileName);
- } else {
- return obj;
- }
-
- } else {
- if (format === "row") {
- const obj = {};
- for (let i = 0; i < df.columns.length; i++) {
- obj[df.columns[i]] = (df).column(df.columns[i]).values;
- }
- if (download) {
- if (!(fileName.endsWith(".json"))) {
- fileName = fileName + ".json";
- }
-
- $downloadFileInBrowser(obj, fileName);
- } else {
- return obj;
- }
- } else {
- const values = df.values;
- const header = df.columns;
- const jsonArr = [];
-
- values.forEach((val) => {
- const obj = {};
- header.forEach((h, i) => {
- obj[h] = val[i];
- });
- jsonArr.push(obj);
- });
- if (download) {
- if (!fileName.endsWith(".json")) {
- fileName = fileName + ".json";
- }
- $downloadFileInBrowser(jsonArr, fileName);
- } else {
- return jsonArr;
- }
- }
- }
-};
-
-/**
- * Function to download a CSV file in the browser.
- * @param content A string of CSV file contents
- * @param fileName The name of the file to be downloaded
- */
-const $downloadFileInBrowser = (content, fileName) => {
- var hiddenElement = document.createElement('a');
- hiddenElement.href = 'data:text/json;charset=utf-8,' + encodeURI(JSON.stringify(content));
- hiddenElement.target = '_blank';
- hiddenElement.download = fileName;
- hiddenElement.click();
-};
-
-
-export {
- $readJSON,
- $toJSON
-};
diff --git a/danfojs-browser/src/plotting/plot.js b/danfojs-browser/src/plotting/plot.js
deleted file mode 100644
index 643908a1..00000000
--- a/danfojs-browser/src/plotting/plot.js
+++ /dev/null
@@ -1,867 +0,0 @@
-/* eslint-disable no-undef */
-import { utils } from "../shared/utils";
-import Series from "../core/series";
-
-/**
- * Change: Rising Odegua 6th Feb 2021
- * We no longer bundle Plotjs with DanfoJs. I noticed that removing this single
- * inport reduces the bundle size from 5.7mb to 2.1mb. I know it's still a huge
- * bundle, but it's progress!
- * If you need to use Plotly, then you can add the Plotly CDN
- * to your script before DanfoJs CDN
- */
-try {
- const version = Plotly.version;
- console.info(`Using Plotly version ${version}`);
-} catch (error) {
- console.info(`Plotly CDN not found. If you need to make Plots, then add the Plotly CDN to your script`);
-}
-
-/**
- * Plotting methods and Functions performed on Series and DataFrames
- */
-export class Plot {
- constructor(ndframe, div) {
- this.div = div;
- this.ndframe = ndframe;
- }
-
-
- /**
- * Plot Series or DataFrame as lines. This function is useful to plot lines using DataFrame’s values as coordinates.
- * Make plots of Series or DataFrame.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- line(config = {}) {
-
- let ret_params = this.__get_plot_params(config);
- let this_config = ret_params[0];
- let params = ret_params[1];
-
- if (this.ndframe instanceof Series) {
- let trace = {};
- let y = this.ndframe.values;
-
- params.forEach((param) => {
- if (!(param == "layout")) {
- trace[param] = config[param];
- }
- });
-
- trace["y"] = y;
- trace['type'] = "line";
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else {
- //check if plotting two columns against each other
- if (utils.keyInObject(this_config, 'x') && utils.keyInObject(this_config, 'y')) {
- if (!this.ndframe.columns.includes(this_config['x'])) {
- throw Error(`Column Error: ${this_config['x']} not found in columns`);
- }
- if (!this.ndframe.columns.includes(this_config['y'])) {
- throw Error(`Column Error: ${this_config['y']} not found in columns`);
- }
-
-
- let x = this.ndframe[this_config['x']].values;
- let y = this.ndframe[this_config['y']].values;
-
- let trace = {};
- trace["x"] = x;
- trace['y'] = y;
-
-
- let xaxis = {}; let yaxis = {};
- xaxis['title'] = this_config['x'];
- yaxis['title'] = this_config['y'];
-
- this_config['layout']['xaxis'] = xaxis;
- this_config['layout']['yaxis'] = yaxis;
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else if (utils.keyInObject(this_config, 'x') || utils.keyInObject(this_config, 'y')) {
- //plot single column specified in either of param [x | y] against index
- //plot columns against index
- let data = [];
- let cols_to_plot;
-
- if (utils.keyInObject(this_config, "columns")) {
- cols_to_plot = this.____check_if_cols_exist(this_config['columns']);
- } else {
- cols_to_plot = this.ndframe.columns;
- }
-
- cols_to_plot.forEach((c_name) => {
- let trace = {};
-
- params.forEach((param) => { //TODO accept individual configuration for traces
- trace[param] = config[param];
- });
- if (utils.keyInObject(this_config, 'x')) {
- trace["x"] = this.ndframe[this_config['x']].values;
- trace["y"] = this.ndframe[c_name].values;
- trace['name'] = c_name;
- } else {
- trace["y"] = this.ndframe[this_config['y']].values;
- trace["x"] = this.ndframe[c_name].values;
- trace['name'] = c_name;
- }
-
- data.push(trace);
-
- });
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
- } else {
- //plot columns against index
- let data = [];
- let cols_to_plot;
-
- if (utils.keyInObject(this_config, "columns")) {
- cols_to_plot = this.____check_if_cols_exist(this_config['columns']);
- } else {
- cols_to_plot = this.ndframe.columns;
- }
-
- cols_to_plot.forEach((c_name) => {
- let trace = {};
-
- params.forEach((param) => { //TODO accept individual configuration for traces
- trace[param] = config[param];
- });
- trace["x"] = this.ndframe.index;
- trace["y"] = this.ndframe[c_name].values;
- trace['name'] = c_name;
-
- data.push(trace);
-
- });
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
- }
-
- }
-
-
- }
-
-
- /**
- * Plot Series or DataFrame as Bars.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- bar(config = {}) {
-
- let ret_params = this.__get_plot_params(config);
- let this_config = ret_params[0];
- let params = ret_params[1];
-
- if (this.ndframe instanceof Series) {
- let trace = {};
- let y = this.ndframe.values;
-
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- trace["y"] = y;
- trace['type'] = "bar";
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else {
- //check if plotting two columns against each other
- if (utils.keyInObject(this_config, 'x') && utils.keyInObject(this_config, 'y')) {
- if (!this.ndframe.columns.includes(this_config['x'])) {
- throw Error(`Column Error: ${this_config['x']} not found in columns`);
- }
- if (!this.ndframe.columns.includes(this_config['y'])) {
- throw Error(`Column Error: ${this_config['y']} not found in columns`);
- }
-
-
- let x = this.ndframe[this_config['x']].values;
- let y = this.ndframe[this_config['y']].values;
-
- let trace = {};
- trace["x"] = x;
- trace['y'] = y;
- trace['type'] = "bar";
-
-
- let xaxis = {}; let yaxis = {};
- xaxis['title'] = this_config['x'];
- yaxis['title'] = this_config['y'];
-
- this_config['layout']['xaxis'] = xaxis;
- this_config['layout']['yaxis'] = yaxis;
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else if (utils.keyInObject(this_config, 'x') || utils.keyInObject(this_config, 'y')) {
- //plot single column specified in either of param [x | y] against index
- let trace = {};
-
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- if (utils.keyInObject(this_config, 'x')) {
- trace['y'] = this.ndframe[this_config['x']].values;
- } else {
- trace['y'] = this.ndframe[this_config['y']].values;
- }
- trace['type'] = "bar";
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else {
- //plot columns against index
- let data = [];
- let cols_to_plot;
-
- if (utils.keyInObject(this_config, "columns")) {
- cols_to_plot = this.____check_if_cols_exist(this_config['columns']);
- } else {
- cols_to_plot = this.ndframe.columns;
- }
-
- cols_to_plot.forEach((c_name) => {
- let trace = {};
-
- // params.forEach(param => { //TODO accept individual configuration for traces
- // trace[param] = config[param]
- // })
- trace['x'] = this.ndframe.index;
- trace["y"] = this.ndframe[c_name].values;
- trace['name'] = c_name;
- trace['type'] = "bar";
-
- data.push(trace);
-
- });
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
- }
-
- }
-
-
- }
-
-
- /**
- * Plot two or more columns in a DataFrame as scatter points. If Series, plot against its index
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- scatter(config = {}) {
-
- let ret_params = this.__get_plot_params(config);
- let this_config = ret_params[0];
- let params = ret_params[1];
-
- if (this.ndframe instanceof Series) {
- let trace = {};
-
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- trace["x"] = this.ndframe.values;
- trace['y'] = this.ndframe.index;
- trace['type'] = "scatter";
- trace['mode'] = "markers";
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else {
- //check if plotting two columns against each other
- if (utils.keyInObject(this_config, 'x') && utils.keyInObject(this_config, 'y')) {
- if (!this.ndframe.columns.includes(this_config['x'])) {
- throw Error(`Column Error: ${this_config['x']} not found in columns`);
- }
- if (!this.ndframe.columns.includes(this_config['y'])) {
- throw Error(`Column Error: ${this_config['y']} not found in columns`);
- }
-
-
- let x = this.ndframe[this_config['x']].values;
- let y = this.ndframe[this_config['y']].values;
-
- let trace = {};
- trace["x"] = x;
- trace['y'] = y;
- trace['type'] = "scatter";
- trace['mode'] = "markers";
-
- let xaxis = {}; let yaxis = {};
- xaxis['title'] = this_config['x'];
- yaxis['title'] = this_config['y'];
-
- this_config['layout']['xaxis'] = xaxis;
- this_config['layout']['yaxis'] = yaxis;
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else if (utils.keyInObject(this_config, 'x') || utils.keyInObject(this_config, 'y')) {
- //plot single column specified in either of param [x | y] against index
- let trace = {};
-
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- if (utils.keyInObject(this_config, 'x')) {
- trace['y'] = this.ndframe.index;
- trace['x'] = this.ndframe[this_config['x']].values;
-
- } else {
- trace['x'] = this.ndframe.index;
- trace['y'] = this.ndframe[this_config['y']].values;
-
- }
- trace['type'] = "scatter";
- trace['mode'] = "markers";
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else {
- //plot columns against index
- let data = [];
- let cols_to_plot;
-
- if (utils.keyInObject(this_config, "columns")) {
- cols_to_plot = this.____check_if_cols_exist(this_config['columns']);
- } else {
- cols_to_plot = this.ndframe.columns;
- }
-
- cols_to_plot.forEach((c_name) => {
- let trace = {};
-
- // params.forEach(param => { //TODO accept individual configuration for traces
- // trace[param] = config[param]
- // })
- trace['y'] = this.ndframe.index;
- trace["x"] = this.ndframe[c_name].values;
- trace['name'] = c_name;
- trace['type'] = "scatter";
- trace['mode'] = "markers";
- data.push(trace);
-
- });
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
- }
-
- }
-
-
- }
-
-
- /**
- * Plot columns in a Series/DataFrame as Histograms.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- hist(config = {}) {
-
- let ret_params = this.__get_plot_params(config);
- let this_config = ret_params[0];
- let params = ret_params[1];
-
- if (this.ndframe instanceof Series) {
- let trace = {};
-
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- trace["x"] = this.ndframe.values;
- trace['type'] = "histogram";
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else if (utils.keyInObject(this_config, 'x')) {
- //plot as vertical histogram
- let trace = {};
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- trace['x'] = this.ndframe[this_config['x']].values;
- trace['type'] = "histogram";
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else if (utils.keyInObject(this_config, 'y')) {
- //plot as vertical histogram
- let trace = {};
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- trace['y'] = this.ndframe[this_config['y']].values;
- trace['type'] = "histogram";
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else {
- let data = [];
- let cols_to_plot;
-
- if (utils.keyInObject(this_config, "columns")) {
- cols_to_plot = this.____check_if_cols_exist(this_config['columns']);
- } else {
- cols_to_plot = this.ndframe.columns;
- }
-
- cols_to_plot.forEach((c_name) => {
- let trace = {};
- trace["x"] = this.ndframe[c_name].values;
- trace['name'] = c_name;
- trace['type'] = "histogram";
- data.push(trace);
-
- });
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
- }
-
- }
-
-
- /**
- * Makes Pie Plots from two Columns in a DataFrame.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- pie(config = {}) {
-
- let ret_params = this.__get_plot_params(config);
- let this_config = ret_params[0];
-
-
- if (this.ndframe instanceof Series) {
- let data = [{
- values: this.ndframe.values,
- labels: this.ndframe.index,
- type: 'pie',
- name: this_config['labels'],
- hoverinfo: 'label+percent+name',
- automargin: true
- }];
-
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
- } else if (utils.keyInObject(this_config, 'values') && utils.keyInObject(this_config, 'labels')) {
- if (!this.ndframe.columns.includes(this_config['labels'])) {
- throw Error(`Column Error: ${this_config['labels']} not found in columns. labels name must be one of [ ${this.ndframe.columns}]`);
- }
- if (!this.ndframe.columns.includes(this_config['values'])) {
- throw Error(`Column Error: ${this_config['values']} not found in columns. value name must be one of [ ${this.ndframe.columns}]`);
- }
- let data = [{
- values: this.ndframe[this_config['values']].values,
- labels: this.ndframe[this_config['labels']].values,
- type: 'pie',
- name: this_config['labels'],
- hoverinfo: 'label+percent+name',
- automargin: true
- }];
-
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
- } else {
- let cols_to_plot;
-
- if (utils.keyInObject(this_config, "columns")) {
- cols_to_plot = this.____check_if_cols_exist(this_config['columns']);
- } else {
- cols_to_plot = this.ndframe.columns;
- }
-
- if (utils.keyInObject(this_config, 'row_pos')) {
- if (this_config['row_pos'].length != cols_to_plot.length - 1) {
- throw Error(`Lenght of row_pos array must be equal to number of columns. Got ${this_config['row_pos'].length}, expected ${cols_to_plot.length - 1}`);
- }
- } else {
- let temp_arr = [];
- for (let i = 0; i < cols_to_plot.length - 1; i++) {
- temp_arr.push(0);
- }
- this_config['row_pos'] = temp_arr;
-
- }
-
- if (utils.keyInObject(this_config, 'col_pos')) {
- if (this_config['col_pos'].length != cols_to_plot.length - 1) {
- throw Error(`Lenght of col_pos array must be equal to number of columns. Got ${this_config['col_pos'].length}, expected ${cols_to_plot.length - 1}`);
- }
- } else {
- let temp_arr = [];
- for (let i = 0; i < cols_to_plot.length - 1; i++) {
- temp_arr.push(i);
- }
- this_config['col_pos'] = temp_arr;
-
- }
- let data = [];
-
- cols_to_plot.forEach((c_name, i) => {
- let trace = {};
- trace["values"] = this.ndframe[c_name].values;
- trace['labels'] = this.ndframe[this_config['labels']].values;
- trace['name'] = c_name;
- trace['type'] = "pie";
- trace['domain'] = { row: this_config['row_pos'][i], column: this_config['col_pos'][i] };
- trace["hoverinfo"] = 'label+percent+name';
- trace['textposition'] = "outside";
- trace['automargin'] = true;
- data.push(trace);
-
- });
-
- if (!utils.keyInObject(this_config, "grid")) {
- //set default grid
- let size = Number((this.ndframe.shape[1] / 2).toFixed()) + 1;
- this_config['grid'] = { rows: size, columns: size };
- }
- this_config['layout']['grid'] = this_config['grid'];
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
-
- }
-
- }
-
-
- /**
- * Plot Box plots from Series or DataFrame as lines.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- box(config = {}) {
-
- let ret_params = this.__get_plot_params(config);
- let this_config = ret_params[0];
- let params = ret_params[1];
-
- if (this.ndframe instanceof Series) {
- let trace = {};
- let y = this.ndframe.values;
-
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- trace["y"] = y;
- trace['type'] = "box";
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else {
- //check if plotting two columns against each other
- if (utils.keyInObject(this_config, 'x') && utils.keyInObject(this_config, 'y')) {
- if (!this.ndframe.columns.includes(this_config['x'])) {
- throw Error(`Column Error: ${this_config['x']} not found in columns`);
- }
- if (!this.ndframe.columns.includes(this_config['y'])) {
- throw Error(`Column Error: ${this_config['y']} not found in columns`);
- }
-
-
- let x = this.ndframe[this_config['x']].values;
- let y = this.ndframe[this_config['y']].values;
-
- let trace = {};
- trace["x"] = x;
- trace['y'] = y;
- trace['type'] = 'box';
-
-
- let xaxis = {}; let yaxis = {};
- xaxis['title'] = this_config['x'];
- yaxis['title'] = this_config['y'];
-
- this_config['layout']['xaxis'] = xaxis;
- this_config['layout']['yaxis'] = yaxis;
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else if (utils.keyInObject(this_config, 'x') || utils.keyInObject(this_config, 'y')) {
- //plot single column specified in either of param [x | y] against index
- let trace = {};
-
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- if (utils.keyInObject(this_config, 'x')) {
- trace['x'] = this.ndframe[this_config['x']].values;
- trace['y'] = this.ndframe.index;
- trace['type'] = 'box';
- } else {
- trace['x'] = this.ndframe.index;
- trace['y'] = this_config['y'];
- trace['type'] = 'box';
- }
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else {
- //plot columns against index
- let data = [];
- let cols_to_plot;
-
- if (utils.keyInObject(this_config, "columns")) {
- cols_to_plot = this.____check_if_cols_exist(this_config['columns']);
- } else {
- cols_to_plot = this.ndframe.columns;
- }
-
- cols_to_plot.forEach((c_name) => {
- let trace = {};
-
- params.forEach((param) => { //TODO accept individual configuration for traces
- trace[param] = config[param];
- });
- trace["x"] = this.ndframe[c_name].values;
- trace["y"] = this.ndframe.index;
- trace['name'] = c_name;
- trace['type'] = 'box';
- data.push(trace);
-
- });
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
- }
-
- }
-
-
- }
-
-
- /**
- * Plot Violin plots from Series or DataFrame as lines.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- violin(config = {}) {
-
- let ret_params = this.__get_plot_params(config);
- let this_config = ret_params[0];
- let params = ret_params[1];
-
- if (this.ndframe instanceof Series) {
- let trace = {};
- let y = this.ndframe.values;
-
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- trace["y"] = y;
- trace['type'] = "violin";
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else {
- //check if plotting two columns against each other
- if (utils.keyInObject(this_config, 'x') && utils.keyInObject(this_config, 'y')) {
- if (!this.ndframe.columns.includes(this_config['x'])) {
- throw Error(`Column Error: ${this_config['x']} not found in columns`);
- }
- if (!this.ndframe.columns.includes(this_config['y'])) {
- throw Error(`Column Error: ${this_config['y']} not found in columns`);
- }
-
-
- let x = this.ndframe[this_config['x']].values;
- let y = this.ndframe[this_config['y']].values;
-
- let trace = {};
- trace["x"] = x;
- trace['y'] = y;
- trace['type'] = 'violin';
-
-
- let xaxis = {}; let yaxis = {};
- xaxis['title'] = this_config['x'];
- yaxis['title'] = this_config['y'];
-
- this_config['layout']['xaxis'] = xaxis;
- this_config['layout']['yaxis'] = yaxis;
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else if (utils.keyInObject(this_config, 'x') || utils.keyInObject(this_config, 'y')) {
- //plot single column specified in either of param [x | y] against index
- let trace = {};
-
- params.forEach((param) => {
- if (!param == "layout") {
- trace[param] = config[param];
- }
- });
-
- if (utils.keyInObject(this_config, 'x')) {
- trace['x'] = this.ndframe[this_config['x']].values;
- trace['y'] = this.ndframe.index;
- trace['type'] = 'violin';
- } else {
- trace['x'] = this.ndframe.index;
- trace['y'] = this_config['y'];
- trace['type'] = 'violin';
- }
-
- Plotly.newPlot(this.div, [trace], this_config['layout'], this_config);
-
- } else {
- //plot columns against index
- let data = [];
- let cols_to_plot;
-
- if (utils.keyInObject(this_config, "columns")) {
- cols_to_plot = this.____check_if_cols_exist(this_config['columns']);
- } else {
- cols_to_plot = this.ndframe.columns;
- }
-
- cols_to_plot.forEach((c_name) => {
- let trace = {};
-
- params.forEach((param) => { //TODO accept individual configuration for traces
- trace[param] = config[param];
- });
- trace["y"] = this.ndframe[c_name].values;
- trace['name'] = c_name;
- trace['type'] = 'violin';
- data.push(trace);
-
- });
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
- }
-
- }
-
-
- }
-
- /**
- * Display DataFrame in a div using D3.js format
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- table(config = {}) {
- let ret_params = this.__get_plot_params(config);
- let this_config = ret_params[0];
- let header = {};
- let cells = {};
- let cols_data = [];
- let cols_2_show;
-
-
- if (utils.keyInObject(this_config, 'columns')) {
-
- this_config['columns'].forEach((cname) => {
- if (!this.ndframe.columns.includes(cname)) {
- throw Error(`Column Error: ${cname} not found in columns. Columns should be one of [ ${this.ndframe.columns} ]`);
- }
-
- let idx = this.ndframe.columns.indexOf(cname);
- cols_data.push(this.ndframe.getColumnData[idx]);
- });
- cols_2_show = this_config['columns'];
- } else {
-
- cols_2_show = this.ndframe.columns;
- cols_data = this.ndframe.getColumnData;
-
- }
-
- header['values'] = cols_2_show.map((col) => [col]);
- cells['values'] = cols_data;
-
- if (this_config['header_style']) {
- Object.keys(this_config['header_style']).forEach((param) => {
- header[param] = this_config['header_style'][param];
- });
- }
-
- if (this_config['cell_style']) {
- Object.keys(this_config['cell_style']).forEach((param) => {
- cells[param] = this_config['cell_style'][param];
- });
- }
- var data = [{
- type: 'table',
- header: header,
- cells: cells
- }];
- Plotly.newPlot(this.div, data, this_config['layout'], this_config);
-
- }
-
-
- __get_plot_params(config) {
- let params = Object.keys(config);
- let this_config = {};
-
- params.forEach((param) => {
- this_config[param] = config[param];
- });
-
- if (!utils.keyInObject(config, "layout")) {
- this_config['layout'] = {};
- }
-
-
- return [this_config, params];
-
- }
-
- ____check_if_cols_exist(cols) {
- cols.forEach((col) => {
- if (!this.ndframe.columns.includes(col)) {
- throw Error(`Column Error: ${col} not found in columns. Columns should be one of [ ${this.ndframe.columns} ]`);
- }
- });
- return cols;
- }
-
-
-}
diff --git a/danfojs-browser/src/preprocessing/encodings.js b/danfojs-browser/src/preprocessing/encodings.js
deleted file mode 100644
index 49b507ef..00000000
--- a/danfojs-browser/src/preprocessing/encodings.js
+++ /dev/null
@@ -1,106 +0,0 @@
-import Series from "../core/series";
-import DataFrame from "../core/frame";
-import { utils } from "../shared/utils";
-
-export class LabelEncoder {
-
- /**
- *
- * @param {data} data [Array|Series]
- * @returns Array.
- */
- fit(data) {
- let in_data = null;
- if (Array.isArray(data)) {
- in_data = data;
- } else if (data instanceof Series) {
- in_data = data.values;
- } else {
- throw new Error("data must be an array or a Series");
- }
-
- let data_set = new Set(in_data);
- this.label = Array.from(data_set);
-
- let self = this;
- let output_data = in_data.map((x) => {
- return self.label.indexOf(x);
- });
-
- return new Series(output_data);
- }
-
- /**
- * Transform data using the label generated from fitting
- * @param {data} data [Array|Series]
- * @returns Array
- */
- transform(data) {
- let in_data = null;
- if (Array.isArray(data)) {
- in_data = data;
- } else if (data instanceof Series) {
- in_data = data.values;
- } else {
- throw new Error("data must be an array or a Series");
- }
-
- let self = this;
- let output_data = in_data.map((x) => {
- return self.label.indexOf(x);
- });
- return new Series(output_data);
- }
-}
-
-export class OneHotEncoder {
-
- fit(data) {
- let in_data = null;
- if (Array.isArray(data)) {
- in_data = data;
- } else if (data instanceof Series) {
- in_data = data.values;
- } else {
- throw new Error("data must be an array");
- }
-
- let data_set = new Set(in_data);
- this.label = Array.from(data_set);
-
- let onehot_data = utils.zeros(in_data.length, this.label.length);
-
- for (let i = 0; i < in_data.length; i++) {
-
- let elem = in_data[i];
- let elem_index = this.label.indexOf(elem);
- onehot_data[i][elem_index] = 1;
- }
-
- return new DataFrame(onehot_data, { columns: this.label });
-
- }
-
- transform(data) {
- let in_data = null;
-
- if (Array.isArray(data)) {
- in_data = data;
- } else if (data instanceof Series) {
- in_data = data.values;
- } else {
- throw new Error("data must be an array");
- }
-
- let onehot_data = utils.zeros(in_data.length, this.label.length);
-
- for (let i = 0; i < in_data.length; i++) {
- let elem = in_data[i];
- let elem_index = this.label.indexOf(elem);
- onehot_data[i][elem_index] = 1;
- }
-
- return new DataFrame(onehot_data, { columns: this.label });
-
- }
-}
diff --git a/danfojs-browser/src/preprocessing/scalers.js b/danfojs-browser/src/preprocessing/scalers.js
deleted file mode 100644
index 3b27e227..00000000
--- a/danfojs-browser/src/preprocessing/scalers.js
+++ /dev/null
@@ -1,342 +0,0 @@
-import { tensor, moments } from "@tensorflow/tfjs";
-import Series from "../core/series";
-import DataFrame from "../core/frame";
-import { utils } from "../shared/utils";
-
-export class MinMaxScaler {
- /**
- * Fit minmax scaler on data, to obtain their min and max value
- * @param {data} data [DataFrame | Series | Array]
- * @returns Array
- */
- fit(data) {
- let tensor_data = null;
- if (Array.isArray(data)) {
- tensor_data = tensor(data);
- } else if (data instanceof DataFrame || data instanceof Series) {
- if (data.dtypes.includes("string")) {
- throw Error("Dtype Error: Cannot perform operation on string dtypes");
- }
- tensor_data = tensor(data.values);
- } else {
- throw new Error("data must either be an Array, DataFrame or Series");
- }
-
- this.max = tensor_data.max(0);
- this.min = tensor_data.min(0);
-
- let output_data = tensor_data
- .sub(this.min)
- .div(this.max.sub(this.min))
- .arraySync();
-
- if (data instanceof Series || Array.isArray(data)) {
- return new Series(output_data);
- } else {
- return new DataFrame(output_data);
- }
- }
-
- /**
- * Transform an array using the min and max generated from the fitting on data
- * @param {data} data [Array]
- * @returns array
- */
- transform(data) {
- if (data instanceof Series) {
- if (data.dtypes.includes("string")) {
- throw Error("Dtype Error: Cannot perform operation on string dtypes");
- }
- let tensor_data = tensor(data.values);
- let output_data = tensor_data
- .sub(this.min)
- .div(this.max.sub(this.min))
- .arraySync();
- return new Series(output_data);
- } else if (Array.isArray(data)) {
- let tensor_data = tensor(data);
- let output_data = tensor_data
- .sub(this.min)
- .div(this.max.sub(this.min))
- .arraySync();
- if (utils.is1DArray(data)) {
- return new Series(output_data);
- } else {
- return new DataFrame(output_data);
- }
- } else if (data instanceof DataFrame) {
- if (data.dtypes.includes("string")) {
- throw Error("Dtype Error: Cannot perform operation on string dtypes");
- }
- let tensor_data = tensor(data.values);
- let output_data = tensor_data
- .sub(this.min)
- .div(this.max.sub(this.min))
- .arraySync();
- return new DataFrame(output_data);
- } else {
- throw Error("Value Error: Data type not supported");
- }
- }
-
- /**
- * Restore a transformed array to their original values,
- * using the min and max generated from the fitting on data
- * @param {Series|Array|DataFrame} data
- * @returns Series|DataFrame
- */
- inverse_transform(data) {
- if (data instanceof Series) {
- if (data.dtypes.includes("string")) {
- throw Error("Dtype Error: Cannot perform operation on string dtypes");
- }
- let tensor_data = tensor(data.values);
- let output_data = tensor_data
- .mul(this.max.sub(this.min))
- .add(this.min)
- .arraySync();
- return new Series(output_data);
- } else if (Array.isArray(data)) {
- let tensor_data = tensor(data);
- let output_data = tensor_data
- .mul(this.max.sub(this.min))
- .add(this.min)
- .arraySync();
- if (utils.is1DArray(data)) {
- return new Series(output_data);
- } else {
- return new DataFrame(output_data);
- }
- } else if (data instanceof DataFrame) {
- if (data.dtypes.includes("string")) {
- throw Error("Dtype Error: Cannot perform operation on string dtypes");
- }
- let tensor_data = tensor(data.values);
- let output_data = tensor_data
- .mul(this.max.sub(this.min))
- .add(this.min)
- .arraySync();
- return new DataFrame(output_data);
- } else {
- throw Error("Value Error: Data type not supported");
- }
- }
-}
-
-export class StandardScaler {
- /**
- *
- * @param {data} data [DataFame | Series | Array]
- * @returns Array
- */
- fit(data) {
- let tensor_data = null;
- if (Array.isArray(data)) {
- tensor_data = tensor(data);
- } else if (data instanceof DataFrame || data instanceof Series) {
- if (data.dtypes.includes("string")) {
- throw Error("Dtype Error: Cannot perform operation on string dtypes");
- }
- tensor_data = tensor(data.values);
- } else {
- throw new Error("data must either be an Array, DataFrame or Series");
- }
-
- this.std = moments(tensor_data, 0).variance.sqrt();
- this.mean = tensor_data.mean(0);
- let output_data = tensor_data.sub(this.mean).div(this.std).arraySync();
-
- if (data instanceof Series || Array.isArray(data)) {
- return new Series((data = output_data));
- } else {
- return new DataFrame((data = output_data));
- }
- }
-
- transform(data) {
- // if(!Array.isArray(data)){
- // throw new Error(data)
- // }
-
- if (data instanceof Series) {
- if (data.dtypes.includes("string")) {
- throw Error("Dtype Error: Cannot perform operation on string dtypes");
- }
- let tensor_data = tensor(data.values);
- let output_data = tensor_data.sub(this.mean).div(this.std).arraySync();
- return new Series(output_data);
- } else if (Array.isArray(data)) {
- let tensor_data = tensor(data);
- let output_data = tensor_data.sub(this.mean).div(this.std).arraySync();
- if (utils.is1DArray(data)) {
- return new Series(output_data);
- } else {
- return new DataFrame(output_data);
- }
- } else if (data instanceof DataFrame) {
- if (data.dtypes.includes("string")) {
- throw Error("Dtype Error: Cannot perform operation on string dtypes");
- }
- let tensor_data = tensor(data.values);
- let output_data = tensor_data.sub(this.mean).div(this.std).arraySync();
- return new DataFrame(output_data);
- } else {
- throw Error("Value Error: Data type not supported");
- }
- }
-
- /**
- * Restore a transformed array to their original values,
- * using the mean and std generated from the fitting on data
- * @param {Series|Array|DataFrame} data
- * @returns Series|DataFrame
- */
- inverse_transform(data) {
- if (data instanceof Series) {
- if (data.dtypes.includes("string")) {
- throw Error("Dtype Error: Cannot perform operation on string dtypes");
- }
- let tensor_data = tensor(data.values);
- let output_data = tensor_data.mul(this.std).add(this.mean).arraySync();
- return new Series(output_data);
- } else if (Array.isArray(data)) {
- let tensor_data = tensor(data);
- let output_data = tensor_data.mul(this.std).add(this.mean).arraySync();
- if (utils.is1DArray(data)) {
- return new Series(output_data);
- } else {
- return new DataFrame(output_data);
- }
- } else if (data instanceof DataFrame) {
- if (data.dtypes.includes("string")) {
- throw Error("Dtype Error: Cannot perform operation on string dtypes");
- }
- let tensor_data = tensor(data.values);
- let output_data = tensor_data.mul(this.std).add(this.mean).arraySync();
- return new DataFrame(output_data);
- } else {
- throw Error("Value Error: Data type not supported");
- }
- }
-}
-
-// export class RobustScaler{
-
-// __median(arr, isTensor,return_index) {
-// if (!isTensor) {
-// const sorted = arr.slice().sort((a, b) => a - b);
-// const middle = Math.floor(sorted.length / 2);
-
-// if (sorted.length % 2 === 0) {
-
-// return return_index ? [(middle - 1) , middle] : (sorted[middle - 1] + sorted[middle]) / 2;
-// }
-
-// return return_index ? middle : sorted[middle] ;
-// } else {
-// let result_arr = []
-// arr.map(ele => {
-// const sorted = ele.slice().sort((a, b) => a - b);
-// const middle = Math.floor(sorted.length / 2);
-
-// if (sorted.length % 2 === 0) {
-// result_arr.push(return_index ? [(middle - 1) , middle] :(sorted[middle - 1] + sorted[middle]) / 2 )
-// } else {
-// result_arr.push(return_index ? middle : sorted[middle])
-// }
-
-// })
-// return result_arr
-// }
-
-// }
-
-// quantile(data,isTensor){
-
-// if(isTensor){
-// data = utils.__get_col_values(data);
-// }
-
-// let median = this.__median(data,isTensor,true)
-// let med = this.__median(data,isTensor,false)
-
-// let q1 = []
-// let q2 = []
-
-// if(!isTensor){
-// let sorted = data.slice().sort((a, b) => a - b);
-
-// if(Array.isArray(median)){
-// let lower = median[0]
-// let lower_data = sorted.slice(0,lower+1)
-// let upper_data = sorted.slice(lower+1,)
-
-// q1.push(this.__median(lower_data,isTensor,false));
-// q2.push(this.__median(upper_data,isTensor,false));
-
-// }else{
-// let lower_data = sorted.slice(0,median)
-// let upper_data = sorted.slice(median+1,)
-
-// q1.push(this.__median(lower_data,isTensor,false));
-// q2.push(this.__median(upper_data,isTensor,false));
-// }
-
-// }else{
-
-// data.map((x,i)=>{
-// let sorted = x.slice().sort((a, b) => a - b);
-
-// if(Array.isArray(median[i])){
-// let lower = median[i][0]
-// let lower_data = sorted.slice(0,lower+1)
-// let upper_data = sorted.slice(lower+1,)
-
-// q1.push(this.__median(lower_data,!isTensor,false));
-// q2.push(this.__median(upper_data,!isTensor,false));
-
-// }else{
-// let lower_data = sorted.slice(0,median[i])
-// let upper_data = sorted.slice(median[i]+1,)
-
-// q1.push(this.__median(lower_data,!isTensor,false));
-// q2.push(this.__median(upper_data,!isTensor,false));
-// }
-
-// })
-// }
-
-// return [q1,q2, med]
-
-// }
-
-// /**
-// * Fit robust scalar on data to obtain the first quantile and third quantile
-// * @param {data} data [DataFrame | Series | Array]
-// * @returns Array
-// */
-// fit(data){
-
-// let tensor_data = null
-// let isTensor = false;
-// if(Array.isArray(data)){
-// tensor_data = tensor(data)
-// }
-// else if((data instanceof DataFrame)){
-// tensor_data = tensor(data.values)
-// isTensor = true;
-// }
-// else if((data instanceof Series)){
-// tensor_data = tensor(data.values)
-// }
-// else{
-// throw new Error("data must either be an Array, DataFrame or Series")
-// }
-
-// let [q1, q3, median] = this.quantile(data,isTensor)
-// let q3_tensor = tensor(q3)
-// let output_data = tensor_data.sub(median).div(q3_tensor.sub(q1)).arraySync()
-
-// return output_data;
-// }
-// }
diff --git a/danfojs-browser/src/shared/config.js b/danfojs-browser/src/shared/config.js
deleted file mode 100644
index 40f011c5..00000000
--- a/danfojs-browser/src/shared/config.js
+++ /dev/null
@@ -1,83 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-
-/**
- * Package wide configuration class
- */
-export default class Configs {
-
- constructor(options) {
- const {
- tableDisplayConfig,
- tableMaxRow,
- tableMaxColInConsole,
- dtypeTestLim,
- lowMemoryMode
- } = {
- tableDisplayConfig: {},
- tableMaxRow: 10,
- tableMaxColInConsole: 21,
- dtypeTestLim: 10,
- lowMemoryMode: false,
- ...options
- };
- this.tableDisplayConfig = tableDisplayConfig;
- this.tableMaxRow = tableMaxRow; // The maximum number of rows to display in console
- this.tableMaxColInConsole = tableMaxColInConsole; // The maximum number of columns to display in console
- this.dtypeTestLim = dtypeTestLim; // The number of rows to use when inferring data type
- this.lowMemoryMode = lowMemoryMode; // Whether to use minimal memory or not.
- }
-
- setTableDisplayConfig(config) {
- this.tableDisplayConfig = config;
- }
-
- get getTableDisplayConfig() {
- return this.tableDisplayConfig;
- }
-
- setTableMaxColInConsole(val) {
- this.tableMaxColInConsole = val;
- }
-
- get getTableMaxColInConsole() {
- return this.tableMaxColInConsole;
- }
-
- setMaxRow(val) {
- this.tableMaxRow = val;
- }
-
- get getMaxRow() {
- return this.tableMaxRow;
- }
-
- get getDtypeTestLim() {
- return this.dtypeTestLim;
- }
-
- setDtypeTestLim(val) {
- this.dtypeTestLim = val;
- }
-
- get isLowMemoryMode() {
- return this.lowMemoryMode;
- }
-
- setIsLowMemoryMode(val) {
- this.lowMemoryMode = val;
- }
-}
-
diff --git a/danfojs-browser/src/shared/errors.js b/danfojs-browser/src/shared/errors.js
deleted file mode 100644
index ebd91219..00000000
--- a/danfojs-browser/src/shared/errors.js
+++ /dev/null
@@ -1,93 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-
-/**
- * Package wide error throwing class
- */
-class ErrorThrower {
-
- throwColumnNamesLengthError(ndframe, columns){
- const msg = `ParamError: Column names length mismatch. You provided a column of length ${columns.length} but Ndframe columns has lenght of ${ndframe.shape[1]}`;
- throw new Error(msg);
- }
-
- throwIndexLengthError(ndframe, index){
- const msg = `IndexError: You provided an index of length ${index.length} but Ndframe rows has lenght of ${ndframe.shape[0]}`;
- throw new Error(msg);
- }
-
- throwIndexDuplicateError(){
- const msg = `IndexError: Row index must contain unique values`;
- throw new Error(msg);
- }
-
- throwColumnDuplicateError(){
- const msg = `ColumnIndexError: Column index must contain unique values`;
- throw new Error(msg);
- }
-
- throwDtypesLengthError(ndframe, dtypes){
- const msg = `DtypeError: You provided a dtype array of length ${dtypes.length} but Ndframe columns has lenght of ${ndframe.shape[1]}`;
- throw new Error(msg);
- }
-
- throwDtypeNotSupportedError (dtype){
- const msg = `DtypeError: Dtype "${dtype}" not supported. dtype must be one of "${DATA_TYPES}"`;
- throw new Error(msg);
- }
-
- throwColumnLengthError(ndframe, arrLen){
- const msg = `ParamError: Column data length mismatch. You provided data with length ${arrLen} but Ndframe has column of lenght ${ndframe.shape[1]}`;
- throw new Error(msg);
- }
-
- throwRowLengthError(ndframe, arrLen){
- const msg = `ParamError: Row data length mismatch. You provided data with length ${arrLen} but Ndframe has row of lenght ${ndframe.shape[0]}`;
- throw new Error(msg);
- }
-
- throwColumnNotFoundError(ndframe){
- const msg = `ParamError: Column not found!. Column name must be one of ${ndframe.columns}`;
- throw new Error(msg);
- }
-
- throwNotImplementedError(){
- const msg = `Method not implemented`;
- throw new Error(msg);
- }
-
- throwIlocRowIndexError(){
- const msg = `ParamError: rows parameter must be a Array. For example: rows: [1,2] or rows: ["0:10"]`;
- throw new Error(msg);
- }
-
- throwIlocColumnsIndexError(){
- const msg = `ParamError: columns parameter must be a Array. For example: columns: [1,2] or columns: ["0:10"]`;
- throw new Error(msg);
- }
-
- throwStringDtypeOperationError(operation){
- const msg = `DtypeError: String data type does not support ${operation} operation`;
- throw new Error(msg);
- }
-
- throwSeriesMathOpLengthError(ndframe, other){
- const msg = `ParamError: Row length mismatch. Length of other (${other.shape[0]}), must be the same as Ndframe (${ndframe.shape[0]})`;
- throw new Error(msg);
- }
-
-}
-
-export default new ErrorThrower();
diff --git a/danfojs-browser/src/shared/utils.js b/danfojs-browser/src/shared/utils.js
deleted file mode 100644
index 2279f7bd..00000000
--- a/danfojs-browser/src/shared/utils.js
+++ /dev/null
@@ -1,801 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-
-import { BASE_CONFIG } from './defaults';
-import Config from './config';
-import { Series } from '..';
-import { DataFrame } from '..';
-import ErrorThrower from './errors';
-
-const config = new Config(BASE_CONFIG);
-
-/**
- * Utility class for working with Frames and Series
- */
-class Utils {
- /**
- * Removes an element from a 1D array
- *
- * ```js
- *
- * ```
- * @param arr The array to filter.
- * @param index The index to filter by.
- */
- removeElementFromArray(arr, index) {
- const newArr = arr.filter((_, i) => i != index);
- return newArr;
- }
-
- /**
- * Check if value is a string.
- * @param value The value to check.
- * @returns
- */
- isString(value) {
- return typeof value === "string";
- }
-
- /**
- * Checks if value is a number.
- * @param value The value to check.
- * @returns
- */
- isNumber(value) {
- return typeof value === "number" && isFinite(value);
- }
-
- /**
- * Checks if value is an object.
- * @param value The value to check.
- * @returns
- */
- isObject(value) {
- return typeof value === "object" && value.constructor && value.constructor.name === "Object";
- }
-
- /**
- * Checks if a value is null
- * @param value The value to check.
- * @returns
- */
- isNull(value) {
- return value === null;
- }
-
- /**
- * Checks if a value is undefined
- * @param value The value to check.
- * @returns
- */
- isUndefined(value) {
- return typeof value === "undefined";
- }
-
- /**
- * Checks if a value is empty. Empty means it's either null, undefined or NaN
- * @param value The value to check.
- * @returns
- */
- isEmpty(value) {
- return value === undefined || value === null || (isNaN(value) && typeof value !== "string");
- }
-
- /**
- * Generates an array of integers between specified range
- * @param start The starting number.
- * @param end The ending number.
- */
- range(start, end){
- if (end < start) {
- throw new Error("ParamError: end must be greater than start");
- }
-
- if (start === end) {
- return [start];
- }
-
- const arr = [];
- for (let i = start; i <= end; i++) {
- arr.push(i);
- }
- return arr;
- }
-
- /**
- * Checks if object has the specified key
- * @param obj The object to check.
- * @param key The key to find.
- */
- keyInObject(obj, key) {
- return Object.prototype.hasOwnProperty.call(obj, key);
- }
-
- /**
- * Transposes an array of array
- * @param obj The object to check.
- * @param key The key to find.
- */
- transposeArray(arr ) { //old name: __get_col_values
- if (arr.length === 0) return arr;
-
- const rowLen = arr.length;
- if (Array.isArray(arr[0])) {
- const colLen = arr[0].length;
- const newArr = [];
-
- for (let i = 0; i <= colLen - 1; i++) {
- const temp = [];
- for (let j = 0; j < rowLen; j++) {
- const _elem = (arr)[j][i];
- temp.push(_elem);
- }
- newArr.push(temp);
- }
- return newArr;
- } else {
- return arr;
- }
- }
-
- /**
- * Retrieve row array and column names from an object of the form {a: [1,2,3,4], b: [30,20, 30, 20]}
- * @param obj The object to retrieve rows and column names from.
- */
- getRowAndColValues(obj) {
- const colNames = Object.keys(obj);
- const colData = Object.values(obj);
- const firstColLen = colData[0].length;
-
- colData.forEach((cdata) => {
- if (cdata.length != firstColLen) {
- throw Error("Length Error: Length of columns must be the same!");
- }
- });
-
- const rowsArr = this.transposeArray(colData);
- return [rowsArr, colNames];
- }
-
- /**
- * Converts a 2D array of array to 1D array for Series Class
- * @param arr The array to convert.
- */
- convert2DArrayToSeriesArray(arr){
- const newArr = arr.map((val) => {
- if (this.isObject(val)) {
- return JSON.stringify(val);
- } else {
- return `${val}`;
- }
- });
- return newArr;
- }
-
- /**
- * Replaces all missing values with NaN. Missing values are undefined, Null and Infinity
- * @param arr The array
- * @param isSeries Whether the arr is a series or not
- */
- replaceUndefinedWithNaN(arr, isSeries) {
- if (arr.length === 0) return arr;
-
- if (isSeries && Array.isArray(arr)) {
- const newArr = arr.map((ele) => {
- if (typeof ele === "undefined") {
- return NaN;
- }
- if (typeof ele === "number" && (isNaN(ele) || ele == Infinity)) {
- return NaN;
- }
- if (ele == null) {
- return NaN;
- }
- return ele;
- });
- return newArr;
- } else {
- const newArr = [];
- if (Array.isArray(arr)) {
- for (let i = 0; i < arr.length; i++) {
- const innerArr = arr[i];
- const temp = (innerArr).map((ele) => {
- if (typeof ele === "undefined") {
- return NaN;
- }
- if (typeof ele === "number" && (isNaN(ele) || ele == Infinity)) {
- return NaN;
- }
- if (ele == null) {
- return NaN;
- }
- return ele;
- });
- newArr.push(temp);
- }
- }
- return newArr;
- }
- }
-
- /**
- * Infer data type from an array or array of arrays
- * @param arr An array or array of arrays
- */
- inferDtype(arr ) {
- const self = this;
- if (this.is1DArray(arr)) {
- return [this.$typeChecker(arr)];
- } else {
- const arrSlice = this.transposeArray(arr.slice(0, config.getDtypeTestLim));
- const dtypes = arrSlice.map((innerArr) => {
- return self.$typeChecker(innerArr);
- });
- return dtypes;
- }
- }
-
- /**
- * Private type checker used by inferDtype function
- * @param arr The array
- */
- $typeChecker(arr ) {
- let dtypes;
- let lim;
- let intTracker = [];
- let floatTracker = [];
- let stringTracker = [];
- let boolTracker = [];
-
- if (arr.length < config.getDtypeTestLim) {
- lim = arr.length;
- } else {
- lim = config.getDtypeTestLim;
- }
-
- const arrSlice = arr.slice(0, lim);
-
- for (let i = 0; i < lim; i++) {
- const ele = arrSlice[i];
- if (typeof ele == "boolean") {
- floatTracker.push(false);
- intTracker.push(false);
- stringTracker.push(false);
- boolTracker.push(true);
- } else if (this.isEmpty(ele)) {
- floatTracker.push(true);
- intTracker.push(false);
- stringTracker.push(false);
- boolTracker.push(false);
- } else if (!isNaN(Number(ele))) {
- if ((ele).toString().includes(".")) {
- floatTracker.push(true);
- intTracker.push(false);
- stringTracker.push(false);
- boolTracker.push(false);
- } else {
- floatTracker.push(false);
- intTracker.push(true);
- stringTracker.push(false);
- boolTracker.push(false);
- }
- } else {
- floatTracker.push(false);
- intTracker.push(false);
- stringTracker.push(true);
- boolTracker.push(false);
- }
- }
-
- const even = (ele) => ele == true;
-
- if (stringTracker.some(even)) {
- dtypes = "string";
- } else if (floatTracker.some(even)) {
- dtypes = "float32";
- } else if (intTracker.some(even)) {
- dtypes = "int32";
- } else if (boolTracker.some(even)) {
- dtypes = "boolean";
- } else {
- dtypes = "undefined";
- }
-
- return dtypes;
- }
-
- /**
- * Returns the unique values in an 1D array
- * @param arr The array
- */
- unique(arr) {
- const uniqueArr = new Set(arr);
- return Array.from(uniqueArr);
- }
-
- /**
- * Checks if array is 1D
- * @param arr The array
- */
- is1DArray(arr ) {
- if (
- typeof arr[0] == "number" ||
- typeof arr[0] == "string" ||
- typeof arr[0] == "boolean"
- ) {
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * Converts an array to an object using array index as object keys
- * @param arr The array
- */
- convertArrayToObject(arr ) {
- const arrObj = {};
- for (let i = 0; i < arr.length; i++) {
- arrObj[i] = arr[i];
-
- }
- return arrObj;
- }
-
- /**
- * Count the NaN and non-NaN values present in an array
- * @param arr Array object
- * @param val whether to return the value count instead of the null count
- * @param isSeries Whether the array is of type series or not
- */
- countNaNs(arr, returnVal = true, isSeries) {
- if (isSeries) {
- let nullCount = 0;
- let valCount = 0;
- for (let i = 0; i < arr.length; i++) {
- const ele = arr[i];
- if (Number.isNaN(ele)) {
- nullCount = nullCount + 1;
- } else {
- valCount = valCount + 1;
- }
-
- }
- if (returnVal) {
- return valCount;
- } else {
- return nullCount;
- }
- } else {
- const resultArr = [];
- for (let i = 0; i < arr.length; i++) {
- const innerArr = arr[i];
- let nullCount = 0;
- let valCount = 0;
- for (let i = 0; i < (innerArr).length; i++) {
- const ele = (innerArr)[i];
- if (Number.isNaN(ele)) {
- nullCount = nullCount + 1;
- } else {
- valCount = valCount + 1;
- }
- }
-
- if (returnVal) {
- resultArr.push(valCount);
- } else {
- resultArr.push(nullCount);
- }
- }
- return resultArr;
- }
- }
-
- /**
- * Round elements of an array or array of arrays to specified dp
- * @param arr The Array to round
- * @param dp The number of dp to round to
- * @param isSeries Whether the array is of type Series or not
- */
- round(arr, dp = 1, isSeries) {
- if (dp < 0) {
- dp = 1;
- }
-
- if (isSeries) {
- const newArr = [];
- for (let i = 0; i < arr.length; i++) {
- const ele = arr[i];
- if (typeof ele == "number" && !isNaN(ele) && ele !== undefined && ele !== null) {
- newArr.push(Number((ele).toFixed(dp)));
- } else {
- newArr.push(ele);
- }
- }
- return newArr;
- } else {
- const resultArr = [];
- for (let i = 0; i < arr.length; i++) {
- const innerVal = arr[i];
- const newArr = [];
- if (Array.isArray(innerVal)) {
- for (let i = 0; i < innerVal.length; i++) {
- const ele = innerVal[i];
- if (typeof ele == "number" && !isNaN(ele) && ele !== undefined && ele !== null) {
- newArr.push(Number((ele).toFixed(dp)));
- } else {
- newArr.push(ele);
- }
- }
- resultArr.push(newArr);
- } else {
- if (typeof innerVal == "number" && !isNaN(innerVal) && innerVal !== undefined && innerVal !== null) {
- newArr.push(Number((innerVal).toFixed(dp)));
- } else {
- newArr.push(innerVal);
- }
- }
-
- }
- return resultArr;
- }
- }
-
- /**
- * Checks if a func is a function
- * @param func
- */
- isFunction(func) {
- return typeof func == "function";
- }
-
- /**
- * Generates n random numbers between start and end.
- * @param start
- * @param end
- * @param size
- */
- randNumberGenerator(start, end, size) {
- let genNum = [];
-
- function randi(a, b) {
- return Math.floor(Math.random() * (b - a) + a);
- }
-
- function recursive(val, arr) {
- if (!arr.includes(val)) {
- return val;
- }
- val = randi(start, end);
- recursive(val, arr);
- }
-
- for (let i = 0; i < size; i++) {
- let genVal = randi(start, end);
- let recursiveVal = recursive(genVal, genNum);
- genNum.push(recursiveVal);
- }
- return genNum;
- }
-
- /**
- * Throws error when a required parameter is missing.
- * @param paramsObject The parameters passed to the function
- * @param paramsNeeded The required parameters in the function
- */
- throwErrorOnWrongParams(paramsObject, paramsNeeded) {
- const keys = Object.keys(paramsObject);
- const bool = [];
- for (let i = 0; i < keys.length; i++) {
- if (paramsNeeded.includes(keys[i])) {
- bool.push(true);
- } else {
- bool.push(false);
- }
- }
- const truthy = (element) => element == false;
- if (bool.some(truthy)) {
- throw Error(
- `Params Error: Required parameter not found. Your params must include the following [${paramsNeeded}]`
- );
- }
- }
-
- /**
- * Maps integer values (0, 1) to boolean (false, true)
- * @param arr The array of integers
- * @param dim The dimension of the array
- */
- mapIntegersToBooleans(arr, dim){
- if (dim == 2) {
- const newArr = [];
- arr.map((innerArr) => {
- const temp = [];
- (innerArr).map((val) => temp.push(val == 1));
- newArr.push(temp);
- });
- return newArr;
- } else {
- const newArr = [];
- arr.map((val) => newArr.push(val == 1));
- return newArr;
- }
- }
-
- /**
- * Maps boolean values (false, true) to integer equivalent (0, 1)
- * @param arr The array of booleans
- * @param dim The dimension of the array
- */
- mapBooleansToIntegers(arr, dim){
- if (dim == 2) {
- const newArr = [];
- arr.map((innerArr) => {
- const temp = [];
- (innerArr).map((val) => temp.push(val ? 1 : 0));
- newArr.push(temp);
- });
- return newArr;
- } else {
- const newArr = [];
- arr.map((val) => newArr.push(val ? 1 : 0));
- return newArr;
- }
- }
-
- /**
- * Generates an array of dim (row x column) with inner values set to zero
- * @param row
- * @param column
- */
- zeros(row, column) {
- const zeroData = [];
- for (let i = 0; i < row; i++) {
- const colData = Array(column);
- for (let j = 0; j < column; j++) {
- colData[j] = 0;
- }
- zeroData.push(colData);
- }
- return zeroData;
- }
-
- /**
- * Shuffles and returns a random slice of an array
- * @param num
- * @param array
- */
- shuffle(array, num) {
- let i = array.length;
- let j = 0;
- let temp;
-
- while (i--) {
- j = Math.floor(Math.random() * (i + 1));
- temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
-
- return array.slice(0, num);
- }
-
- /**
- * Sorts an array in specified order
- * @param arr
- * @param ascending
- * @returns
- */
- sort(arr, ascending = true) {
- const sorted = [...arr];
- return sorted.sort((a, b) => {
- if (ascending) {
- if (typeof a === "string" && typeof b === "string") {
- return a.charCodeAt(0) - b.charCodeAt(0);
- } else {
- return (a) - (b);
- }
- } else {
- if (typeof a === "string" && typeof b === "string") {
- return b.charCodeAt(0) - a.charCodeAt(0);
- } else {
- return (b) - (a);
- }
- }
- });
- }
-
- /**
- * Checks if current environment is Browser
- */
- isBrowserEnv() {
- const isBrowser = new Function(
- "try {return this===window;}catch(e){ return false;}"
- );
- return isBrowser();
- }
-
- /**
- * Checks if current environment is Node
- */
- isNodeEnv() {
- const isNode = new Function(
- "try {return this===global;}catch(e){return false;}"
- );
- return isNode();
- }
-
- /**
- * Remove NaN values from 1D array
- * @param arr
- */
- removeMissingValuesFromArray(arr) {
- const values = arr.filter((val) => {
- return !(this.isEmpty(val));
- });
- return values;
- }
-
-
- /**
- * Replace NaN with null before tensor operations
- * @param arr
- */
- replaceNanWithNull(arr ) {
- const values = arr.map((val) => {
- if (isNaN(val)) {
- return null;
- } else {
- return val;
- }
- });
- return values;
- }
-
- /**
- * Get duplicate values in a array
- * @param arr
- */
- getDuplicate(arr) {
- const tempObj = {};
- const resultObj = {};
-
- for (let i = 0; i < arr.length; i++) {
- const val = arr[i];
- if (this.keyInObject(tempObj, val)) {
- tempObj[val]["count"] += 1;
- tempObj[val]["index"].push(i);
- } else {
- tempObj[val] = {};
- tempObj[val]["count"] = 1;
- tempObj[val]["index"] = [i];
- }
- }
-
- for (let key in tempObj) {
- if (tempObj[key]["count"] >= 2) {
- resultObj[key] = {};
- resultObj[key]["count"] = tempObj[key]["count"];
- resultObj[key]["index"] = tempObj[key]["index"];
- }
- }
-
- return resultObj;
- }
-
- /**
- * Returns the index of a sorted array
- * @param arr1 The first array
- * @param arr2 The second array
- * @param dtype The data type of the arrays
- *
- * @returns sorted index
- */
- sortArrayByIndex(arr1, arr2, dtype) {
- const sortedIdx = arr1.map((item, index) => {
- return [arr2[index], item];
- });
- if (dtype == "string") {
- sortedIdx.sort();
- } else {
- sortedIdx.sort(([arg1], [arg2]) => (arg2) - (arg1));
- }
-
- return sortedIdx.map(([, item]) => item);
- }
-
- /**
- * Returns a new series with properties of the old series
- *
- * @param series The series to copy
- */
- createNdframeFromNewDataWithOldProps({ ndFrame, newData, isSeries }) {
- if (isSeries) {
- return new Series(
- newData,
- {
- index: [...ndFrame.index],
- columns: [...ndFrame.columns],
- dtypes: [...ndFrame.dtypes],
- config: { ...ndFrame.config }
- });
- } else {
- return new DataFrame(newData,
- {
- index: [...ndFrame.index],
- columns: [...ndFrame.columns],
- dtypes: [...ndFrame.dtypes],
- config: { ...ndFrame.config }
- });
- }
- }
-
- /**
- * Checks if two series are compatible for a mathematical operation
- * @param object
- *
- * firstSeries ==> First Series object
- *
- * secondSeries ==> Second Series object to comapre with
- *
- * operation ==> The mathematical operation
- */
- checkSeriesOpCompactibility({ firstSeries, secondSeries, operation }) {
-
- if (firstSeries.shape[0] != secondSeries.shape[0]) {
- ErrorThrower.throwSeriesMathOpLengthError(firstSeries, secondSeries);
- }
- if (firstSeries.dtypes[0] == 'string' || secondSeries.dtypes[0] == 'string') {
- ErrorThrower.throwStringDtypeOperationError(operation);
- }
- }
-
- /**
- * Custom sort for an array of index and values
- * @param arr The array of objects to sort
- * @param ascending Whether to sort in ascending order or not
- */
- sortObj(
- arr,
- ascending
- ) {
- return arr.sort((obj1, obj2) => {
- const a = obj2.value;
- const b = obj1.value;
-
- if (!ascending) {
- if (typeof a === "string" && typeof b === "string") {
- return a.charCodeAt(0) - b.charCodeAt(0);
- } else if ((typeof a === "number" && typeof b === "number") || (typeof a === "boolean" && typeof b === "boolean")) {
- return Number(a) - Number(b);
- } else {
- throw Error('ParamError: column values must be either numbers or strings');
- }
- } else {
- if (typeof a === "string" && typeof b === "string") {
- return b.charCodeAt(0) - a.charCodeAt(0);
- } else if ((typeof a === "number" && typeof b === "number") || (typeof a === "boolean" && typeof b === "boolean")) {
- return Number(b) - Number(a);
- } else {
- throw Error('ParamError: column values must be either numbers or strings');
- }
- }
- });
- }
-}
-
-const utils = new Utils();
-export { utils };
-
diff --git a/danfojs-browser/tests/core/frame.js b/danfojs-browser/tests/core/frame.js
deleted file mode 100644
index 8851345e..00000000
--- a/danfojs-browser/tests/core/frame.js
+++ /dev/null
@@ -1,2168 +0,0 @@
-/* eslint-disable no-undef */
-
-describe("DataFrame", function () {
-
- describe("to_csv", function () {
-
- it("return dataframe csv string", async function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- const csvContent = df.to_csv({ download: false });
- assert.deepEqual(csvContent, "A,B,C\n1,2,3\n4,5,6\n");
- });
-
- });
-
- describe("drop", function () {
- it("throw error for wrong row index", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- assert.throws(function () { df.drop({ columns: [3], axis: 0, inplace: false }); }, Error, 'ParamError: specified column "3" not found in columns');
- });
- it("throw error for wrong row index", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- assert.throws(function () { df.drop({ columns: ["D"], axis: 1, inplace: false }); }, Error, 'ParamError: specified column "D" not found in columns');
- });
-
- it("drop a column inplace", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- df.drop({ columns: ["C", "B"], axis: 1, inplace: true });
- let column = ["A"];
- assert.deepEqual(df.columns, column);
- });
- it("check if data is updated after column is dropped", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- df.drop({ columns: ["C"], axis: 1, inplace: true });
- let new_data = [[1, 2], [4, 5]];
- assert.deepEqual(df.values, new_data);
- assert.deepEqual(df.dtypes.length, 2);
-
- });
-
- it("check if data is updated after row is dropped", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- df.drop({ index: [0], axis: 0, inplace: true });
- let new_data = [[4, 5, 6]];
- assert.deepEqual(df.values, new_data);
- });
- it("check if new dataframe is properly created after column is dropped (not-in-inplace)", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let df_drop = df.drop({ columns: ["C"], axis: 1, inplace: false });
-
- let expected_data = [[1, 2], [4, 5]];
- let expected_cols = ["A", "B"];
- let expected_df = new dfd.DataFrame(expected_data, { columns: expected_cols });
- assert.deepEqual(df_drop.values, expected_df.values);
- });
- it("check that the dtype is updated after column drop", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- df.drop({ columns: ["A"], axis: 1, inplace: true });
- let dtype = ['int32', 'int32'];
- assert.deepEqual(df.ctypes.values, dtype);
- });
- it("drop row by single string labels", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 34, 5]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols, index: ["a", "b", "c"] });
- df.drop({ index: ["a"], axis: 0, inplace: true });
- let new_data = [[4, 5, 6], [20, 34, 5]];
- assert.deepEqual(df.values, new_data);
- });
- it("drop row by two or more string labels", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 34, 5], [2, 3.4, 5], [2.0, 340, 5]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols, index: ["a", "b", "c", "d", "e"] });
- df.drop({ index: ["a", "b"], axis: 0, inplace: true });
- let new_data = [[20, 34, 5], [2, 3.4, 5], [2.0, 340, 5]];
- assert.deepEqual(df.values, new_data);
-
- });
- });
-
- describe("head", function () {
- it("Gets the first n rows in a DataFrame", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- assert.deepEqual(df.head(2).values, [[1, 2, 3], [4, 5, 6]]);
- });
-
- });
-
- describe("tail", function () {
- it("Prints the last n rows of a DataFrame", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- assert.deepEqual(df.tail(2).values, [[20, 30, 40], [39, 89, 78]]);
- });
- it("Return last 3 row index in a DataFrame", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- assert.deepEqual(df.tail(2).index, [2, 3]);
- });
- it("Check print format on head call", function () {
- let data = [[1, 2, 34, 5, 0, 6, 4, 5, 6, 7], [20, 30, 40, 39, 89, 78, 45, 56, 56, 45]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.tail(2).values, [[1, 2, 34, 5, 0, 6, 4, 5, 6, 7], [20, 30, 40, 39, 89, 78, 45, 56, 56, 45]]);
- });
- });
-
- describe("sample", function () {
- it("Samples n number of random elements from a DataFrame", async function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78], [100, 200, 300]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let expected = [[1, 2, 3], [20, 30, 40]];
- let values = (await df.sample(2)).values;
- assert.deepEqual(values, expected);
- });
- it("Throw error if n is greater than lenght of Dataframe", async function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78], [100, 200, 300]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- try {
- await df.sample(100);
- } catch (e) {
- expect(e).to.be.instanceOf(Error);
- expect(e.message).to.eql('ParamError: Sample size cannot be bigger than number of rows');
- }
- });
- it("Throw error if n is less than -1", async function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78], [100, 200, 300]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- try {
- await df.sample(-2);
- } catch (e) {
- expect(e).to.be.instanceOf(Error);
- expect(e.message).to.eql('ParamError: Sample size cannot be less than 1');
- }
- });
- it("Throw error if n is 0", async function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78], [100, 200, 300]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- try {
- await df.sample(0);
- } catch (e) {
- expect(e).to.be.instanceOf(Error);
- expect(e.message).to.eql('ParamError: Sample size cannot be less than 1');
- }
- });
- });
-
- describe("loc", function () {
-
- it("throw error for wrong column name", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- assert.throws(function () { df.loc({ "rows": [0, 1], "columns": ["A", "D"] }); }, Error, "ColumnIndexError: Specified column (D) not found");
- });
- it("check data after selecting column", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.loc({ "rows": [0, 1], "columns": ["B", "C"] });
- let col_data = [[2, 3], [5, 6]];
-
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("check data after selecting row index", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.loc({ "rows": [1], "columns": ["B", "C"] });
- let col_data = [[5, 6]];
-
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("check data after row and column slice", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.loc({ "rows": ["0:2"], "columns": ["B:C"] });
- let col_data = [[2], [5]];
-
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("check data after row slice", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.loc({ "rows": ["0:2"], "columns": ["B", "C"] });
- let col_data = [[2, 3], [5, 6]];
-
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("check data after column slice", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.loc({ "rows": [0, 1], "columns": ["A:C"] });
- let col_data = [[1, 2], [4, 5]];
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("loc by single string index", function () {
- let data = {
- "Name": ["Apples", "Mango", "Banana", "Pear"],
- "Count": [21, 5, 30, 10],
- "Price": [200, 300, 40, 250]
- };
-
- let df = new dfd.DataFrame(data, { index: ["a", "b", "c", "d"] });
- let sub_df = df.loc({ rows: ["a"], columns: ["Name", "Count"] });
- let expected = [["Apples", 21]];
- assert.deepEqual(sub_df.values, expected);
-
- });
- it("loc by multiple string index", function () {
- let data = {
- "Name": ["Apples", "Mango", "Banana", "Pear"],
- "Count": [21, 5, 30, 10],
- "Price": [200, 300, 40, 250]
- };
-
- let df = new dfd.DataFrame(data, { index: ["a", "b", "c", "d"] });
- let sub_df = df.loc({ rows: ["a", "b"], columns: ["Name", "Count"] });
- let expected = [["Apples", 21], ["Mango", 5]];
- assert.deepEqual(sub_df.values, expected);
-
- });
- it("loc by slice string index", function () {
- let data = {
- "Name": ["Apples", "Mango", "Banana", "Pear"],
- "Count": [21, 5, 30, 10],
- "Price": [200, 300, 40, 250]
- };
-
- let df = new dfd.DataFrame(data, { index: ["a", "b", "c", "d"] });
- let sub_df = df.loc({ rows: [`"a":"c"`], columns: ["Name", "Count"] });
- let expected = [["Apples", 21], ["Mango", 5]];
- assert.deepEqual(sub_df.values, expected);
-
- });
-
-
- });
-
- describe("iloc", function () {
-
- it("throw error for wrong row index", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- assert.throws(function () { df.iloc({ "rows": 0, "columns": [0, 3] }); }, Error, `rows parameter must be an Array. For example: rows: [1,2] or rows: ["0:10"]`);
- });
-
- it("throw error for wrong column indexing", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- assert.throws(function () { df.iloc({ "rows": [0, 1], "columns": 3 }); }, Error, `columns parameter must be an Array. For example: columns: [1,2] or columns: ["0:10"]`);
- });
-
- it("throw error for wrong row index", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- assert.throws(function () { df.iloc({ "rows": [0, 8], "columns": [1, 2] }); }, Error, "Invalid row parameter: Specified index 8 cannot be bigger than index length 2");
- });
-
- it("check data after selecting column", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.iloc({ "rows": [0, 1], "columns": [1, 2] });
- let col_data = [[2, 3], [5, 6]];
-
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("check data after selecting row index", function () {
- let data = [[1, 2, 3], [4, 5, 6]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.iloc({ "rows": [1], "columns": [1, 2] });
- let col_data = [[5, 6]];
-
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("check data after row and column slice", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.iloc({ "rows": ["0:2"], "columns": ["1:2"] });
- let col_data = [[2], [5]];
-
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("check data after row slice", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.iloc({ "rows": ["0:2"], "columns": [1, 2] });
- let col_data = [[2, 3], [5, 6]];
-
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("check data after column slice", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.iloc({ "rows": [0, 1, 2], "columns": ["1:2"] });
- let col_data = [[2], [5], [30]];
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("Return all columns if columns parameter is not specified", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.iloc({ "rows": [0, 1, 2] });
- let col_data = [[1, 2, 3], [4, 5, 6], [20, 30, 40]];
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("Return all rows if rows parameter is not specified", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let col_df = df.iloc({ "columns": ["1:2"] });
- let col_data = [[2], [5], [30], [89]];
- assert.deepEqual(col_df.values, col_data);
-
- });
- it("column slice starting with 0 and returning a single result works", function () {
- let data = {
- "Name": ["Apples", "Mango", "Banana", "Pear"],
- "Count": [21, 5, 30, 10],
- "Price": [200, 300, 40, 250]
- };
- let df = new dfd.DataFrame(data);
- let sub_df = df.iloc({ rows: ["2:3"], columns: ["0:1"] });
- const result = [["Banana"]];
- assert.deepEqual(sub_df.values, result);
-
- });
- it("column slice with format '0:' works", function () {
- let data = {
- "Name": ["Apples", "Mango", "Banana", "Pear"],
- "Count": [21, 5, 30, 10],
- "Price": [200, 300, 40, 250]
- };
- let df = new dfd.DataFrame(data);
- let sub_df = df.iloc({ rows: ["2:3"], columns: ["0:"] });
- const result = [["Banana", 30, 40]];
- assert.deepEqual(sub_df.values, result);
-
- });
- it("column slice with format ':2' works", function () {
- let data = {
- "Name": ["Apples", "Mango", "Banana", "Pear"],
- "Count": [21, 5, 30, 10],
- "Price": [200, 300, 40, 250]
- };
- let df = new dfd.DataFrame(data);
- let sub_df = df.iloc({ rows: ["2:3"], columns: [":2"] });
- const result = [["Banana", 30]];
- assert.deepEqual(sub_df.values, result);
-
- });
- it("row slice with format ':2' works", function () {
- let data = {
- "Name": ["Apples", "Mango", "Banana", "Pear"],
- "Count": [21, 5, 30, 10],
- "Price": [200, 300, 40, 250]
- };
- let df = new dfd.DataFrame(data);
- let sub_df = df.iloc({ rows: [":2"], columns: [":1"] });
- const result = [['Apples'], ['Mango']];
- assert.deepEqual(sub_df.values, result);
-
- });
- it("row slice with format '1:' works", function () {
- let data = {
- "Name": ["Apples", "Mango", "Banana", "Pear"],
- "Count": [21, 5, 30, 10],
- "Price": [200, 300, 40, 250]
- };
- let df = new dfd.DataFrame(data);
- let sub_df = df.iloc({ rows: [":2"], columns: [":2"] });
- const result = [['Apples', 21], ['Mango', 5]];
- assert.deepEqual(sub_df.values, result);
-
- });
-
- });
-
-
- describe("add", function () {
- it("Return Addition of DataFrame with a single Number", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.add(2).values, [[2, 4, 6], [362, 182, 362]]);
- });
- it("Return addition of a DataFrame with a Series along default axis 1", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let sf = new dfd.Series([1, 2, 1]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.add(sf).values, [[1, 4, 5], [361, 182, 361]]);
- });
- it("Return addition of a DataFrame with a Series along axis 0", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let sf = new dfd.Series([1, 2]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.add(sf, { axis: 0 }).values, [[1, 3, 5], [362, 182, 362]]);
- });
- it("Return addition of a DataFrame with a DataFrame along default axis 1", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [360, 180, 360]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.add(df2).values, [[1, 4, 8], [370, 185, 360]]);
- });
- it("Return addition of a DataFrame with a DataFrame along axis 0", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [360, 180, 360]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.add(df2, { axis: 0 }).values, [[1, 4, 8], [370, 185, 360]]);
- });
-
- });
-
- describe("sub", function () {
- it("Return subtraction of DataFrame with a single Number", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.sub(2).values, [[-2, 0, 2], [358, 178, 358]]);
- });
- it("Return subtraction of a DataFrame with a Series along default axis 1", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let sf = new dfd.Series([1, 2, 1]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.sub(sf).values, [[-1, 0, 3], [359, 178, 359]]);
- });
- it("Return subtraction of a DataFrame with a Series along axis 0", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let sf = new dfd.Series([1, 2]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.sub(sf, { axis: 0 }).values, [[-1, 1, 3], [358, 178, 358]]);
- });
- it("Return subtraction of a DataFrame with a DataFrame along default axis 1", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [360, 180, 360]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.sub(df2).values, [[-1, 0, 0], [350, 175, 360]]);
- });
- it("Return subtraction of a DataFrame with a DataFrame along axis 0", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [360, 180, 360]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.sub(df2).values, [[-1, 0, 0], [350, 175, 360]]);
- });
-
- });
-
- describe("mul", function () {
- it("Return multiplication of DataFrame with a single Number", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.mul(2).values, [[0, 4, 8], [720, 360, 720]]);
- });
- it("Return multiplication of a DataFrame with a Series along default axis 1", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let sf = new dfd.Series([1, 2, 1]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.mul(sf).values, [[0, 4, 4], [360, 360, 360]]);
- });
- it("Return multiplication of a DataFrame with a Series along axis 0", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let sf = new dfd.Series([1, 2]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.mul(sf, { axis: 0 }).values, [[0, 2, 4], [720, 360, 720]]);
- });
- it("Return multiplication of a DataFrame with a DataFrame along default axis 1", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [360, 180, 360]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.mul(df2).values, [[0, 4, 16], [3600, 900, 0]]);
- });
- it("Return multiplication of a DataFrame with a DataFrame along axis 0", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [360, 180, 360]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.mul(df2, { axis: 0 }).values, [[0, 4, 16], [3600, 900, 0]]);
- });
-
- });
-
- describe("div", function () {
- it("Return division of DataFrame with a single Number", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.div(2).values, [[0, 1, 2], [180, 90, 180]]);
- });
- it("Return division of a DataFrame with a Series along default axis 1", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let sf = new dfd.Series([1, 2, 1]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.div(sf).values, [[0, 1, 4], [360, 90, 360]]);
- });
- it("Return division of a DataFrame with a Series along axis 0", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let sf = new dfd.Series([1, 2]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.div(sf, { axis: 0 }).values, [[0, 2, 4], [180, 90, 180]]);
- });
- it("Return division of a DataFrame with a DataFrame along default axis 1", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [360, 180, 360]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.div(df2).values, [[0, 1, 1], [36, 36, Infinity]]);
- });
- it("Return division of a DataFrame with a DataFrame along axis 0", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [360, 180, 360]]);
- assert.deepEqual(df1.div(df1).values, [[NaN, 1, 1], [1, 1, 1]]);
- });
- it("Return division of a DataFrame with a DataFrame along axis 0", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [360, 180, 360]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.div(df2).values, [[0, 1, 1], [36, 36, Infinity]]);
- });
-
- });
-
- describe("pow", function () {
- it("Return exponential of DataFrame with a single Number", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.pow(2).values, [[0, 4, 16], [129600, 32400, 129600]]);
- });
- it("Return exponential of a DataFrame with a Series along default axis 1", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let sf = new dfd.Series([1, 2, 1]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.pow(sf).values, [[0, 4, 4], [360, 32400, 360]]);
- });
- it("Return exponential of a DataFrame with a Series along axis 0", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let sf = new dfd.Series([1, 2]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.pow(sf, { axis: 0 }).values, [[0, 2, 4], [129600, 32400, 129600]]);
- });
- it("Return exponential of a DataFrame with another DataFrame along default axis 1", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [3, 10, 4]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.pow(df2).values, [[0, 4, 256], [59049, 100000, 1]]);
- });
- it("Return exponential of a DataFrame with another DataFrame along axis 0", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [3, 10, 4]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.pow(df2, { axis: 0 }).values, [[0, 4, 256], [59049, 100000, 1]]);
- });
-
- });
-
- describe("mod", function () {
- it("Return modulus of DataFrame with a single Number", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.mod(2).values, [[0, 0, 0], [0, 0, 0]]);
- });
- it("Return modulus of a DataFrame with a Series along default axis 1", function () {
- let data = [[0, 2, 4], [31, 15, 360]];
- let sf = new dfd.Series([1, 2, 1]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.mod(sf).values, [[0, 0, 0], [0, 1, 0]]);
- });
- it("Return modulus of a DataFrame with a Series along axis 0", function () {
- let data = [[0, 2, 4], [31, 15, 360]];
- let sf = new dfd.Series([1, 2]);
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.mod(sf, { axis: 0 }).values, [[0, 0, 0], [1, 1, 0]]);
- });
- it("Return modulus of a DataFrame with a DataFrame along default axis 1", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [31, 15, 360]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.mod(df2).values, [[0, 0, 0], [1, 0, NaN]]);
- });
- it("Return modulus of a DataFrame with a DataFrame along axis 0", function () {
- let df1 = new dfd.DataFrame([[0, 2, 4], [360, 180, 360]]);
- let df2 = new dfd.DataFrame([[1, 2, 4], [10, 5, 0]]);
- assert.deepEqual(df1.mod(df2).values, [[0, 0, 0], [0, 0, NaN]]);
- });
-
- });
-
- describe("mean", function () {
- it("Returns the mean of a DataFrame (Default axis is [1:column])", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data, { columns: ["col1", "col2", "col3"] });
- assert.deepEqual(df.mean().values, [180, 91, 182]);
- assert.deepEqual(df.mean().index, ["col1", "col2", "col3"]);
-
- });
- it("Return mean of a DataFrame along axis 0 (row)", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.mean({ axis: 0 }).values, [2, 300]);
- });
- it("Removes NaN before calculating mean of a DataFrame", function () {
- let data = [[11, 20, 3], [NaN, 15, 6], [2, 30, 40], [2, 89, 78]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.mean().values, [5, 38.5, 31.75]);
- });
- });
-
- describe("median", function () {
- it("Returns the median of a DataFrame (Default axis is [1:column])", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.median().values, [180, 91, 182]);
- });
- it("Return median of a DataFrame along axis 0 (row)", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.median({ "axis": 0 }).values, [2, 360]);
- });
-
- });
-
- describe("mode", function () {
- it("Returns the mode of a DataFrame (Default axis is [1:column])", function () {
- let data = [[0, 2, 4], [360, 180, 360], [0, 2, 4]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.mode().values, [0, 2, 4]);
- });
- it("Returns mode of a DataFrame along axis 0 (row)", function () {
- let data = [[0, 2, 0], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.mode({ "axis": 0 }).values, [0, 360]);
- });
- });
-
- describe("min", function () {
- it("Returns the minimum values in a DataFrame (Default axis is [1:column])", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.min().values, [0, 2, 4]);
- });
- it("Returns the minimum values of a DataFrame along axis 0 (row)", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.min({ "axis": 0 }).values, [0, 180]);
- });
-
- });
-
- describe("max", function () {
- it("Returns the maximum values in a DataFrame (Default axis is [1:column])", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.max().values, [360, 180, 360]);
- });
- it("Returns the maximum values of a DataFrame along axis 0 (row)", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.max({ "axis": 0 }).values, [4, 360]);
- });
-
- });
-
- describe("std", function () {
- it("Returns the standard deviations of values in a DataFrame (Default axis is [1:column])", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.std().values, [254.55844122715712, 125.86500705120545, 251.7300141024109]);
- });
- it("Return the standard deviations of values of a DataFrame along axis 0 (row)", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.std({ "axis": 0 }).values, [2, 103.92304845413264]);
- });
-
-
- });
-
- describe("var", function () {
- it("Returns the variance of values in a DataFrame (Default axis is [1:column])", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.var().values, [64800, 15842, 63368]);
- });
- it("Return the variance of values of a DataFrame along axis 0 (row)", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.var({ "axis": 0 }).values, [4, 10800]);
- });
-
-
- });
-
- describe("describe", function () {
- it("Returns descriptive statistics of columns in a DataFrame created from an array", function () {
- let data = [[0, 2, 4, "a"],
- [360, 180, 360, "b"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data);
- let res = [[3, 3, 3], [120.66666666666667, 62, 123.33333333333333],
- [207.27115895206774, 102.19589032832974, 204.961785055979],
- [0, 2, 4], [2, 4, 6],
- [360, 180, 360],
- [42961.33333333333, 10444, 42009.333333333336]];
-
- assert.deepEqual(df.describe().values, res);
- });
- it("Returns descriptive statistics of columns in a DataFrame created from an Object", function () {
- let data = {
- "col1": [0, 2, 4],
- "col2": [360, 180, 360],
- "col3": [2, 4, 6],
- "col4": ["boy", "girl", "man"],
- "col5": ["apple", "car", "bee"]
- };
- let df = new dfd.DataFrame(data);
-
- let res = [[3, 3, 3], [2, 300, 4],
- [2, 103.92304845413264, 2],
- [0, 180, 2], [2, 360, 4],
- [4, 360, 6],
- [4, 10800, 4]];
-
- assert.deepEqual(df.describe().values, res);
- });
-
- });
-
- describe("count", function () {
- it("Returns the count of non-nan values in a DataFrame (Default axis is [1:column])", function () {
- let data = [[0, 2, 4], [360, 180.1, 360.11], [NaN, 2, 4], [360, undefined, 360]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.count().values, [3, 3, 4]);
- });
- it("Return the count of non NaN values of a DataFrame along axis 0", function () {
- let data = [[0, 2, 4, NaN], [360, undefined, 360, 70]];
- let df = new dfd.DataFrame(data);
- assert.deepEqual(df.count({ axis: 0 }).values, [3, 3]);
- });
-
- });
-
- describe("round", function () {
- it("Rounds values in a DataFrame to 3dp", function () {
- let data = [[10.1, 2.092, 4.23], [360.232244, 180.0190290, 36.902612]];
- let df = new dfd.DataFrame(data);
- let expected = [[10.1, 2.092, 4.23], [360.232, 180.0190, 36.903]];
- assert.deepEqual(df.round(3).values, expected);
- });
- it("Rounds values in a DataFrame to 1dp", function () {
- let data = [[10.1, 2.092, 4.23], [360.232244, 180.0190290, 36.902612]];
- let df = new dfd.DataFrame(data);
- let expected = [[10.1, 2.1, 4.2], [360.2, 180.0, 36.9]];
- assert.deepEqual(df.round(1).values, expected);
- });
-
- });
-
- describe("sort_values", function () {
- it("Sort values in DataFrame by specified column in ascending order (Default)", function () {
- let data = [[0, 2, 4, "a"],
- [360, 180, 360, "b"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] });
- df.sort_values({ "by": "col1", inplace: true, ascending: true });
- let expected = [[0, 2, 4, "a"], [2, 4, 6, "c"], [360, 180, 360, "b"]];
- assert.deepEqual(df.values, expected);
- assert.deepEqual(df.index, [0, 2, 1]);
-
- });
-
- it("Sort values in DataFrame by specified column in ascending order (Default)", function () {
- let data = [[0, 2, 4, "a"],
- [360, 180, 1, "b"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] });
- let df_sort = df.sort_values({ "by": "col3" });
- let expected = [[360, 180, 1, "b"], [0, 2, 4, "a"], [2, 4, 6, "c"]];
- assert.deepEqual(df_sort.values, expected);
- assert.deepEqual(df_sort.index, [1, 0, 2]);
-
- });
- it("Sort values in DataFrame by specified column in descending order", function () {
- let data = [[0, 2, 4, "a"],
- [360, 180, 360, "b"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] });
- let expected = [[360, 180, 360, "b"], [2, 4, 6, "c"], [0, 2, 4, "a"]];
- assert.deepEqual(df.sort_values({ "by": "col1", "ascending": false }).values, expected);
- });
-
- it("Sort values in DataFrame by specified column in descending order (second col)", function () {
- let data = [[0, 2, 4, "a"],
- [360, 180, 1, "b"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] });
- let expected = [[2, 4, 6, "c"], [0, 2, 4, "a"], [360, 180, 1, "b"]];
- assert.deepEqual(df.sort_values({ "by": "col3", "ascending": false }).values, expected);
- });
- it("Sort values in DataFrame by specified column containing alpha(numeric) values", function () {
- let data = [[0, 2, 4, "a"],
- [360, 180, 1, "b"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] });
- let expected = [[2, 4, 6, 'c'], [360, 180, 1, 'b'], [0, 2, 4, 'a']];
- assert.deepEqual(df.sort_values({ "by": "col4", "ascending": false }).values, expected);
- });
- it("Sort duplicate DataGrame with duplicate columns", function () {
-
- let data = {
- "A": [1, 2, 3, 4, 5, 3, 5, 6, 4, 5, 3, 4],
- "B": [2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4]
- };
-
- let df = new dfd.DataFrame(data);
- let expected = [[1, 2],
- [2, 3],
- [3, 4],
- [3, 7],
- [3, 3],
- [4, 5],
- [4, 1],
- [4, 4],
- [5, 6],
- [5, 8],
- [5, 2],
- [6, 9]];
- assert.deepEqual(df.sort_values({ "by": "A", "ascending": true }).values, expected);
- });
-
-
- });
-
- describe("copy", function () {
- it("Makes a deep copy of DataFrame", function () {
- let data = [[0, 2, 4], [360, 180, 360]];
- let df = new dfd.DataFrame(data);
- let df_copy = df.copy();
- assert.deepEqual(df_copy.values, [[0, 2, 4], [360, 180, 360]]);
- });
- });
-
-
- describe("set_index", function () {
- it("Sets the index of a DataFrame created from an Object", function () {
- let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }];
- let df = new dfd.DataFrame(data);
- let df_new = df.set_index({ "index": ["one", "two", "three"] });
- assert.deepEqual(df_new.index, ["one", "two", "three"]);
- });
- it("Sets the index of a DataFrame from column name", function () {
- let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }];
- let df = new dfd.DataFrame(data);
- let df_new = df.set_index({ "column": "alpha" });
- assert.deepEqual(df_new.index, ["A", "B", "C"]);
- });
- it("Sets the index of a DataFrame from column name", function () {
- let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }];
- let df = new dfd.DataFrame(data);
- let df_new = df.set_index({ column: "alpha", drop: true });
- assert.deepEqual(df_new.index, ["A", "B", "C"]);
- });
- it("Sets the index of a DataFrame created from an Array", function () {
- let data = [[0, 2, 4], [360, 180, 360], [0, 2, 4], [360, 180, 360], [0, 2, 4]];
- let df = new dfd.DataFrame(data);
- df.set_index({ "index": ["one", "two", "three", "four", "five"], "inplace": true });
- assert.deepEqual(df.index, ["one", "two", "three", "four", "five"]);
- });
-
- });
-
- describe("reset_index", function () {
- it("Resets the index of a DataFrame created from an Object", function () {
- let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }];
- let df = new dfd.DataFrame(data);
- let df_new = df.set_index({ "index": ["one", "two", "three"] });
- let df_reset = df_new.reset_index();
- assert.deepEqual(df_reset.index, [0, 1, 2]);
- });
- it("Resets the index of a DataFrame created from an Array", function () {
- let data = [[0, 2, 4], [360, 180, 360], [0, 2, 4], [360, 180, 360], [0, 2, 4]];
- let df = new dfd.DataFrame(data);
- df.set_index({ "index": ["one", "two", "three", "four", "five"], "inplace": true });
- df.reset_index({ inplace: true });
- assert.deepEqual(df.index, [0, 1, 2, 3, 4]);
- });
-
- });
-
-
- describe("apply_map", function () {
- it("Apply a function to all values of a DataFrame", function () {
- let data = [[0, 2, 4],
- [360, 180, 360],
- [0, 2, 4]];
- let df = new dfd.DataFrame(data);
-
- let apply_func = (x) => {
- return x + 1000;
- };
- let expected = [[1000, 1002, 1004], [1360, 1180, 1360], [1000, 1002, 1004]];
- assert.deepEqual(df.apply_map(apply_func).values, expected);
- });
- });
-
-
- describe("query", function () {
-
- it("Get the DataFrame containing rows with the filtered column", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let query_df = df.query({ condition: df["B"].ge(5) });
- let query_data = [[4, 5, 6], [20, 30, 40], [39, 89, 78]];
- assert.deepEqual(query_df.values, query_data);
- });
- it("Get the Dataframe containing rows with the filtered column in String values", function () {
- let data = { "Abs": [20, 30, 47], "Count": [34, 4, 5], "country code": ["NG", "FR", "GH"] };
- let cols = ["Abs", "Count", "country code"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let query_df = df.query({ condition: df["country code"].eq("NG") });
- let query_data = [[20, 34, "NG"]];
- assert.deepEqual(query_df.values, query_data);
- });
-
- it("Confirms that query index are updated", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let df_query = df.query({ condition: df["B"].ge(5) });
- assert.deepEqual(df_query.index, [1, 2, 3]);
- });
-
- it("Confirms that query happens inplace", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- df.query({ condition: df["B"].ge(5), inplace: true });
- let query_data = [[4, 5, 6], [20, 30, 40], [39, 89, 78]];
- assert.deepEqual(df.values, query_data);
- });
-
-
- it("Get the DataFrame containing rows with the filtered column (Without Condition)", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let query_df = df.query({ "column": "B", "is": ">=", "to": 5 });
- let query_data = [[4, 5, 6], [20, 30, 40], [39, 89, 78]];
- assert.deepEqual(query_df.values, query_data);
- });
- it("Get the Dataframe containing rows with the filtered column in String values (Without Condition)", function () {
- let data = { "Abs": [20, 30, 47], "Count": [34, 4, 5], "country code": ["NG", "FR", "GH"] };
- let cols = ["Abs", "Count", "country code"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let query_df = df.query({ column: "country code", is: "==", to: "NG" });
- let query_data = [[20, 34, "NG"]];
- assert.deepEqual(query_df.values, query_data);
- });
- it("Print Error for value key not specified (Without Condition)", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- assert.throws(function () { df.query({ "column": "B", "is": ">=" }); }, Error, "ParamError: specify a value to query by");
- });
- it("Print Error for operator key not specified (Without Condition)", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- assert.throws(function () { df.query({ "column": "B", "to": 5 }); }, Error, "ParamError: specify an operator to apply. operator must be one of >,<,<=,>=,==,!=");
- });
-
- it("Print Error for column key not specified (Without Condition)", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- assert.throws(function () { df.query({ "is": ">=", "to": 5 }); }, Error, "ParamError: specify a column name to query");
- });
- it("Print Error for column name not in dataframe (Without Condition)", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- assert.throws(function () { df.query({ "column": "D", "is": ">=", "to": 5 }); }, Error, "ParamError: column D not found in column names");
- });
- it("Confirms that query index are updated (Without Condition)", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let df_query = df.query({ "column": "B", "is": ">=", "to": 5 });
- assert.deepEqual(df_query.index, [1, 2, 3]);
- });
- it("Confirms that columns data are updated inplace (Without Condition)", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- df.query({ "column": "B", "is": ">=", "to": 5, inplace: true });
- assert.deepEqual(df.getColumnData, [[4, 20, 39], [5, 30, 89], [6, 40, 78]]);
- });
- it("Confirms that query happens inplace (Without Condition)", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- df.query({ "column": "B", "is": ">=", "to": 5, inplace: true });
- let query_data = [[4, 5, 6], [20, 30, 40], [39, 89, 78]];
- assert.deepEqual(df.values, query_data);
- });
- it("Confirms that query happens inplace and index are updated (Without Condition)", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- df.query({ "column": "B", "is": ">=", "to": 5, inplace: true });
- assert.deepEqual(df.index, [1, 2, 3]);
- });
- });
-
- describe("addColumn", function () {
- it("Print the data, after changing a column data", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let new_col = [1, 2, 3, 4];
-
- df.addColumn({ "column": "C", "values": new_col });
-
- let new_data = [[1, 2, 1], [4, 5, 2], [20, 30, 3], [39, 89, 4]];
-
- assert.deepEqual(df.values, new_data);
- });
- it("Print the Dataframe column names, after a new column is added ", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let new_col = [1, 2, 3, 4];
-
- df.addColumn({ "column": "D", "values": new_col, inplace: true });
-
- let new_column = ["A", "B", "C", "D"];
-
- assert.deepEqual(df.columns, new_column);
- });
- it("Print Error for column name not in keyword passed", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let new_col = [1, 2, 3, 4];
-
- assert.throws(function () { df.addColumn({ "values": new_col, inplace: true }); }, Error, "ParamError: column must be specified");
- });
- it("Check if new column value length is the same with Dataframe length", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let new_col = [1, 2, 3];
- assert.throws(function () { df.addColumn({ "column": "D", "values": new_col, inplace: true }); }, Error, "ParamError: Column data length mismatch. You provided data with length 3 but Ndframe has column of lenght 3");
- });
-
- it("Check that dtype is updated after a new column is added ", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let new_col = ["n", "b", "c", "f"];
-
- df.addColumn({ "column": "D", "values": new_col, inplace: true });
- let dtype = ["int32", "int32", "int32", "string"];
-
- assert.deepEqual(df.dtypes, dtype);
- });
-
- it("add series as value to a new column ", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let sf = new dfd.Series(["n", "b", "c", "f"]);
-
- let newdf = df.addColumn({ "column": "D", "values": sf });
- let dtype = ["int32", "int32", "int32", "string"];
-
- assert.deepEqual(newdf.dtypes, dtype);
- });
- it("Confirms that column names are not changed", function () {
-
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let df_query = df.query({ "column": "B", "is": ">=", "to": 5 });
- assert.deepEqual(df_query.index, [1, 2, 3]);
- assert.deepEqual(df_query.columns, ["A", "B", "C"]);
- });
-
- });
-
- // describe("groupby", function () {
- // it("Check group by One column data", function () {
-
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- // let cols = ["A", "B", "C"];
- // let df = new dfd.DataFrame(data, { columns: cols });
- // let group_df = df.groupby(["A"]);
-
- // let group_dict = {
- // '1': [[1, 2, 3]],
- // '4': [[4, 5, 6]],
- // '20': [[20, 30, 40]],
- // '39': [[39, 89, 78]]
- // };
-
- // assert.deepEqual(group_df.col_dict, group_dict);
- // });
- // it("Obtain the DataFrame of one of the group", function () {
-
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- // let cols = ["A", "B", "C"];
- // let df = new dfd.DataFrame(data, { columns: cols });
- // let group_df = df.groupby(["A"]);
- // let new_data = [[1, 2, 3]];
-
- // assert.deepEqual(group_df.get_groups([1]).values, new_data);
- // });
- // it("Check group by Two column data", function () {
-
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- // let cols = ["A", "B", "C"];
- // let df = new dfd.DataFrame(data, { columns: cols });
- // let group_df = df.groupby(["A", "B"]);
- // let new_data = {
- // '1': { '2': [[1, 2, 3]] },
- // '4': { '5': [[4, 5, 6]] },
- // '20': { '30': [[20, 30, 40]] },
- // '39': { '89': [[39, 89, 78]] }
- // };
-
- // assert.deepEqual(group_df.col_dict, new_data);
- // });
-
- // it("Obtain the DataFrame of one of the group, grouped by two column", function () {
-
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- // let cols = ["A", "B", "C"];
- // let df = new dfd.DataFrame(data, { columns: cols });
- // let group_df = df.groupby(["A", "B"]);
- // let new_data = [[1, 2, 3]];
-
- // assert.deepEqual(group_df.get_groups([1, 2]).values, new_data);
- // });
-
- // it("Count column in group", function () {
-
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- // let cols = ["A", "B", "C"];
- // let df = new dfd.DataFrame(data, { columns: cols });
- // let group_df = df.groupby(["A", "B"]);
- // let new_data = {
- // '1': { '2': [1] },
- // '4': { '5': [1] },
- // '20': { '30': [1] },
- // '39': { '89': [1] }
- // };
-
- // assert.deepEqual(group_df.col(["C"]).count(), new_data);
- // });
- // it("sum column element in group", function () {
-
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- // let cols = ["A", "B", "C"];
- // let df = new dfd.DataFrame(data, { columns: cols });
- // let group_df = df.groupby(["A", "B"]);
- // let new_data = {
- // '1': { '2': [3] },
- // '4': { '5': [6] },
- // '20': { '30': [40] },
- // '39': { '89': [78] }
- // };
-
- // assert.deepEqual(group_df.col(["C"]).sum(), new_data);
- // });
-
- // it("sum column element group by one column", function () {
-
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- // let cols = ["A", "B", "C"];
- // let df = new dfd.DataFrame(data, { columns: cols });
- // let group_df = df.groupby(["A"]);
-
- // let new_data = { '1': [2, 3], '4': [5, 6], '20': [30, 40], '39': [89, 78] };
-
- // assert.deepEqual(group_df.col(["B", "C"]).sum(), new_data);
- // });
-
- // it("Perform aggregate on column for groupby", function () {
-
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- // let cols = ["A", "B", "C"];
- // let df = new dfd.DataFrame(data, { columns: cols });
- // let group_df = df.groupby(["A", "B"]);
- // let new_data = {
- // '1': { '2': [2, 1] },
- // '4': { '5': [5, 1] },
- // '20': { '30': [30, 1] },
- // '39': { '89': [89, 1] }
- // };
-
- // assert.deepEqual(group_df.agg({ "B": "mean", "C": "count" }), new_data);
- // });
-
-
- // });
-
- // describe("column", function () {
- // it("Obtain a column from a dataframe created from object", function () {
- // let data = [{ alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 }];
- // let options = { columns: ["Gender", "count"] };
- // let df = new dfd.DataFrame(data, options);
- // let col_data = df.column("count");
- // let rslt_data = [1, 2, 3];
- // assert.deepEqual(col_data.values, rslt_data);
- // });
- // it("Obtain a column from a dataframe", function () {
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- // let cols = ["A", "B", "C"];
- // let df = new dfd.DataFrame(data, { columns: cols });
- // let col_data = df.column("C");
- // let rslt_data = [3, 6, 40, 78];
- // assert.deepEqual(col_data.values, rslt_data);
- // });
- // it("Throw Error for wrong column", function () {
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- // let cols = ["A", "B", "C"];
- // let df = new dfd.DataFrame(data, { columns: cols });
- // assert.throws(() => { df.column("D"); }, Error, "ParamError: Column not found!. Column name must be one of A,B,C");
-
- // });
- // });
-
- // describe("Concatenate", function () {
-
- // it("Check the axis 0 concatenation", function () {
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
- // let cols = ["A", "B", "C"]
- // let df = new dfd.DataFrame(data, { columns: cols })
-
- // let data1 = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
- // let cols1 = ["A", "B", "C"]
- // let df1 = new dfd.DataFrame(data1, { columns: cols1 })
-
- // let data2 = [[1, 2, 3, 5], [4, 5, 6, 8], [20, 30, 40, 10]]
- // let cols2 = ["A", "B", "C", "D"]
- // let df2 = new dfd.DataFrame(data2, { columns: cols2 })
-
- // let new_df = DataFrame.concat({ "df_list": [df, df1, df2], "axis": 0 })
-
- // let data_values = [[1, 2, 3, NaN], [4, 5, 6, NaN], [20, 30, 40, NaN], [39, 89, 78, NaN],
- // [1, 2, 3, NaN], [4, 5, 6, NaN], [20, 30, 40, NaN], [39, 89, 78, NaN],
- // [1, 2, 3, 5], [4, 5, 6, 8], [20, 30, 40, 10]]
-
- // assert.deepEqual(new_df.values, data_values);
- // });
-
- // it("Check the axis 1 concatenation", function () {
- // let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
- // let cols = ["A", "B", "C"]
- // let df = new dfd.DataFrame(data, { columns: cols })
-
- // let data1 = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]]
- // let cols1 = ["A", "B", "C"]
- // let df1 = new dfd.DataFrame(data1, { columns: cols1 })
-
- // let data2 = [[1, 2, 3, 5], [4, 5, 6, 8], [20, 30, 40, 10]]
- // let cols2 = ["A", "B", "C", "D"]
- // let df2 = new dfd.DataFrame(data2, { columns: cols2 })
-
- // let new_df = DataFrame.concat({ "df_list": [df, df1, df2], "axis": 1 })
-
- // let data_values = [[1, 2, 3, 1, 2, 3, 1, 2, 3, 5], [4, 5, 6, 4, 5, 6, 4, 5, 6, 8],
- // [20, 30, 40, 20, 30, 40, 20, 30, 40, 10], [39, 89, 78, 39, 89, 78, NaN,
- // NaN, NaN, NaN]]
- // assert.deepEqual(new_df.values, data_values);
- // });
- // });
-
-
- describe("Apply", function () {
-
- it("Apply add operation element wise dataframe on axis 0", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let result = [6, 15, 90, 206];
-
- let apply_rslt = df.apply((x) => {
- return x.reduce((a, b) => a + b, 0);
- }, {
- axis: 0
- });
-
- assert.deepEqual(apply_rslt.values, result);
- });
-
- it("Apply add operation element wise dataframe on axis 0", function () {
- let data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
-
- let result = [64, 126, 127];
-
- let apply_rslt = df.apply((x) => {
- return x.reduce((a, b) => a + b, 0);
- }, {
- axis: 1
- });
-
- assert.deepEqual(apply_rslt.values, result);
- });
- });
-
- describe("dropna", function () {
- it("drop inplace at axis 0", function () {
- let data = [[NaN, 1, 2, 3], [3, 4, NaN, 9], [5, 6, 7, 8]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
-
- let df_val = [[5, 6, 7, 8]];
-
- assert.deepEqual(df.dropna(0).values, df_val);
-
- });
- it("drop inplace at axis 1, inplace false ", function () {
- let data = [[NaN, 1, 2, 3], [3, 4, NaN, 9], [5, 6, 7, 8]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
-
- let df_val = [[1, 3], [4, 9], [6, 8]];
-
- assert.deepEqual(df.dropna(1).values, df_val);
-
- });
- it("drop inplace at axis 1, inplace true ", function () {
- let data = [[NaN, 1, 2, 3], [3, 4, NaN, 9], [5, 6, 7, 8]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
-
- let df_val = [[1, 3], [4, 9], [6, 8]];
- df.dropna(1, { inplace: true });
-
-
- assert.deepEqual(df.values, df_val);
-
- });
- it("drop inplace at axis 0 at inplace true", function () {
- let data = [[NaN, 1, 2, 3], [3, 4, NaN, 9], [5, 6, 7, 8]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
-
- let df_val = [[5, 6, 7, 8]];
-
- df.dropna(0, { inplace: true });
- assert.deepEqual(df.values, df_val);
-
- });
- });
-
- describe("isna", function () {
-
- it("check if each value are nan", function () {
- let data = [[NaN, 1, 2, 3], [3, 4, undefined, 9], [5, 6, 7, 8]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
-
- let df_val = [
- [true, false, false, false],
- [false, false, true, false],
- [false, false, false, false]
- ];
-
- assert.deepEqual(df.isna().values, df_val);
- });
- });
-
- describe("fillna", function () {
-
- it("replace all NaN value", function () {
- let data = [[NaN, 1, 2, 3], [3, 4, NaN, 9], [5, 6, 7, 8]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
-
- let df_val = [[-999, 1, 2, 3], [3, 4, -999, 9], [5, 6, 7, 8]];
- df.fillna(-999, { inplace: true });
- assert.deepEqual(df.values, df_val);
- });
- it("replace all NaN value", function () {
- let data = [[NaN, 1, 2, 3], [3, 4, NaN, 9], [5, 6, 7, 8]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
-
- let df_val = [[-999, 1, 2, 3], [3, 4, -999, 9], [5, 6, 7, 8]];
-
- let df_filled = df.fillna(-999);
- assert.deepEqual(df_filled.values, df_val);
- });
-
- it("Fills only a specified column", function () {
- let data = [[1, 2, 3],
- [4, 5, 6],
- [20, NaN, 40],
- [39, NaN, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let new_vals = [[1, 2, 3], [4, 5, 6], [20, 2, 40], [39, 2, 78]];
- let df_filled = df.fillna([2], { columns: ["B"] });
-
- assert.deepEqual(df_filled.values, new_vals);
- });
- it("Fills column with specified values not in place", function () {
- let data = [[1, 2, 3], [4, 5, 6], [NaN, 20, 40], [NaN, -1, 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let new_vals = [[1, 2, 3], [4, 5, 6], [-2, 20, 40], [-2, -1, 78]];
- let df_filled = df.fillna([-2], { columns: ["A"] });
-
- assert.deepEqual(df_filled.values, new_vals);
- });
-
- it("Fills a list of columns with specified values", function () {
- let data = [[1, undefined, 3], [4, undefined, 6], [NaN, "boy", 40], [NaN, "girl", 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let new_vals = [[1, "girl", 3], [4, "girl", 6], [200, "boy", 40], [200, "girl", 78]];
- let df_filled = df.fillna([200, "girl"], { columns: ["A", "B"] });
- assert.deepEqual(df_filled.values, new_vals);
- });
- it("Fills a list of columns with specified values inplace", function () {
- let data = [[1, undefined, 3], [4, undefined, 6], [NaN, "boy", 40], [NaN, "girl", 78]];
- let cols = ["A", "B", "C"];
- let df = new dfd.DataFrame(data, { columns: cols });
- let new_vals = [[1, "girl", 3], [4, "girl", 6], [200, "boy", 40], [200, "girl", 78]];
- df.fillna([200, "girl"], { columns: ["A", "B"], inplace: true });
- assert.deepEqual(df.values, new_vals);
- });
- });
-
-
- // describe("nanindex", function () {
-
- // it("print out the nanIndex", function () {
- // let data = [[NaN, 1, 2, 3], [3, 4, NaN, 9], [5, 6, 7, 8]];
- // let column = ["A", "B", "C", "D"];
- // let df = new dfd.DataFrame(data, { columns: column });
-
- // let df_val = [0, 1];
- // assert.deepEqual(df.nanIndex(), df_val);
- // });
- // });
-
- describe("select_dtypes", function () {
-
- it("Returns float columns in a DataFrame", function () {
- let data = [[30, 1, 2, "boy"], [3.2, 4, 30, "girl"], [5.09, 6, 7, "cat"]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
- let df_sub = df.select_dtypes(['float32']);
- assert.deepEqual(df_sub["A"].values, [30, 3.2, 5.09]);
- });
-
- it("Returns int columns in a DataFrame", function () {
- let data = [[30, 1, 2, "boy"],
- [3.2, 4, 30, "girl"],
- [5.09, 6, 7, "cat"]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
- let df_sub = df.select_dtypes(['int32']);
- assert.deepEqual(df_sub.values, [[1, 2], [4, 30], [6, 7]]);
- });
-
- it("Returns string columns in a DataFrame", function () {
- let data = [[30, 1, 2, "boy"],
- [3.2, 4, 30, "girl"],
- [5.09, 6, 7, "cat"]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
- let df_sub = df.select_dtypes(['string']);
- assert.deepEqual(df_sub["D"].values, ["boy", "girl", "cat"]);
- });
-
- it("Returns string and float columns in a DataFrame", function () {
- let data = [[30, 1, 2, "boy"],
- [3.2, 4, 30, "girl"],
- [5.09, 6, 7, "cat"]];
- let expected = [[30, "boy"],
- [3.2, "girl"],
- [5.09, "cat"]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
- let df_sub = df.select_dtypes(['string', 'float32']);
- assert.deepEqual(df_sub.values, expected);
- });
-
- it("Returns int and float columns in a DataFrame", function () {
- let data = [[30, 1, 2, "boy"],
- [3.2, 4, 30, "girl"],
- [5.09, 6, 7, "cat"]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
- let df_sub = df.select_dtypes(['int32', 'float32']);
- assert.deepEqual(df_sub.values, [[30, 1, 2], [3.2, 4, 30], [5.09, 6, 7]]);
- });
- });
-
- describe("cum_ops", function () {
-
- it("check cumsum data", function () {
- let data = [[2, 1, 2, 3], [3, 4, 11, 9], [5, 6, 7, 8]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
- let rslt = [[2, 1, 2, 3], [5, 5, 13, 12], [10, 11, 20, 20]];
-
- assert.deepEqual(df.cumsum({ axis: 1 }).values, rslt);
- });
- it("check cumsum data along axis 1", function () {
- let data = [[2, 1, 2, 3], [3, 4, 11, 9], [5, 6, 7, 8]];
- let column = ["A", "B", "C", "D"];
- let df = new dfd.DataFrame(data, { columns: column });
- let rslt = [[2, 3, 5, 8], [3, 7, 18, 27], [5, 11, 18, 26]];
-
- assert.deepEqual(df.cumsum({ axis: 0 }).values, rslt);
- });
- });
-
- describe("lt", function () {
- it("Returns Less than of DataFrame and other DataFrame (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 20, 10]];
- let data2 = [[100, 450, 590, 5], [25, 2, 0, 10]];
-
- let df = new dfd.DataFrame(data1);
- let df2 = new dfd.DataFrame(data2);
- let expected = [[true, true, true, false],
- [false, false, false, false]];
- assert.deepEqual(df.lt(df2).values, expected);
- });
-
- it("Return Less than of series scalar (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 20, 10]];
- let sf = new dfd.DataFrame(data1);
- let expected = [[true, false, false, true],
- [true, true, true, true]];
- assert.deepEqual(sf.lt(30).values, expected);
- });
- it("Return Less than of series and DataFrame scalar along axis 1 (column)", function () {
- let data1 = [[10, 45, 56, 10],
- [23, 20, 10, 10]];
- let sf = new dfd.Series([10, 23, 56, 100]);
- let df = new dfd.DataFrame(data1);
- let expected = [[false, false, false, true], [false, true, true, true]];
- assert.deepEqual(df.lt(sf, { axis: 1 }).values, expected);
- });
-
- it("Return Less than of Array and DataFrame scalar along axis 1 (column)", function () {
- let data1 = [[10, 45, 56, 10], [23, 20, 10, 10]];
- let sf = [10, 23, 56, 100];
- let df = new dfd.DataFrame(data1);
- let expected = [[false, false, false, true], [false, true, true, true]];
- assert.deepEqual(df.lt(sf, { axis: 1 }).values, expected);
- });
-
- });
-
- describe("gt", function () {
- it("Return Greater than of series and other series (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 20, 10]];
- let data2 = [[100, 450, 590, 5], [25, 2, 0, 10]];
-
- let df = new dfd.DataFrame(data1);
- let df2 = new dfd.DataFrame(data2);
- let expected = [[false, false, false, true], [false, true, true, false]];
- assert.deepEqual(df.gt(df2).values, expected);
- });
-
- it("Return Greater than of series scalar (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 20, 10]];
- let sf = new dfd.DataFrame(data1);
- let expected = [[false, true, true, false], [false, false, false, false]];
- assert.deepEqual(sf.gt(30).values, expected);
- });
-
- it("Return Less than of Array and DataFrame scalar along axis 1 (column)", function () {
- let data1 = [[10, 45, 56, 10], [23, 20, 10, 10]];
- let sf = [10, 23, 56, 100];
- let df = new dfd.DataFrame(data1);
- let expected = [[false, true, false, false], [true, false, false, false]];
- assert.deepEqual(df.gt(sf, { axis: 1 }).values, expected);
- });
-
- });
-
- describe("le", function () {
- it("Return Less than or Equal to of series and other series (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 20, 10]];
- let data2 = [[100, 450, 590, 5], [25, 2, 0, 10]];
-
- let df = new dfd.DataFrame(data1);
- let df2 = new dfd.DataFrame(data2);
- let expected = [[true, true, true, false], [true, false, false, true]];
- assert.deepEqual(df.le(df2).values, expected);
- });
-
- it("Return Less than or Equal to of series scalar (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 30, 10]];
- let sf = new dfd.DataFrame(data1);
- let expected = [[true, false, false, true], [true, true, true, true]];
- assert.deepEqual(sf.le(30).values, expected);
- });
-
- });
-
- describe("ge", function () {
- it("Return Greater than or Equal to of series and other series (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 20, 10]];
- let data2 = [[100, 450, 590, 5], [25, 2, 0, 10]];
-
- let df = new dfd.DataFrame(data1);
- let df2 = new dfd.DataFrame(data2);
- let expected = [[false, false, false, true], [true, true, true, true]];
- assert.deepEqual(df.ge(df2).values, expected);
- });
-
- it("Return Greater than or Equal to of series scalar (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 30, 10]];
- let sf = new dfd.DataFrame(data1);
- let expected = [[false, true, true, false], [false, false, true, false]];
- assert.deepEqual(sf.ge(30).values, expected);
- });
-
- });
-
- describe("ne", function () {
- it("Return Not Equal to of series and other series (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 20, 10]];
- let data2 = [[100, 450, 590, 5], [25, 2, 0, 10]];
-
- let df = new dfd.DataFrame(data1);
- let df2 = new dfd.DataFrame(data2);
- let expected = [[true, true, true, true], [false, true, true, false]];
- assert.deepEqual(df.ne(df2).values, expected);
- });
-
- it("Return Not Equal to of series scalar (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 30, 10]];
- let sf = new dfd.DataFrame(data1);
- let expected = [[true, true, true, true], [true, true, false, true]];
- assert.deepEqual(sf.ne(30).values, expected);
- });
-
- });
-
- describe("eq", function () {
- it("Return Equal to of DataFrame and other DataFrame (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 20, 10]];
- let data2 = [[100, 450, 590, 5], [25, 2, 0, 10]];
-
- let df = new dfd.DataFrame(data1);
- let df2 = new dfd.DataFrame(data2);
- let expected = [[false, false, false, false], [true, false, false, true]];
- assert.deepEqual(df.eq(df2).values, expected);
- });
-
- it("Return Equal to of DataFrame with scalar (element-wise)", function () {
- let data1 = [[10, 45, 56, 10], [25, 23, 30, 10]];
- let sf = new dfd.DataFrame(data1);
- let expected = [[false, false, false, false], [false, false, true, false]];
- assert.deepEqual(sf.eq(30).values, expected);
- });
- it("Return Equal to of series and DataFrame scalar along axis 1 (column)", function () {
- let data1 = { "Col1": [10, 45, 56, 10], "Col2": [23, 20, 10, 10] };
- let sf = new dfd.Series([10, 23]);
- let df = new dfd.DataFrame(data1);
- let expected = [[true, true], [false, false], [false, false], [true, false]];
- assert.deepEqual(df.eq(sf, { axis: 1 }).values, expected);
- });
-
- });
-
- describe("replace", function () {
- it("Replace values given in replace param", function () {
- let data1 = [[10, 45, 56, 25], [23, 20, 10, 24]];
- let sf = new dfd.DataFrame(data1);
- let expected = [[-999, 45, 56, 25], [23, 20, -999, 24]];
- let df_rep = sf.replace(10, -999);
- assert.deepEqual(df_rep.values, expected);
- });
-
- it("Replace values given in replace param with value (String type)", function () {
- let data1 = [["A", "A", "A", "B"], ["B", "C", "C", "D"]];
- let df = new dfd.DataFrame(data1);
- let expected = [["boy", "boy", "boy", "B"], ["B", "C", "C", "D"]];
- let df_rep = df.replace("A", "boy");
- assert.deepEqual(df_rep.values, expected);
- });
-
- it("Replace values in specified two column(s)", function () {
- let data1 = [["A", "A", 1, "girl"],
- ["B", "A", 2, "woman"],
- ["A", "B", 3, "man"]];
- let df = new dfd.DataFrame(data1, { columns: ["col1", "col2", "col3", "col4"] });
- let expected = [["boy", "boy", 1, "girl"],
- ["B", "boy", 2, "woman"],
- ["boy", "B", 3, "man"]];
- let df_rep = df.replace("A", "boy", { columns: ["col1", "col2"] });
- assert.deepEqual(df_rep.values, expected);
- });
-
- it("Replace values in specified single column(s)", function () {
- let data1 = [[2, "A", 1, "girl"],
- [3, "A", 2, "woman"],
- [4, "B", 3, "man"]];
- let df = new dfd.DataFrame(data1, { columns: ["col1", "col2", "col3", "col4"] });
- let expected = [[2, "A", 1, "girl"],
- [10, "A", 2, "woman"],
- [4, "B", 3, "man"]];
- let df_rep = df.replace(3, 10, { columns: ["col1"] });
- assert.deepEqual(df_rep.values, expected);
- });
-
- });
-
-
- describe("sum", function () {
- it("Sum values of a DataFrame by Default axis column (axis=1)", function () {
- let data1 = [[30, 40, 3.1],
- [5, 5, 5.1],
- [5, 5, 3.2]];
- let sf = new dfd.DataFrame(data1);
- let res = [40, 50, 11.399999999999999];
- assert.deepEqual(sf.sum().values, res);
- });
- it("Sum values of a DataFrame along row axis (axis=0)", function () {
- let data1 = [[30, 40, 3.1],
- [5, 5, 5.1],
- [5, 5, 3.2]];
- let df = new dfd.DataFrame(data1);
- let res = [73.1, 15.1, 13.2];
- assert.deepEqual(df.sum({ axis: 0 }).values, res);
- });
- it("Sum values of a mixed DataFrame along row axis (axis=0)", function () {
- let data1 = [[30, 40, 3.1, true],
- [5, 5, 5.1, true],
- [5, 5, 3.2, true]];
- let df = new dfd.DataFrame(data1);
- let res = [74.1, 16.1, 14.2];
- assert.deepEqual(df.sum({ axis: 0 }).values, res);
- });
- it("Sum values of a boolean DataFrame along row axis (axis=0)", function () {
- let data1 = [[true, true, false, true],
- [false, false, false, false],
- [false, true, true, false]];
- let df = new dfd.DataFrame(data1);
- let res = [3, 0, 2];
- assert.deepEqual(df.sum({ axis: 0 }).values, res);
- });
- it("Sum values of a boolean DataFrame along default column axis (axis=1)", function () {
- let data1 = [[true, true, false, true],
- [false, false, false, false],
- [false, true, true, false]];
- let df = new dfd.DataFrame(data1);
- let res = [1, 2, 1, 1];
- assert.deepEqual(df.sum().values, res);
- });
- it("Sum values of a df with missing values", function () {
- let data1 = [[11, 20, 3], [null, 15, 6], [2, 30, 40], [2, 89, 78]];
- let df = new dfd.DataFrame(data1);
- let res = [15, 154, 127];
- assert.deepEqual(df.sum().values, res);
- });
-
- });
-
- describe("abs", function () {
- it("Returns the absolute values in DataFrame of ints", function () {
- let data1 = [[-10, 45, 56, 10], [-25, 23, 20, -10]];
- let df = new dfd.DataFrame(data1);
- let expected = [[10, 45, 56, 10], [25, 23, 20, 10]];
- assert.deepEqual(df.abs().values, expected);
- });
-
- it("Returns the absolute values in mixed DataFrame", function () {
- let data1 = [[-10, -45.1, 56, 10], [-25, -23.2, 20, -10]];
- let df = new dfd.DataFrame(data1);
- let expected = [[10, 45.1, 56, 10], [25, 23.2, 20, 10]];
- assert.deepEqual(df.abs().values, expected);
- });
- });
-
- describe("T", function () {
- it("Returns the Tranpose of a DataFrame", function () {
- let data1 = [[10, 45, 56, 10],
- [25, 23, 20, 10]];
-
- let cols = ["a", "b", "c", "d"];
- let df = new dfd.DataFrame(data1, { columns: cols });
- let df_trans = df.T;
- let expected_vals = [[10, 25], [45, 23], [56, 20], [10, 10]];
- let expected_index = cols;
- let expected_col_names = ['0', '1'];
- assert.deepEqual(df_trans.index, expected_index);
- assert.deepEqual(df_trans.values, expected_vals);
- assert.deepEqual(df_trans.columns, expected_col_names);
-
- });
-
- });
-
-
- describe("astype", function () {
- it("set type of float column to int", function () {
- let data = {
- "A": [-20.1, 30, 47.3, -20],
- "B": [34, -4, 5, 6],
- "C": [20.1, -20.23, 30.3, 40.11],
- "D": ["a", "b", "c", "c"]
- };
- let ndframe = new dfd.DataFrame(data);
- let df = ndframe.astype({ column: "A", dtype: "int32" });
-
- assert.deepEqual(df.dtypes, ['int32', 'int32', 'float32', 'string']);
- assert.deepEqual(df['A'].values, [-20, 30, 47, -20]);
-
- });
- it("set type of int column to float", function () {
- let data = {
- "A": [-20.1, 30, 47.3, -20],
- "B": [34, -4, 5, 6],
- "C": [20.1, -20.23, 30.3, 40.11],
- "D": ["a", "b", "c", "c"]
- };
- let ndframe = new dfd.DataFrame(data);
- let df = ndframe.astype({ column: "B", dtype: "float32" });
-
- assert.deepEqual(df.dtypes, ['float32', 'float32', 'float32', 'string']);
- assert.deepEqual(df['B'].values, [34, -4, 5, 6]);
-
- });
- it("set type of string column to int", function () {
- let data = {
- "A": [-20.1, 30, 47.3, -20],
- "B": [34, -4, 5, 6],
- "C": [20.1, -20.23, 30.3, 40.11],
- "D": ["20.1", "21", "23.4", "50.78"]
- };
- let ndframe = new dfd.DataFrame(data);
- let df = ndframe.astype({ column: "D", dtype: "int32" });
-
- assert.deepEqual(df.dtypes, ['float32', 'int32', 'float32', 'int32']);
- assert.deepEqual(df['D'].values, [20, 21, 23, 50]);
-
- });
- it("set type of string column to float", function () {
- let data = {
- "A": [-20.1, 30, 47.3, -20],
- "B": [34, -4, 5, 6],
- "C": [20.1, -20.23, 30.3, 40.11],
- "D": ["20.1", "21", "23.4", "50.78"]
- };
- let ndframe = new dfd.DataFrame(data);
- let df = ndframe.astype({ column: "D", dtype: "float32" });
-
- assert.deepEqual(df.dtypes, ['float32', 'int32', 'float32', 'float32']);
- assert.deepEqual(df['D'].values, [20.1, 21, 23.4, 50.78]);
-
- });
- });
-
-
- describe("nunique", function () {
- it("Returns the number of unique elements along axis 1", function () {
- let data = {
- "A": [-20, 30, 47.3, -20],
- "B": [34, -4, 5, 6],
- "C": [20, 20, 30, 30],
- "D": ["a", "b", "c", "c"]
- };
-
- let ndframe = new dfd.DataFrame(data);
- let df = ndframe.nunique(1);
- let res = [3, 4, 2, 3];
- assert.deepEqual(df.values, res);
-
- });
- it("Returns the number of unique elements along axis 0", function () {
- let data = {
- "A": [20, 30, 47.3, 30],
- "B": [34, -4, 5, 30],
- "C": [20, 20, 30, 30],
- "D": ["a", "b", "c", "c"]
- };
-
- let ndframe = new dfd.DataFrame(data);
- let df = ndframe.nunique(0);
- let res = [3, 4, 4, 2];
- assert.deepEqual(df.values, res);
-
- });
-
- });
-
- describe("rename", function () {
- it("Rename columns along axis 1", function () {
- let data = {
- "A": [-20, 30, 47.3, -20],
- "B": [34, -4, 5, 6],
- "C": [20, 20, 30, 30],
- "D": ["a", "b", "c", "c"]
- };
-
- let ndframe = new dfd.DataFrame(data);
- let df = ndframe.rename({ mapper: { "A": "a1", "B": "b1" } });
- let res = ["a1", "b1", "C", "D"];
- assert.deepEqual(df.columns, res);
-
- });
-
- it("Rename columns along axis 1 inplace", function () {
- let data = {
- "A": [-20, 30, 47.3, -20],
- "B": [34, -4, 5, 6],
- "C": [20, 20, 30, 30],
- "D": ["a", "b", "c", "c"]
- };
-
- let df = new dfd.DataFrame(data);
- df.rename({ mapper: { "A": "a1", "B": "b1" }, inplace: true });
- let res = ["a1", "b1", "C", "D"];
- assert.deepEqual(df.columns, res);
-
- });
- it("Rename string index along axis 0", function () {
- let data = {
- "A": [-20, 30, 47.3, -20],
- "B": [34, -4, 5, 6],
- "C": [20, 20, 30, 30],
- "D": ["a", "b", "c", "c"]
- };
-
- let ndframe = new dfd.DataFrame(data, { index: ["a", "b", "c", "d"] });
- let df = ndframe.rename({ mapper: { "a": 0, "b": 1 }, axis: 0 });
- let res = [0, 1, "c", "d"];
- assert.deepEqual(df.index, res);
-
- });
- it("Rename string index along axis 0 inplace", function () {
- let data = {
- "A": [-20, 30, 47.3, -20],
- "B": [34, -4, 5, 6],
- "C": [20, 20, 30, 30],
- "D": ["a", "b", "c", "c"]
- };
-
- let df = new dfd.DataFrame(data, { index: ["a", "b", "c", "d"] });
- df.rename({ mapper: { "a": 0, "b": 1 }, axis: 0, inplace: true });
- let res = [0, 1, "c", "d"];
- assert.deepEqual(df.index, res);
-
- });
-
- it("Get new column via subseting works after rename (inplace)", function () {
- let data = {
- "A": [-20, 30, 47.3],
- "B": [34, -4, 5],
- "C": [20, 2, 30]
- };
- let df = new dfd.DataFrame(data);
- df.rename({ mapper: { "A": "new_name" }, inplace: true });
- assert.deepEqual(df["new_name"].values, data["A"]);
- });
-
- it("Get new column via subseting works after rename (not-inplace)", function () {
- let data = {
- "A": [-20, 30, 47.3],
- "B": [34, -4, 5],
- "C": [20, 2, 30]
- };
- let df = new dfd.DataFrame(data);
- let new_df = df.rename({ mapper: { "A": "new_name" } });
- assert.deepEqual(new_df["new_name"].values, data["A"]);
- });
- });
-
- describe("sort_index", function () {
-
- it("sort index in ascending order", function () {
- let data = [[0, 2, 4, "b"],
- [360, 180, 360, "a"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"], index: ["b", "a", "c"] });
- let df2 = df.sort_index();
- let rslt = [[360, 180, 360, 'a'], [0, 2, 4, 'b'], [2, 4, 6, 'c']];
-
- assert.deepEqual(df2.values, rslt);
- });
- it("sort index in descending order", function () {
- let data = [[0, 2, 4, "b"],
- [360, 180, 360, "a"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"], index: ["b", "a", "c"] });
- let df2 = df.sort_index({ ascending: false });
- let rslt = [[2, 4, 6, 'c'], [0, 2, 4, 'b'], [360, 180, 360, 'a']];
-
- assert.deepEqual(df2.values, rslt);
- });
- it("sort index in descending order with inplace set to true", function () {
- let data = [[0, 2, 4, "b"],
- [360, 180, 360, "a"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"], index: ["b", "a", "c"] });
- df.sort_index({ ascending: false, inplace: true });
- let rslt = [[2, 4, 6, 'c'], [0, 2, 4, 'b'], [360, 180, 360, 'a']];
- assert.deepEqual(df.values, rslt);
- });
- it("sort index in descending order and retains index", function () {
- let data = [[0, 2, 4, "b"],
- [360, 180, 360, "a"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"], index: ["b", "a", "c"] });
- let df2 = df.sort_index({ ascending: false });
- let rslt = ["c", "b", "a"];
-
- assert.deepEqual(df2.index, rslt);
- });
- });
-
- describe("append", function () {
-
- it("Add a new single row (array) to the end of a DataFrame", function () {
- let data = [[0, 2, 4, "b"],
- [360, 180, 360, "a"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data);
- let expected_val = [[0, 2, 4, "b"],
- [360, 180, 360, "a"],
- [2, 4, 6, "c"],
- [20, 40, 60, "d"]];
-
- let rslt_df = df.append([[20, 40, 60, "d"]], [3]);
- assert.deepEqual(rslt_df.values, expected_val);
-
- });
-
- it("Add a new single row (object) to the end of a DataFrame", function () {
- let data = [[0, 2, 4, "b"],
- [360, 180, 360, "a"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] });
- let df2 = new dfd.DataFrame([[20, 40, 60, "d"]], { "columns": ["col1", "col2", "col3", "col4"] });
-
- let expected_val = [[0, 2, 4, "b"],
- [360, 180, 360, "a"],
- [2, 4, 6, "c"],
- [20, 40, 60, "d"]];
-
- let rslt_df = df.append(df2, [3]);
- assert.deepEqual(rslt_df.values, expected_val);
-
- });
- it("Confirm index Change after append", function () {
- let data = [[0, 2, 4, "b"],
- [360, 180, 360, "a"],
- [2, 4, 6, "c"]];
-
- let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] });
- let df2 = new dfd.DataFrame([[20, 40, 60, "d"]], { "columns": ["col1", "col2", "col3", "col4"] });
-
- let rslt_df = df.append(df2, [3]);
- assert.deepEqual(rslt_df.index, [0, 1, 2, 3]);
-
- });
- });
-
- describe("Str", function () {
- it("Str (startsWith) works for columns selected from a DF", function () {
- let data = {
- "Name": ["Apples", "Bake", "Application", undefined],
- "Count": [2, 5, 4, 10],
- "Price": [200, 300, 40, 250]
- };
-
- let df = new dfd.DataFrame(data);
- let name_sf = df['Name'];
- assert.deepEqual(name_sf.str.startsWith("App").values, [true, false, true, NaN]);
- });
- it("Str (toLowerCase) works for columns selected from a DF", function () {
- let data = {
- "Name": ["Apples", "Bake", "Application", undefined],
- "Count": [2, 5, 4, 10],
- "Price": [200, 300, 40, 250]
- };
-
- let df = new dfd.DataFrame(data);
- let name_sf = df['Name'];
- assert.deepEqual(name_sf.str.toLowerCase().values, ["apples", "bake", "application", NaN]);
- });
- });
-
-});
diff --git a/danfojs-browser/tests/core/generic.js b/danfojs-browser/tests/core/generic.js
deleted file mode 100644
index 0312b723..00000000
--- a/danfojs-browser/tests/core/generic.js
+++ /dev/null
@@ -1,259 +0,0 @@
-/* eslint-disable no-undef */
-const tf = require('@tensorflow/tfjs');
-
-
-describe("Generic (NDFrame)", function () {
- describe("NDframe Created from Array", function () {
- it("prints the shape of a 1D array", function () {
- let data = [ 1, 2, 3, "Boy", "Girl" ];
- let ndframe = new dfd.NDframe({ data, isSeries:true });
- assert.deepEqual(ndframe.shape, [ 5, 1 ]);
- });
- it("prints the default assigned column name in a series", function () {
- let data = [ "Boy", 20, 25 ];
- let ndframe = new dfd.NDframe({ data, isSeries:true });
- assert.deepEqual(ndframe.columns, [ "0" ]);
- });
- it("prints the assigned column name in a series", function () {
- let data = [ "Boy", 20, 25 ];
- let options = { "columns": 'Records', isSeries : true };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.columns, "Records");
- });
- it("prints the shape of a 2D array", function () {
- let data = [ [ "Boy", 20 ], [ "Girl", 25 ] ];
- let ndframe = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(ndframe.shape, [ 2, 2 ]);
- });
- it("prints the default assigned column names in 2D frame", function () {
- let data = [ [ "Boy", 20 ], [ "Girl", 25 ] ];
- let ndframe = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(ndframe.columns, [ "0", "1" ]);
- });
- it("prints the assigned column names", function () {
- let data = [ [ "Boy", 20 ], [ "Girl", 25 ] ];
- let options = { "columns": [ "Gender", "Age" ], isSeries : false };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.columns, [ "Gender", "Age" ]);
- });
- it("prints the size of a frame", function () {
- let data = [ [ "Boy", 20, 1 ], [ "Girl", 25, 3 ] ];
- let options = { "columns": [ "Gender", "Age", "count" ], isSeries : false };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.size, 6);
- });
- it("prints the dimension of a frame", function () {
- let data = [ [ "Boy", 20, 1 ], [ "Girl", 25, 3 ] ];
- let options = { "columns": [ "Gender", "Age", "count" ], isSeries:false };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.ndim, 2);
- });
- it("prints the values of a frame", function () {
- let data = [ [ 21, 20, 1 ], [ 20, 25, 3 ] ];
- let ndframe = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(ndframe.values, data);
- });
- it("prints the values of a frame", function () {
- let data = [ [ 21, 20, 1 ], [ 20, 25, 3 ] ];
- let ndframe = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(ndframe.values, data);
- });
- });
-
- describe("NDframe Created from JavaScript Object", function () {
-
- it("prints the shape of a 2D frame created from an Object", function () {
- let data = [ { alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 } ];
- let ndframe = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(ndframe.shape, [ 3, 2 ]);
- });
- it("prints the column names of frame created from an Object", function () {
- let data = [ { alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 } ];
- let ndframe = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(ndframe.columns, [ "alpha", "count" ]);
- });
- it("prints the shape of a 1D frame created from an Object", function () {
- let data = [ { alpha: "A", count: 1 } ];
- let ndframe = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(ndframe.shape, [ 1, 2 ]);
- });
-
- it("prints the size of a frame created from an Object", function () {
- let data = [ { alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 } ];
- let options = { isSeries : false };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.size, 6);
- });
- it("prints the dimension of a frame created from an Object", function () {
- let data = [ { alpha: "A", count: 1 }, { alpha: "B", count: 2 } ];
- let options = { isSeries : false };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.ndim, 2);
- });
- it("prints the values of a frame created from an Object", function () {
- let data = [ { alpha: "A", count: 1 }, { alpha: "B", count: 2 } ];
- let ndframe = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(ndframe.values, [ [ "A", 1 ], [ "B", 2 ] ]);
- });
- it("prints the values of a frame created from an Object with null values", function () {
- let data = [ { alpha: "A", count: null }, { alpha: null, count: 2 } ];
- let ndframe = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(ndframe.values, [ [ "A", null ], [ null, 2 ] ]);
- });
- });
-
- describe("NDframe Created from a Tensor", function () {
-
- it("prints the shape of a 2D frame created from a 2D tensor", function () {
- let data = tf.tensor([ 1, 2, 3, 4 ]);
- let ndframe = new dfd.NDframe({ data, isSeries:true });
- assert.deepEqual(ndframe.ndim, 1);
- assert.deepEqual(ndframe.values, [ 1, 2, 3, 4 ]);
-
- });
- it("prints the shape of a 2D frame created from a 1D tensor", function () {
- let data = tf.tensor([ [ 2, 3, 4 ], [ 4, 5, 6 ] ]);
- let ndframe = new dfd.NDframe({ data, columns: [ "alpha", "count", "sum" ], isSeries : false });
- assert.deepEqual(ndframe.columns, [ "alpha", "count", "sum" ]);
- });
-
- });
-
- describe("index", function () {
- it("Returns the index of an NDframe", function () {
- let data = [ { alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 } ];
- let df = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(df.index, [ 0, 1, 2 ]);
- });
- it("Returns the index of an NDframe created from an Array", function () {
- let data = [ [ 12, 2, 20 ], [ 90, 5, 23 ], [ 45, 56, 70 ], [ 9, 10, 19 ] ];
- let df = new dfd.NDframe({ data, isSeries:false });
- assert.deepEqual(df.index, [ 0, 1, 2, 3 ]);
- });
- });
-
- describe("NDframe Created from JavaScript Object of Arrays", function () {
- it("retrieves the row data created from OA ", function () {
- let data = { alpha: [ "A", "B" ], count: [ 1, 2 ] };
- let ndframe = new dfd.NDframe({ data, isSeries:false });
- // assert.deepEqual(ndframe.shape, [4, 2])
- assert.deepEqual(ndframe.values, [ [ "A", 1 ], [ "B", 2 ] ]);
- });
- });
-
- describe("$setIndex", function () {
- it("sets the index of an NDframe", function () {
- let data = [ { alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 } ];
- let df = new dfd.NDframe({ data, isSeries:false });
- df.$setIndex([ "A", "B", "C" ]);
- assert.deepEqual(df.index, [ "A", "B", "C" ]);
- });
- it("Returns the index of an NDframe created from an Array", function () {
- let data = [ [ 12, 2, 20 ], [ 90, 5, 23 ], [ 45, 56, 70 ], [ 9, 10, 19 ] ];
- let df = new dfd.NDframe({ data, isSeries:false });
- df.$setIndex([ 10, 20, 30, 40 ]);
- assert.deepEqual(df.index, [ 10, 20, 30, 40 ]);
- });
- });
-
-
- describe("dtype", function () {
- it("Returns int dtype set during creation of 1DFrame (Series) from an Object", function () {
- let data = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
- let options = { dtypes: [ 'int32' ], isSeries : true };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.dtypes, [ 'int32' ]);
- });
-
- it("Returns string dtype set during creation of 1DFrame (Series) from an Array", function () {
- let data = [ "Alice", "Yemi", "Rising", "Mark" ];
- let options = { dtypes: [ 'string' ], isSeries : true };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.dtypes, [ "string" ]);
- });
-
- it("Returns string dtype automatically inferred from 1DFrame (Series)", function () {
- let data = [ "Alice", "Yemi", "Rising", "Mark" ];
- let options = { columns: 'Names', isSeries : true };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.dtypes, [ "string" ]);
- });
-
- it("Returns int dtype automatically inferred from 1DFrame (Series)", function () {
- let data = [ 20, 30, 20, 20 ];
- let options = { columns: 'Size', isSeries : true };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.dtypes, [ "int32" ]);
- });
- it("Returns float dtype automatically inferred from 1DFrame (Series)", function () {
- let data = [ 20.1, 30.4, 20.2, 4.23, 20.1 ];
- let options = { columns: 'Size', isSeries : true };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.dtypes, [ "float32" ]);
- });
-
-
- it("Returns dtype set during creation of 2DFrame from an Object", function () {
- let data = [ { alpha: "A", count: 1 }, { alpha: "B", count: 2 } ];
- let options = { dtypes: [ 'string', 'int32' ], isSeries : false };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.dtypes, [ 'string', 'int32' ]);
- });
- it("Returns dtype set during creation of 2DFrame from an Array", function () {
- let data = [ [ "Alice", 2, 3.0 ], [ "Boy", 5, 6.1 ], [ "Girl", 30, 40 ], [ 39, 89, 78.2 ] ];
- let cols = [ "Name", "Count", "Score" ];
- let options = { columns: cols, dtypes: [ 'string', 'int32', 'float32' ], isSeries : false };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.dtypes, [ "string", "int32", "float32" ]);
- });
-
- it("Returns dtype automatically inferred from 2DFrame", function () {
- let data = [ [ "Alice", 2, 3.1 ], [ "Boy", 5, 6.1 ], [ "Girl", 30, 40.2 ], [ 39, 89, 78.2 ] ];
- let cols = [ "Name", "Count", "Score" ];
- let options = { columns: cols, isSeries : false };
- let ndframe = new dfd.NDframe({ data, ...options });
- assert.deepEqual(ndframe.dtypes, [ "string", "int32", "float32" ]);
- });
-
- });
-
-
- // describe("to_csv", async function () {
- // it("Converts DataFrame to csv format and return string", async function () {
- // let data = [ { alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 } ];
- // let df = new dfd.NDframe({ data, isSeries:true });
- // let result = `alpha,count\nA,1\nB,2\nC,3\n`;
- // df.to_csv().then((csv) => {
- // assert.deepEqual(csv, result);
- // });
- // });
- // it("Converts DataFrame of Series to csv format and return string when path is not specified", async function () {
- // let data = [ [ 12, 2, 20 ], [ 90, 5, 23 ], [ 45, 56, 70 ], [ 9, 10, 19 ] ];
- // let df = new dfd.NDframe(data, { columns: [ "A", "B", "C" ] });
- // let result = `A,B,C\n12,2,20\n90,5,23\n45,56,70\n9,10,19\n`;
- // assert.deepEqual(await df.to_csv(), result);
- // });
- // });
-
- // describe("to_json", async function () {
- // it("Converts DataFrame to json format and return string", async function () {
- // let data = [ { alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 } ];
- // let result = JSON.stringify([ { alpha: "A", count: 1 }, { alpha: "B", count: 2 }, { alpha: "C", count: 3 } ]);
-
- // let df = new dfd.NDframe({ data, isSeries:true });
- // df.to_json().then((json) => {
- // assert.deepEqual(json, result);
- // });
- // });
- // it("Converts DataFrame to json format", async function () {
- // let data = [ [ 12, 2, 20 ], [ 90, 5, 23 ], [ 45, 56, 70 ] ];
- // let df = new dfd.NDframe(data, { columns: [ "A", "B", "C" ] });
- // let result = JSON.stringify([ { A: 12, B: 2, C: 20 }, { A: 90, B: 5, C: 23 }, { A: 45, B: 56, C: 70 } ]);
- // df.to_json().then((json) => {
- // assert.deepEqual(json, result);
- // });
- // });
- // });
-
-
-});
diff --git a/danfojs-browser/tests/core/get_dummies.js b/danfojs-browser/tests/core/get_dummies.js
deleted file mode 100644
index 3acfea24..00000000
--- a/danfojs-browser/tests/core/get_dummies.js
+++ /dev/null
@@ -1,175 +0,0 @@
-/* eslint-disable no-undef */
-
-describe("DummyEncoder", function () {
- it("get_dummies works on Series", function () {
-
- const data = ["dog", "male", "female", "male", "female", "male", "dog"];
- const series = new dfd.Series(data);
- const df = dfd.get_dummies(series, { prefix: "test", prefixSeparator: "/" });
-
- const dfValues = [
- [1, 0, 0],
- [0, 1, 0],
- [0, 0, 1],
- [0, 1, 0],
- [0, 0, 1],
- [0, 1, 0],
- [1, 0, 0]
- ];
- const dfColumns = ['test/dog', 'test/male', 'test/female'];
- assert.deepEqual(df.values, dfValues);
- assert.deepEqual(df.columns, dfColumns);
- });
- it("get_dummies works on Series with default prefix and prefixSeperator", function () {
-
- const data = ["dog", "male", "female", "male", "female", "male", "dog"];
- const series = new dfd.Series(data);
- const df = dfd.get_dummies(series);
-
- const dfValues = [
- [1, 0, 0],
- [0, 1, 0],
- [0, 0, 1],
- [0, 1, 0],
- [0, 0, 1],
- [0, 1, 0],
- [1, 0, 0]
- ];
- const dfColumns = ['0_dog', '1_male', '2_female'];
- assert.deepEqual(df.values, dfValues);
- assert.deepEqual(df.columns, dfColumns);
- });
-
- it("get_dummies works on DataFrame", function () {
-
- const data = [[1, "dog", 1.0, "fat"], [3, "fog", 2.0, "good"], [4, "gof", 3.0, "best"]];
- const columns = ["A", "B", "C", "d"];
- const df = new dfd.DataFrame(data, { columns: columns });
-
- const df1 = dfd.get_dummies(df, { prefixSeparator: ["_", "#"], columns: ["A", "d"], prefix: "test" });
- const expectedColumns = ['B', 'C', 'test_1', 'test_3', 'test_4', 'test#fat', 'test#good', 'test#best'];
- const expected = [['dog', 1.0, 1, 0, 0, 1, 0, 0],
- ['fog', 2.0, 0, 1, 0, 0, 1, 0],
- ['gof', 3.0, 0, 0, 1, 0, 0, 1]];
- assert.deepEqual(df1.values, expected);
- assert.deepEqual(df1.columns, expectedColumns);
-
- });
- it("Throw error if the prefix specified is not equal to the column specified", function () {
-
- const data = [[1, "dog", 1.0, "fat"], [3, "fog", 2.0, "good"], [4, "gof", 3.0, "best"]];
- const columns = ["A", "B", "C", "d"];
- const df = new dfd.DataFrame(data, { columns: columns });
-
- assert.throws(function () { dfd.get_dummies(df, { prefix: ["fg"], prefixSeparator: "_", columns: ["A", "d"] }); }, Error,
- `ParamError: prefix and data array must be of the same length. If you need to use the same prefix, then pass a string param instead. e.g {prefix: "fg"}`);
-
- });
- it("replace column sepecified with prefix", function () {
-
- const data = [[1, "dog", 1.0, "fat"], [3, "fog", 2.0, "good"], [4, "gof", 3.0, "best"]];
- const columns = ["A", "B", "C", "d"];
- const df = new dfd.DataFrame(data, { columns: columns });
-
- const df1 = dfd.get_dummies(df, { prefix: ["F", "G"], prefixSeparator: "_", columns: ["A", "d"] });
- const expectedColumns = [
- 'B', 'C',
- 'F_1', 'F_3',
- 'F_4', 'G_fat',
- 'G_good', 'G_best'
- ];
-
- const expected = [['dog', 1.0, 1, 0, 0, 1, 0, 0],
- ['fog', 2.0, 0, 1, 0, 0, 1, 0],
- ['gof', 3.0, 0, 0, 1, 0, 0, 1]];
-
- assert.deepEqual(df1.values, expected);
- assert.deepEqual(df1.columns, expectedColumns);
-
- });
-
- it("get_dummies auto infers and encode columns with string dtype", function () {
-
- const data = [[1, "dog", 1.0, "fat"], [3, "fog", 2.0, "good"], [4, "gof", 3.0, "best"]];
- const columns = ["A", "B", "C", "d"];
- const df = new dfd.DataFrame(data, { columns: columns });
-
- const df1 = dfd.get_dummies(df, { prefixSeparator: "_" });
- const expectedColumns = [
- 'A', 'C',
- 'B_dog', 'B_fog',
- 'B_gof', 'd_fat',
- 'd_good', 'd_best'
- ];
- const expected = [
- [
- 1, 1, 1, 0,
- 0, 1, 0, 0
- ],
- [
- 3, 2, 0, 1,
- 0, 0, 1, 0
- ],
- [
- 4, 3, 0, 0,
- 1, 0, 0, 1
- ]
- ];
- assert.deepEqual(df1.values, expected);
- assert.deepEqual(df1.columns, expectedColumns);
-
- });
-
- it("should one hot encode all other columns", function () {
-
- const data = [[1, "dog", 1.0, "fat"], [3, "fog", 2.0, "good"], [4, "gof", 3.0, "best"]];
- const columns = ["A", "B", "C", "d"];
- const df = new dfd.DataFrame(data, { columns: columns });
- const rslt = [
- [1, 'dog', 1, 1, 0, 0],
- [3, 'fog', 2, 0, 1, 0],
- [4, 'gof', 3, 0, 0, 1]
- ];
-
- assert.deepEqual(dfd.get_dummies(df, { columns: ["d"] }).values, rslt);
-
- });
-
-
- it("Dummification works for object DF", function () {
-
- let data = {
- fruits: ['pear', 'mango', "pawpaw", "mango", "bean"],
- Count: [20, 30, 89, 12, 30],
- Country: ["NG", "NG", "GH", "RU", "RU"]
- };
-
- let df = new dfd.DataFrame(data);
- const expected = [
- [
- 20, 1, 0, 0,
- 0, 1, 0, 0
- ],
- [
- 30, 0, 1, 0,
- 0, 1, 0, 0
- ],
- [
- 89, 0, 0, 1,
- 0, 0, 1, 0
- ],
- [
- 12, 0, 1, 0,
- 0, 0, 0, 1
- ],
- [
- 30, 0, 0, 0,
- 1, 0, 0, 1
- ]
- ];
-
- let dum_df = dfd.get_dummies(df, { prefixSeparator: "_" });
- assert.deepEqual(dum_df.values, expected);
-
- });
-});
diff --git a/danfojs-browser/tests/core/series.js b/danfojs-browser/tests/core/series.js
deleted file mode 100644
index 0db59136..00000000
--- a/danfojs-browser/tests/core/series.js
+++ /dev/null
@@ -1,1328 +0,0 @@
-/* eslint-disable no-undef */
-
-const tf = require('@tensorflow/tfjs');
-
-describe("Series", function () {
- describe("tensor", function () {
- it("Returns the tensor object of a Series", function () {
- let data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.dtype, "int32");
- });
- it("Returns the float dtype of a tensor object", function () {
- let data = [1.1, 2.2, 3, 4.1, 5, 620, 30.1, 40, 39, 89, 78];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.dtype, "float32");
- });
- it("Compares a tensor returned from a Series to Tensorflow's tensor", function () {
- let data = [1.1, 2.2, 3, 4.1, 5, 620, 30.1, 40, 39, 89, 78];
- let sf = new dfd.Series(data);
- let tf_data = tf.tensor(data);
- assert.deepEqual(sf.tensor.arraySync(), tf_data.arraySync());
- });
- });
- describe("tensor", function () {
- it("Returns the dtype string Series", function () {
- let data = ["b", "c", "d"];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.dtype, "string");
- });
- it("Returns the dtype int Series", function () {
- let data = [1, 2, 3, 4, 5];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.dtype, "int32");
- });
- it("Returns the dtype int Series", function () {
- let data = [1.1, 2.2, 3.3, 4.5, 5];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.dtype, "float32");
- });
- });
-
- describe("head", function () {
- it("Gets the first n rows in a Series", function () {
- let data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
- let cols = ["A"];
- let sf = new dfd.Series(data, { columns: cols });
- assert.deepEqual(sf.head(2).values, [1, 2]);
- });
- });
-
- describe("tail", function () {
- it("Prints the last n rows of a Series", function () {
- let data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.tail(2).values, [89, 78]);
- });
- });
-
- describe("sample", function () {
- it("Samples n number of random elements from a DataFrame", async function () {
- let data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
- let sf = new dfd.Series(data);
- assert.deepEqual((await sf.sample(7)).values.length, 7);
- });
- it("Throw error if n is greater than lenght of Series", async function () {
- let data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
- let sf = new dfd.Series(data);
- try {
- await sf.sample(100);
- } catch (e) {
- expect(e).to.be.instanceOf(Error);
- expect(e.message).to.eql('Sample size n cannot be bigger than size of dataset');
- }
- });
- });
-
- describe("add", function () {
- it("Return Addition of series with another series", function () {
- let data = [1, 2, 3, 4, 5, 6];
- let data2 = [30, 40, 39, 1, 2, 1];
- let sf = new dfd.Series(data);
- let sf2 = new dfd.Series(data2);
- assert.deepEqual(sf.add(sf2).values, [31, 42, 42, 5, 7, 7]);
- });
- it("Return Addition of series with a single value (Broadcasting)", function () {
- let data = [1, 2, 3, 4, 5];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.add(1).values, [2, 3, 4, 5, 6]);
- });
- it("Throws type error on addition of string type", function () {
- let data = [1, 2, 3, 4];
- let data2 = ["A", "B", "C", "d"];
- let sf = new dfd.Series(data);
- let sf2 = new dfd.Series(data2);
- assert.throws(
- () => {
- sf.add(sf2);
- },
- Error,
- "DtypeError: String data type does not support add operation"
- );
- });
- it("Throws length error if series lenght mixmatch", function () {
- let data = [1, 2, 3, 4];
- let data2 = [1, 2, 3, 4, 5, 6];
- let sf = new dfd.Series(data);
- let sf2 = new dfd.Series(data2);
- assert.throws(() => { sf.add(sf2); }, Error, "ParamError: Row length mismatch. Length of other (6), must be the same as Ndframe (4)");
- });
- });
-
- describe("sub", function () {
- it("Return Subtraction of series with another series", function () {
- let data1 = [30, 40, 39, 1, 2, 1];
- let data2 = [1, 2, 3, 4, 5, 6];
- let sf1 = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- assert.deepEqual(sf1.sub(sf2).values, [29, 38, 36, -3, -3, -5]);
- });
- it("Return Subtraction of series with a single value (Broadcasting)", function () {
- let data = [1, 2, 3, 4, 5];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.sub(1).values, [0, 1, 2, 3, 4]);
- });
- it("Throws type error on Subtraction of string type", function () {
- let data = [1, 2, 3, 4];
- let data2 = ["A", "B", "C", "d"];
- let sf = new dfd.Series(data);
- let sf2 = new dfd.Series(data2);
- assert.throws(
- () => {
- sf.sub(sf2);
- },
- Error,
- "DtypeError: String data type does not support sub operation"
- );
- });
- it("Throws length error if series lenght mixmatch", function () {
- let data = [1, 2, 3, 4];
- let data2 = [1, 2, 3, 4, 5, 6];
- let sf = new dfd.Series(data);
- let sf2 = new dfd.Series(data2);
- assert.throws(() => { sf.sub(sf2); }, Error, "ParamError: Row length mismatch. Length of other (6), must be the same as Ndframe (4)");
- });
- });
-
- describe("mul", function () {
- it("Return multiplication of series with another series", function () {
- let data1 = [30, 40, 3, 5];
- let data2 = [1, 2, 3, 4];
- let sf1 = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- assert.deepEqual(sf1.mul(sf2).values, [30, 80, 9, 20]);
- });
- it("Return multiplication of series with a single value (Broadcasting)", function () {
- let data = [1, 2, 3, 4, 5];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.mul(1).values, [1, 2, 3, 4, 5]);
- });
- it("Throws type error on multiplication of string type", function () {
- let data = [1, 2, 3, 4];
- let data2 = ["A", "B", "C", "d"];
- let sf = new dfd.Series(data);
- let sf2 = new dfd.Series(data2);
- assert.throws(() => { sf.mul(sf2); }, Error, "DtypeError: String data type does not support mul operation");
- });
- it("Throws length error if series lenght mixmatch", function () {
- let data = [1, 2, 3, 4];
- let data2 = [1, 2, 3, 4, 5, 6];
- let sf = new dfd.Series(data);
- let sf2 = new dfd.Series(data2);
- assert.throws(() => { sf.mul(sf2); }, Error, "ParamError: Row length mismatch. Length of other (6), must be the same as Ndframe (4)");
- });
- });
-
- describe("div", function () {
- it("Return float division of series with another series", function () {
- let data1 = [30, 40, 3, 5];
- let data2 = [1, 2, 3, 4];
- let sf1 = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- assert.deepEqual(sf1.div(sf2).values, [30, 20, 1, 1.25]);
- });
- it("Return division of series with a single value (Broadcasting)", function () {
- let data = [10, 2, 3, 90];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.div(2).values, [5, 1, 1.5, 45]);
- });
- });
-
- describe("pow", function () {
- it("Return Exponetial power of series with another series", function () {
- let data1 = [2, 3, 4, 5];
- let data2 = [1, 2, 3, 0];
- let sf1 = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- assert.deepEqual(sf1.pow(sf2).values, [2, 9, 64, 1]);
- });
- it("Return Exponetial power of series with a single value (Broadcasting)", function () {
- let data = [1, 2, 3, 4, 5];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.pow(2).values, [1, 4, 9, 16, 25]);
- });
- });
-
- describe("mod", function () {
- it("Return modulo of series with another float series", function () {
- let data1 = [2, 30, 4, 5];
- let data2 = [1.1, 2.2, 3.3, 2.4];
- let sf1 = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- let expected = [
- 0.8999999999999999,
- 1.3999999999999977,
- 0.7000000000000002,
- 0.20000000000000018
- ];
- assert.deepEqual(sf1.mod(sf2).values, expected);
- });
- it("Return modulo of series with another int series", function () {
- let data1 = [2, 30, 4, 5];
- let data2 = [1, 2, 3, 1];
- let sf1 = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- assert.deepEqual(sf1.mod(sf2).values, [0, 0, 1, 0]);
- });
- it("Return modulo power of series with a single value (Broadcasting)", function () {
- let data = [1, 2, 3, 4, 5];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.mod(2).values, [1, 0, 1, 0, 1]);
- });
- });
-
- describe("mean", function () {
- it("Computes the mean of elements in a int series", function () {
- let data1 = [30, 40, 3, 5];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.mean(), 19.5);
- });
- it("Computes the mean of elements in a float series", function () {
- let data1 = [30.1, 40.2, 3.1, 5.1];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.mean(), 19.625);
- });
- it("Computes the mean of elements in a float series with NaN", function () {
- let data1 = [30.1, 40.2, 3.1, 5.1, NaN];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.mean(), 19.625);
- });
- it("Throws error if dtype is string", function () {
- let data1 = ["boy", "girl", "Man"];
- let sf = new dfd.Series(data1);
- assert.throws(
- () => {
- sf.mean();
- },
- Error,
- "DtypeError: String data type does not support mean operation"
- );
- });
- });
-
- describe("median", function () {
- it("Computes the median value of elements across int Series", function () {
- let data1 = [30, 40, 3, 5];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.median(), 17.5);
- });
- it("Computes the median value of elements across float Series", function () {
- let data1 = [30.1, 40.2, 3.1, 5.1, NaN];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.median(), 17.6);
- });
- });
-
- describe("sum", function () {
- it("Sum values of a Int Series", function () {
- let data1 = [30, 40, 3, 5, 5, 5, 5, 5, 3, 3, 3, 21, 3];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.sum(), 131);
- });
- it("Sum values of a Float Series", function () {
- let data1 = [30.1, 3.1, 40.2, 3.1, 5.1];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.sum(), 81.6);
- });
- it("Sum values of a bool Series", function () {
- let data1 = [true, true, false, false, false];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.sum(), 2);
- });
- it("Sum values a Series with missing values", function () {
- let data1 = [11, NaN, 2, 2];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.sum(), 15);
- });
- });
-
- describe("mode", function () {
- it("Computes the multi-modal values of a Series", function () {
- let data1 = [30, 40, 3, 5, 5, 5, 5, 5, 3, 3, 3, 21, 3];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.mode(), [5, 3]);
- });
- it("Computes the modal value of a Series", function () {
- let data1 = [30.1, 3.1, 40.2, 3.1, 5.1];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.mode(), [3.1]);
- });
- });
-
- describe("min", function () {
- it("Returns the single smallest elementin a Series", function () {
- let data = [30, 40, 3, 5];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.min(), 3);
- });
- it("Computes the minimum of elements across an float Series", function () {
- let data1 = [30.1, 40.2, 3.12, 5.1];
- let sf = new dfd.Series(data1, { dtypes: ["float32"] });
- assert.deepEqual(Number(sf.min().toFixed(2)), 3.12);
- });
- });
-
- describe("max", function () {
- it("Computes the maximum of elements across dimensions of a Series", function () {
- let data1 = [30, 40, 3, 5];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.max(), 40);
- });
- it("Return sum of float values in a series", function () {
- let data1 = [30.1, 40.21, 3.1, 5.1];
- let sf = new dfd.Series(data1);
- assert.deepEqual(Number(sf.max().toFixed(2)), 40.21);
- });
- it("Throws error on addition of string Series", function () {
- let data1 = ["boy", "gitl", "woman", "man"];
- let sf = new dfd.Series(data1);
- assert.throws(
- () => {
- sf.max();
- },
- Error,
- "DtypeError: String data type does not support max operation"
- );
- });
- });
-
- describe("std", function () {
- it("Computes the standard of elements in a int series", function () {
- let data1 = [30, 40, 3, 5];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.std(), 18.375708603116962);
- });
- it("Computes the standard deviation of elements in a float series", function () {
- let data1 = [30.1, 40.2, 3.1, 5.1];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.std(), 18.412925713566906);
- });
- it("Computes the standard deviation of elements in a float series with missing values", function () {
- let data1 = [30, 40, 3, 5, undefined];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.std(), 18.375708603116962);
- });
- });
-
- describe("var", function () {
- it("Computes the variance of elements in a int series", function () {
- let data1 = [30, 40, 3, 5];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.var(), 337.6666666666667);
- });
- it("Computes the variance of elements in a float series", function () {
- let data1 = [30.1, 40.2, 3.1, 5.1];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.var(), 339.03583333333336);
- });
- it("Computes the variance of elements in a int series with missing values", function () {
- let data1 = [30, undefined, 40, 3, 5];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.var(), 337.6666666666667);
- });
- });
-
- describe("describe", function () {
- it("Computes the descriptive statistics on an int Series", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.describe().round().values, [
- 7,
- 27,
- 17.4,
- 10,
- 23,
- 56,
- 302
- ]);
- });
- it("Computes the descriptive statistics on a float Series", function () {
- let data1 = [30.1, 40.2, 3.1, 5.1];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.describe().round().values, [4, 19.6, 18.4, 3.1, 17.6, 40.2, 339]);
- });
- it("Computes the descriptive statistics on a float Series", function () {
- let data1 = [30.1, 40.2, 3.1, 5.1];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.describe().index, [
- "count",
- "mean",
- "std",
- "min",
- "median",
- "max",
- "variance"
- ]);
- });
- });
-
- describe("maximum", function () {
- it("Returns the max of a and b (a > b ? a : b) element-wise. Supports broadcasting.", function () {
- let data1 = [30, 40, 3, 5];
- let data2 = [10, 41, 2, 0];
- let sf1 = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- assert.deepEqual(sf1.maximum(sf2).values, [30, 41, 3, 5]);
- });
- it("Throws error on checking maximum of incompatible Series", function () {
- let data1 = [30, 40, 3, 5];
- let data2 = [10, 41, 2];
- let sf1 = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- assert.throws(
- () => {
- sf1.maximum(sf2);
- },
- Error,
- "ParamError: Row length mismatch. Length of other (3), must be the same as Ndframe (4)"
- );
- });
- });
-
- describe("minimum", function () {
- it("Returns the min of a and b (a < b ? a : b) element-wise. Supports broadcasting.", function () {
- let data1 = [30, 40, 3, 5];
- let data2 = [10, 41, 2, 0];
- let sf1 = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- assert.deepEqual(sf1.minimum(sf2).values, [10, 40, 2, 0]);
- });
- it("Return sum of float values in a series", function () {
- let data1 = [30.1, 40.9, 3, 5];
- let data2 = [10.2, 41, 2, 0];
- let sf1 = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- assert.deepEqual(sf1.minimum(sf2).values, [10.2, 40.9, 2, 0]);
-
- });
- });
-
- describe("count", function () {
- it("Returns the count of non NaN values in a string Series", function () {
- let data = ["boy", "gitl", "woman", NaN];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.count(), 3);
- });
- it("Returns the count of non NaN values in a string Series", function () {
- let data = ["boy", "gitl", "woman", "Man"];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.count(), 4);
- });
- it("Returns the count of non NaN values in a int Series", function () {
- let data = [20, 30, NaN, 2, NaN, 30, 21];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.count(), 5);
- });
- it("Returns the count of non NaN values in a float Series", function () {
- let data = [20.1, 30.4, NaN, 2.1, NaN, 30.0, 21.3];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.count(), 5);
- });
- });
-
- describe("round", function () {
- it("Rounds elements in a Series to nearest whole number", function () {
- let data1 = [30.21091, 40.190901, 3.564, 5.0212];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.round(0).values, [30, 40, 4, 5]);
- });
- it("Rounds elements in a Series to 1dp", function () {
- let data1 = [30.21091, 40.190901, 3.564, 5.0212];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.round(1).values, [30.2, 40.2, 3.6, 5.0]);
- });
- it("Rounds elements in a Series to 2dp", function () {
- let data1 = [30.2191, 40.190901, 3.564, 5.0212];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.round(2).values, [30.22, 40.19, 3.56, 5.02]);
- });
- });
-
- describe("isna", function () {
- it("Return a boolean same-sized object indicating if string Series contain NaN", function () {
- let data1 = [NaN, undefined, "girl", "Man"];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.isna().values, [true, true, false, false]);
- });
- it("Return a boolean same-sized object indicating if float Series values are NaN", function () {
- let data1 = [30.21091, NaN, 3.564, undefined];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.isna().values, [false, true, false, true]);
- });
- it("Return a boolean same-sized object indicating if int Series values are NaN", function () {
- let data1 = [30, 40, 3, 5, undefined, undefined];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.isna().values, [
- false,
- false,
- false,
- false,
- true,
- true
- ]);
- });
- });
-
- describe("sort_values", function () {
- it("Sort values in a Series in ascending order (not inplace)", function () {
- let sf = new dfd.Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
- let result = [0, 1, 2, 4, 4, 20, 30, 57, 89];
- let sorted_sf = sf.sort_values();
- assert.deepEqual(sorted_sf.values, result);
- });
- it("confirms that sort_values in ascending order does not happen inplace", function () {
- let sf = new dfd.Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
- let result = [0, 1, 2, 4, 4, 20, 30, 57, 89];
- let expected_index = [7, 2, 3, 8, 4, 0, 1, 5, 6];
- sf.sort_values({ inplace: true });
- assert.deepEqual(sf.values, result);
- assert.deepEqual(sf.index, expected_index);
- });
- it("Sort values in a Series in Descending order", function () {
- let sf = new dfd.Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
- let result = [89, 57, 30, 20, 4, 4, 2, 1, 0];
- let sorted_sf = sf.sort_values({ ascending: false });
- assert.deepEqual(sorted_sf.values, result);
- });
- it("confirms that sort_values in descending order happens inplace", function () {
- let sf = new dfd.Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
- let result = [89, 57, 30, 20, 4, 4, 2, 1, 0];
- sf.sort_values({ ascending: false, inplace: true });
- assert.deepEqual(sf.values, result);
- });
- it("Confirms that series index is sorted in ascending order (not in inplace)", function () {
- let sf = new dfd.Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
- let result = [7, 2, 3, 8, 4, 0, 1, 5, 6];
- let sorted_sf = sf.sort_values();
- assert.deepEqual(sorted_sf.index, result);
- });
- it("Confirms that series index is sorted in descending order (not in inplace)", function () {
- let sf = new dfd.Series([20, 30, 1, 2, 4, 57, 89, 0, 4]);
- let result = [6, 5, 1, 0, 4, 8, 3, 2, 7];
- let sorted_sf = sf.sort_values({ ascending: false });
- assert.deepEqual(sorted_sf.index, result);
- });
- it("Sort string values in a Series", function () {
- let sf = new dfd.Series(["boy", "zebra", "girl", "man"]);
- let result = ["boy", "girl", "man", "zebra"];
- let sorted_sf = sf.sort_values({ ascending: false });
- assert.deepEqual(sorted_sf.values, result);
- });
- });
-
- describe("copy", function () {
- it("Checks if copied values are the same as the first one", function () {
- let sf = new dfd.Series([30.21091, 40.190901, 3.564, 5.0212]);
- let sf_copy = sf.copy();
- assert.deepEqual(sf.values, sf_copy.values);
- });
- it("Checks if copied index are the same", function () {
- let sf = new dfd.Series([30.21091, 40.190901, 3.564, 5.0212]);
- sf = sf.set_index({ index: ["a", "b", "c", "d"] });
- let sf_copy = sf.copy();
- assert.deepEqual(sf.index, sf_copy.index);
- });
- it("Checks if copied dtype is the same", function () {
- let sf = new dfd.Series([30.21091, 40.190901, 3.564, 5.0212]);
- sf.round();
- sf.astype('int32');
- let sf_copy = sf.copy();
- assert.deepEqual(sf.dtypes[0], sf_copy.dtypes[0]);
- assert.deepEqual(sf.values, sf_copy.values);
-
- });
- });
-
- describe("reset_index", function () {
- it("resets the index of a Series", function () {
- let data = [
- { alpha: "A", count: 1 },
- { alpha: "B", count: 2 },
- { alpha: "C", count: 3 }
- ];
- let df = new dfd.Series(data);
- let df_new = df.set_index({ index: ["one", "two", "three"] });
- let df_reset = df_new.reset_index();
- assert.deepEqual(df_reset.index, [0, 1, 2]);
- });
- it("Reset the index of a Series created from an Array", function () {
- let data = [1, 2, 3, 4, 5, 6];
- let df = new dfd.Series(data);
- df.set_index({
- index: ["one", "two", "three", "four", "five", "six"],
- inplace: true
- });
- let df_new = df.reset_index();
- assert.deepEqual(df_new.index, [0, 1, 2, 3, 4, 5]);
- });
- it("checks that the original series changed after reseting new index inplace", function () {
- let data = [
- { alpha: "A", count: 1 },
- { alpha: "B", count: 2 },
- { alpha: "C", count: 3 }
- ];
- let df = new dfd.Series(data);
- df.reset_index({ inplace: true });
- assert.deepEqual(df.index, [0, 1, 2]);
- });
- });
-
- describe("set_index", function () {
- it("sets the index of an Series", function () {
- let data = [
- { alpha: "A", count: 1 },
- { alpha: "B", count: 2 },
- { alpha: "C", count: 3 }
- ];
- let df = new dfd.Series(data);
- let df_new = df.set_index({ index: ["one", "two", "three"] });
- assert.deepEqual(df_new.index, ["one", "two", "three"]);
- assert.notDeepEqual(df.index, df_new.index);
- });
- it("checks that the original series is not modified after setting new index not-inplace", function () {
- let data = [
- { alpha: "A", count: 1 },
- { alpha: "B", count: 2 },
- { alpha: "C", count: 3 }
- ];
- let df = new dfd.Series(data);
- let df_new = df.set_index({ index: ["one", "two", "three"] });
- assert.notDeepEqual(df.index, df_new.index);
- });
- it("sets the index of an Series inplace", function () {
- let data = [12, 2, 20, 50];
- let df = new dfd.Series(data);
- df.set_index({ index: ["one", "two", "three", "four"], inplace: true });
- assert.deepEqual(df.index, ["one", "two", "three", "four"]);
- });
- });
-
- describe("Map", function () {
- it("map series element to object keys", function () {
- let sf = new dfd.Series([1, 2, 3, 4]);
- let map = { 1: "ok", 2: "okie", 3: "frit", 4: "gop" };
-
- let rslt = ["ok", "okie", "frit", "gop"];
-
- assert.deepEqual(sf.map(map).values, rslt);
- });
-
- it("map series element to a function statement", function () {
- let sf = new dfd.Series([1, 2, 3, 4]);
- let func_map = (x) => {
- return x + 1;
- };
-
- let rslt = [2, 3, 4, 5];
-
- assert.deepEqual(sf.map(func_map).values, rslt);
- });
- });
-
- describe("Apply", function () {
- it("apply a function to a series element", function () {
- let sf = new dfd.Series([1, 2, 3, 4, 5, 6, 7, 8]);
-
- let apply_func = (x) => {
- return x + x;
- };
-
- let rslt = [2, 4, 6, 8, 10, 12, 14, 16];
- assert.deepEqual(sf.apply(apply_func).values, rslt);
- });
- });
-
- describe("unique", function () {
- it("returns the unique values in a Series of type int", function () {
- let sf = new dfd.Series([1, 2, 3, 4, 5, 6, 7, 8, 1, 1, 22, 8, 5, 5, 5]);
- let expected = [1, 2, 3, 4, 5, 6, 7, 8, 22];
- assert.deepEqual(sf.unique().values, expected);
- });
- it("returns the unique values in a Series of type string", function () {
- let sf = new dfd.Series(["a", "a", "b", "c", "c", "d", "e", "d", "d", "e"]);
- let expected = ["a", "b", "c", "d", "e"];
- assert.deepEqual(sf.unique().values, expected);
- });
- it("returns the unique values in a Series of type string", function () {
- let sf = new dfd.Series(["a", "a", "b", "c", "c", "d", "e", "d", "d", "e"]);
- let expected = ["a", "b", "c", "d", "e"];
- assert.deepEqual(sf.unique().values, expected);
- });
- it("returns the nunique values in a Series of type string", function () {
- let sf = new dfd.Series(["a", "a", "b", "c", "c", "d", "e", "d", "d", "e"]);
- let expected = 5;
- assert.deepEqual(sf.nunique(), expected);
- });
- });
-
- describe("value_counts", function () {
- it("returns the unique values and their counts in a Series of type int", function () {
- let sf = new dfd.Series([1, 2, 3, 4, 5, 6, 7, 8, 1, 1, 22, 8, 5, 5, 5]);
- let expected_index = [1, 2, 3, 4, 5, 6, 7, 8, 22];
- let expected_vals = [3, 1, 1, 1, 4, 1, 1, 2, 1];
- assert.deepEqual(sf.value_counts().values, expected_vals);
- assert.deepEqual(sf.value_counts().index, expected_index);
- });
- it("returns the unique values and their counts in a Series of type string", function () {
- let sf = new dfd.Series(["a", "a", "b", "c", "c", "d", "e", "d", "d", "e"]);
- let expected_vals = [2, 1, 2, 3, 2];
- let expected_index = ["a", "b", "c", "d", "e"];
- assert.deepEqual(sf.value_counts().values, expected_vals);
- assert.deepEqual(sf.value_counts().index, expected_index);
- });
- });
-
- describe("abs", function () {
- it("Returns the absolute values in Series", function () {
- let data1 = [-10, 45, 56, -25, 23, -20, 10];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.abs().values, [10, 45, 56, 25, 23, 20, 10]);
- });
- it("Computes the descriptive statistics on a float Series", function () {
- let data1 = [-30.1, -40.2, -3.1, -5.1];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.abs().values, [30.1, 40.2, 3.1, 5.1]);
- });
- });
-
- describe("fillna", function () {
- it("replace all NaN value with specified value", function () {
- let data = [NaN, 1, 2, 33, 4, NaN, 5, 6, 7, 8];
- let sf = new dfd.Series(data);
- let sf_val = [-999, 1, 2, 33, 4, -999, 5, 6, 7, 8];
- sf.fillna({ value: -999, inplace: true });
- assert.deepEqual(sf.values, sf_val);
- });
- it("replace all NaN value in string Series with specified value", function () {
- let data = [NaN, "boy", NaN, "hey", "Man", undefined];
- let sf = new dfd.Series(data);
- let sf_val = ["filled", "boy", "filled", "hey", "Man", "filled"];
- let sf_fill = sf.fillna({ value: "filled" });
- assert.deepEqual(sf_fill.values, sf_val);
- });
- });
-
- describe("cumsum", function () {
- it("Return cumulative sum over a Series", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.cumsum().values, [10, 55, 111, 136, 159, 179, 189]);
- });
- });
-
- describe("cummax", function () {
- it("Return cumulative maximum over a Series", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.cummax().values, [10, 45, 56, 56, 56, 56, 56]);
- });
- });
-
- describe("cummin", function () {
- it("Return cumulative minimum over a Series", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let sf = new dfd.Series(data1);
- assert.deepEqual(sf.cummin().values, [10, 10, 10, 10, 10, 10, 10]);
- });
- });
-
- describe("cumprod", function () {
- it("Return cumulative product over a Series", function () {
- let data1 = [1, 2, 10, 3, 12, 14, 1];
- let sf = new dfd.Series(data1);
- let rslt = [1, 2, 20, 60, 720, 10080, 10080];
- assert.deepEqual(sf.cumprod().values, rslt);
- });
- });
-
- describe("lt", function () {
- it("Return Less than of series and other series (element-wise)", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let data2 = [100, 450, 590, 5, 25, 2, 0];
-
- let sf = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- let expected = [true, true, true, false, true, false, false];
- assert.deepEqual(sf.lt(sf2).values, expected);
- });
-
- it("Return Less than of series scalar (element-wise)", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let sf = new dfd.Series(data1);
- let expected = [true, false, false, true, true, true, true];
- assert.deepEqual(sf.lt(30).values, expected);
- });
- it("Correct index is returned after operation", function () {
- const data1 = [true, true, true, false, false];
- const data2 = [true, false, true, true, false];
- const sf = new dfd.Series(data1, { index: ["one", "two", "three", "four", "five"] });
-
- const expected = ["one", "two", "three", "four", "five"];
- assert.deepEqual(sf.lt(data2).index, expected);
- });
- });
-
- describe("gt", function () {
- it("Return Greater than of series and other series (element-wise)", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let data2 = [100, 450, 590, 5, 25, 2, 0];
-
- let sf = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- let expected = [false, false, false, true, false, true, true];
- assert.deepEqual(sf.gt(sf2).values, expected);
- });
-
- it("Return Greater than of series scalar (element-wise)", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let sf = new dfd.Series(data1);
- let expected = [false, true, true, false, false, false, false];
- assert.deepEqual(sf.gt(30).values, expected);
- });
- });
-
- describe("le", function () {
- it("Return Less than or Equal to of series and other series (element-wise)", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let data2 = [100, 450, 590, 5, 25, 2, 0];
-
- let sf = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- let expected = [true, true, true, false, true, false, false];
- assert.deepEqual(sf.le(sf2).values, expected);
- });
-
- it("Return Less than or Equal to of series scalar (element-wise)", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let sf = new dfd.Series(data1);
- let expected = [true, false, false, true, true, true, true];
- assert.deepEqual(sf.le(30).values, expected);
- });
- });
-
- describe("ge", function () {
- it("Return Greater than or Equal to of series and other series (element-wise)", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let data2 = [100, 450, 56, 5, 25, 20, 0];
-
- let sf = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- let expected = [false, false, true, true, false, true, true];
- assert.deepEqual(sf.ge(sf2).values, expected);
- });
-
- it("Return Greater than or Equal to of series scalar (element-wise)", function () {
- let data1 = [30, 45, 56, 25, 23, 20, 10];
- let sf = new dfd.Series(data1);
- let expected = [true, true, true, false, false, false, false];
- assert.deepEqual(sf.ge(30).values, expected);
- });
- });
-
- describe("ne", function () {
- it("Return Not Equal to of series and other series (element-wise)", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let data2 = [10, 450, 56, 5, 25, 2, 0];
-
- let sf = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- let expected = [false, true, false, true, true, true, true];
- assert.deepEqual(sf.ne(sf2).values, expected);
- });
-
- it("Return Not Equal to of series scalar (element-wise)", function () {
- let data1 = [10, 30, 56, 30, 23, 20, 10];
- let sf = new dfd.Series(data1);
- let expected = [true, false, true, false, true, true, true];
- assert.deepEqual(sf.ne(30).values, expected);
- });
- });
-
- describe("eq", function () {
- it("Return Equal to of series and other series (element-wise)", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let data2 = [100, 450, 590, 25, 25, 2, 0];
-
- let sf = new dfd.Series(data1);
- let sf2 = new dfd.Series(data2);
- let expected = [false, false, false, true, false, false, false];
- assert.deepEqual(sf.eq(sf2).values, expected);
- });
-
- it("Return Equal to of series scalar (element-wise)", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 30];
- let sf = new dfd.Series(data1);
- let expected = [false, false, false, false, false, false, true];
- assert.deepEqual(sf.eq(30).values, expected);
- });
- });
-
- describe("replace", function () {
- it("Replace values given in replace param with value", function () {
- let data1 = [10, 45, 56, 25, 23, 20, 10];
- let sf = new dfd.Series(data1);
- let expected = [-50, 45, 56, 25, 23, 20, -50];
- let df_rep = sf.replace({ oldValue: 10, newValue: -50 });
- assert.deepEqual(df_rep.values, expected);
- });
-
- it("Replace values given in replace param with value (String type)", function () {
- let data1 = ["A", "A", "A", "B", "B", "C", "C", "D"];
- let sf = new dfd.Series(data1);
- let expected = ["boy", "boy", "boy", "B", "B", "C", "C", "D"];
- sf.replace({ oldValue: "A", newValue: "boy", inplace: true });
- assert.deepEqual(sf.values, expected);
- });
- it("Throw error on wrong param passed", function () {
- let data1 = ["A", "A", "A", "B", "B", "C", "C", "D"];
- let sf = new dfd.Series(data1);
- let expected = `Params Error: Must specify param 'oldValue' to replace`;
- assert.throws(
- () => {
- sf.replace({ newValue: "boy", inplace: true });
- },
- Error,
- expected
- );
- });
- });
-
- describe("drop_duplicates", function () {
- it("Return Series with duplicate values removed (Default, first values kept)", function () {
- let data1 = [10, 45, 56, 10, 23, 20, 10, 10];
- let sf = new dfd.Series(data1);
- let expected = [10, 45, 56, 23, 20];
- let expected_index = [0, 1, 2, 4, 5];
- let df_drop = sf.drop_duplicates();
- assert.deepEqual(df_drop.values, expected);
- assert.deepEqual(df_drop.index, expected_index);
- });
-
- it("Return Series with duplicate values removed (last values kept)", function () {
- let data1 = [10, 45, 56, 10, 23, 20, 10, 10];
- let sf = new dfd.Series(data1);
- let expected = [45, 56, 23, 20, 10];
- let expected_index = [1, 2, 4, 5, 7];
- let df_drop = sf.drop_duplicates({ keep: "last" });
- assert.deepEqual(df_drop.values, expected);
- assert.deepEqual(df_drop.index, expected_index);
- });
-
- it("Return Series with duplicate values removed (String)", function () {
- let data1 = ["A", "A", "A", "B", "B", "C", "C", "D"];
- let sf = new dfd.Series(data1);
- let expected = ["A", "B", "C", "D"];
- let expected_index = [0, 3, 5, 7];
- sf.drop_duplicates({ inplace: true });
- assert.deepEqual(sf.values, expected);
- assert.deepEqual(sf.index, expected_index);
- });
- });
-
- describe("dropna", function () {
- it("Return a new dfd.Series with missing values removed (Int)", function () {
- let data1 = [10, 45, undefined, 10, 23, 20, undefined, 10];
- let sf = new dfd.Series(data1);
- let expected = [10, 45, 10, 23, 20, 10];
- let expected_index = [0, 1, 3, 4, 5, 7];
- let sf_drop = sf.dropna();
- assert.deepEqual(sf_drop.values, expected);
- assert.deepEqual(sf_drop.index, expected_index);
- });
-
- it("Return a new dfd.Series with missing values removed (String)", function () {
- let data1 = ["A", NaN, "A", "B", "B", NaN, "C", undefined];
- let sf = new dfd.Series(data1);
- let expected = ["A", "A", "B", "B", "C"];
- let expected_index = [0, 2, 3, 4, 6];
-
- sf.dropna({ inplace: true });
- assert.deepEqual(sf.values, expected);
- assert.deepEqual(sf.index, expected_index);
- });
- });
-
- describe("argsort", function () {
- it("Return the integer indices that would sort the Series values", function () {
- let data1 = [10, 45, 20, 10, 23, 20, 30, 11];
- let sf = new dfd.Series(data1);
- let expected = [3, 0, 7, 5, 2, 4, 6, 1];
- let sf_sort = sf.argsort();
- assert.deepEqual(sf_sort.values, expected);
- });
-
- it("Return the integer indices that would sort the Series values (Float)", function () {
- let data1 = [10.22, 4.5, 2.0, 10, 23.23, 20.1, 30, 11];
- let sf = new dfd.Series(data1);
- let expected = [2, 1, 3, 0, 7, 5, 4, 6];
- let sf_sort = sf.argsort(false);
- assert.deepEqual(sf_sort.values, expected);
- });
- });
-
- describe("argmax", function () {
- it("Return int position of the largest value in the Series.", function () {
- let data1 = [10, 45, 20, 10, 23, 20, 30, 11];
- let sf = new dfd.Series(data1);
- let expected = 1;
- let argmax = sf.argmax();
- assert.deepEqual(argmax, expected);
- });
-
- it("Return int position of the largest value in the Float Series.", function () {
- let data1 = [10.22, 4.5, 2.0, 10, 23.23, 20.1, 30, 11];
- let sf = new dfd.Series(data1);
- let expected = 6;
- let argmax = sf.argmax();
- assert.deepEqual(argmax, expected);
- });
- });
-
- describe("argmin", function () {
- it("Return int position of the smallest value in the Series", function () {
- let data1 = [10, 45, 20, 122, 23, 20, 30, 11];
- let sf = new dfd.Series(data1);
- let expected = 0;
- let argmin = sf.argmin();
- assert.deepEqual(argmin, expected);
- });
-
- it("Return int position of the smallest value in a Float Series", function () {
- let data1 = [10.22, 4.5, 2.0, 10, 23.23, 20.1, 30, 11];
- let sf = new dfd.Series(data1);
- let expected = 2;
- let argmin = sf.argmin();
- assert.deepEqual(argmin, expected);
- });
- });
-
- describe("Str", function () {
- it("Converts all characters to lowercase.", function () {
- let data = ["lower", "CAPITALS", "this is a sentence", "SwApCaSe"];
- let res = ["lower", "capitals", "this is a sentence", "swapcase"];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.str.toLowerCase().values, res);
- });
- it("Converts all characters to uppercase.", function () {
- let data = ["lower", "CAPITALS", "this is a sentence", "SwApCaSe"];
- let res = ["LOWER", "CAPITALS", "THIS IS A SENTENCE", "SWAPCASE"];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.str.toUpperCase().values, res);
- });
- it("Converts all characters to capital case.", function () {
- let data = ["lower", "CAPITALS", "this is a sentence", "SwApCaSe"];
- let res = ["Lower", "Capitals", "This is a sentence", "Swapcase"];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.str.capitalize().values, res);
- });
-
- it("Returns the character at the specified index (position)", function () {
- let data = ["lower", "CAPITALS", "this is a sentence", "SwApCaSe"];
- let res = ["w", "P", "i", "A"];
- let sf = new dfd.Series(data);
- assert.deepEqual(sf.str.charAt(2).values, res);
- });
-
- it("Throws error on concat of numeric series", function () {
- let data = [1, 2, 3, 4, 5, 6];
- let sf = new dfd.Series(data);
- assert.throws(
- () => {
- sf.str.concat("20", 1);
- },
- Error,
- "Cannot call accessor str on non-string type"
- );
-
- });
- });
-
- describe("dt", function () {
- it("check month generated", function () {
- let data = ["02Sep2019", "03Dec2019", "04Jan2019"];
- let sf = new dfd.Series(data);
- let new_data = [8, 11, 0];
- assert.deepEqual(sf.dt.month().values, new_data);
- });
-
- it("check month Name generated", function () {
- let data = ["06-30-02019", "07-29-2019", "08-28-2019"];
- let sf = new dfd.Series(data);
- let new_data = ["Jun", "Jul", "Aug"];
- assert.deepEqual(sf.dt.month_name().values, new_data);
- });
-
- it("check days of the weeks generated", function () {
- let data = ["06-30-02019", "07-29-2019", "08-28-2019"];
- let sf = new dfd.Series(data);
- let new_data = ["Sun", "Mon", "Wed"];
- assert.deepEqual(sf.dt.weekdays().values, new_data);
- });
-
- it("check day of the month generated", function () {
- let data = ["06-30-02019", "07-29-2019", "08-28-2019"];
- let sf = new dfd.Series(data);
- let new_data = [30, 29, 28];
- assert.deepEqual(sf.dt.monthday().values, new_data);
- });
- });
-
- describe("astype", function () {
- it("set type of float column to int", function () {
- let data = [-20.1, 30, 47.3, -20];
- let ndframe = new dfd.Series(data);
- let df = ndframe.astype("int32");
-
- assert.deepEqual(df.dtypes, ["int32"]);
- assert.deepEqual(df.values, [-20, 30, 47, -20]);
- });
- it("set type of int column to float", function () {
- let data = [34, -4, 5, 6];
- let ndframe = new dfd.Series(data);
- let df = ndframe.astype("float32");
- assert.deepEqual(df.dtypes, ["float32"]);
- assert.deepEqual(df.values, [34, -4, 5, 6]);
- });
- it("set type of string column to int", function () {
- let data = ["20.1", "21", "23.4", "50.78"];
- let ndframe = new dfd.Series(data);
- let df = ndframe.astype("int32");
-
- assert.deepEqual(df.dtypes, ["int32"]);
- assert.deepEqual(df.values, [20, 21, 23, 50]);
- });
- it("set type of string column to float", function () {
- let data = ["20.1", "21", "23.4", "50.78"];
- let ndframe = new dfd.Series(data);
- let df = ndframe.astype("float32");
-
- assert.deepEqual(df.dtypes, ["float32"]);
- assert.deepEqual(df.values, [20.1, 21, 23.4, 50.78]);
- });
- });
-
- describe("iloc", function () {
- it("indexing by list of index", function () {
- let data = [1, 2, 3, 4, "a", "b", "c"];
- let sf = new dfd.Series(data);
-
- let expected_val = [2, "a", 3, 4, "b"];
-
- assert.deepEqual(sf.iloc([1, 4, 2, 3, 5]).values, expected_val);
- });
- it("indexing by slicing", function () {
- let data = [1, 2, 3, 4, "a", "b", "c"];
- let sf = new dfd.Series(data);
-
- let expected_val = [2, 3, 4];
-
- assert.deepEqual(sf.iloc(["1:4"]).values, expected_val);
- });
- it("indexing by slicing format ':5' works", function () {
- let data = [1, 2, 3, 4, "a", "b", "c"];
- let sf = new dfd.Series(data);
- let expected_val = [1, 2, 3, 4, 'a'];
- assert.deepEqual(sf.iloc([":5"]).values, expected_val);
- });
- it("indexing by slicing format '2:` works", function () {
- let data = [1, 2, 3, 4, "a", "b", "c"];
- let sf = new dfd.Series(data);
- let expected_val = [3, 4, "a", "b", "c"];
- assert.deepEqual(sf.iloc(["2:"]).values, expected_val);
- });
- });
-
- describe("append", function () {
- it("Add a new single value to the end of a Series", function () {
- let data = [1, 2, 3, 4, "a", "b", "c"];
- let sf = new dfd.Series(data);
- let expected_val = [1, 2, 3, 4, "a", "b", "c", "d"];
- sf.append("d", "r1", { inplace: true });
- assert.deepEqual(sf.values, expected_val);
- });
- it("Add a new array of values to the end of a Series", function () {
- let data = [1, 2, 3, 4];
- let to_add = ["a", "b", "c"];
- let sf = new dfd.Series(data);
- let expected_val = [1, 2, 3, 4, "a", "b", "c"];
- sf.append(to_add, ["r1", "r2", "r3"], { inplace: true });
- assert.deepEqual(sf.values, expected_val);
- });
- it("Add a Series to the end of another Series", function () {
- let sf1 = new dfd.Series([1, 2, 3, 4]);
- let sf2 = new dfd.Series(["a", "b", "c"]);
- let expected_val = [1, 2, 3, 4, "a", "b", "c"];
- sf1.append(sf2, ["r1", "r2", "r3"], { inplace: true });
- assert.deepEqual(sf1.values, expected_val);
- });
- it("Add a new single value to the end of a Series", function () {
- let data = [1, 2, 3, 4, "a", "b", "c"];
- let sf = new dfd.Series(data);
- let expected_val = [1, 2, 3, 4, "a", "b", "c", "d"];
- sf = sf.append("d", "r1");
- assert.deepEqual(sf.values, expected_val);
- });
- it("Add a new array of values to the end of a Series", function () {
- let data = [1, 2, 3, 4];
- let to_add = ["a", "b", "c"];
- let sf = new dfd.Series(data);
- let expected_val = [1, 2, 3, 4, "a", "b", "c"];
- sf = sf.append(to_add, ["r1", "r2", "r3"]);
- assert.deepEqual(sf.values, expected_val);
- });
- });
- describe("or", function () {
- it("Return logical OR of series and other series (element-wise)", function () {
- const data1 = [true, true, true, false, false];
- const data2 = [true, false, true, true, false];
- const sf = new dfd.Series(data1);
- const sf2 = new dfd.Series(data2);
-
- const expected = [true, true, true, true, false];
- assert.deepEqual(sf.or(sf2).values, expected);
- });
-
- it("Return logical OR of series and other scalar", function () {
- const data1 = [true, true, true, false, false];
- const sf = new dfd.Series(data1);
-
- const expected = [true, true, true, true, true];
- assert.deepEqual(sf.or(true).values, expected);
- });
-
- it("Return logical OR of series and other array (element-wise)", function () {
- const data1 = [true, true, true, false, false];
- const data2 = [true, false, true, true, false];
- const sf = new dfd.Series(data1);
-
- const expected = [true, true, true, true, false];
- assert.deepEqual(sf.or(data2).values, expected);
- });
- it("Correct index is returned after operation", function () {
- const data1 = [true, true, true, false, false];
- const data2 = [true, false, true, true, false];
- const sf = new dfd.Series(data1, { index: ["one", "two", "three", "four", "five"] });
-
- const expected = ["one", "two", "three", "four", "five"];
- assert.deepEqual(sf.and(data2).index, expected);
- });
- });
-
- describe("and", function () {
- it("Return logical AND of series and other series (element-wise)", function () {
- const data1 = [true, true, true, false, false];
- const data2 = [true, false, true, true, false];
- const sf = new dfd.Series(data1);
- const sf2 = new dfd.Series(data2);
-
- const expected = [true, false, true, false, false];
- assert.deepEqual(sf.and(sf2).values, expected);
- });
-
- it("Return logical AND of series and other scalar", function () {
- const data1 = [true, true, true, false, false];
- const sf = new dfd.Series(data1);
-
- const expected = [true, true, true, false, false];
- assert.deepEqual(sf.and(true).values, expected);
- });
-
- it("Return logical AND of series and other array (element-wise)", function () {
- const data1 = [true, true, true, false, false];
- const data2 = [true, false, true, true, false];
- const sf = new dfd.Series(data1);
-
- const expected = [true, false, true, false, false];
- assert.deepEqual(sf.and(data2).values, expected);
- });
- it("Correct index is returned after operation", function () {
- const data1 = [true, true, true, false, false];
- const data2 = [true, false, true, true, false];
- const sf = new dfd.Series(data1, { index: ["one", "two", "three", "four", "five"] });
-
- const expected = ["one", "two", "three", "four", "five"];
- assert.deepEqual(sf.and(data2).index, expected);
- });
-
- it("Chaining works for logical AND of series and other array (element-wise)", function () {
- const data1 = [true, true, true, false, false];
- const data2 = [true, false, true, true, false];
- const data3 = [true, false, false, true, false];
-
- const sf = new dfd.Series(data1);
- const expected = [true, false, false, false, false];
- assert.deepEqual(sf.and(data2).and(data3).values, expected);
- });
-
- it("Chaining works for logical AND and OR combined", function () {
- const data1 = [true, true, true, false, false];
- const data2 = [true, false, true, true, false];
- const data3 = [true, false, false, true, false];
-
- const sf = new dfd.Series(data1);
- const expected = [true, false, true, true, false];
- assert.deepEqual(sf.and(data2).or(data3).values, expected);
- });
- });
-
-});
diff --git a/danfojs-browser/tests/core/utils.js b/danfojs-browser/tests/core/utils.js
deleted file mode 100644
index decb84d2..00000000
--- a/danfojs-browser/tests/core/utils.js
+++ /dev/null
@@ -1,160 +0,0 @@
-/* eslint-disable no-undef */
-
-describe("Utils Functions", function () {
- it("removes an element from an array", function () {
- let arr = [ 1, 2, 3, 4 ];
- assert.deepEqual(dfd.utils.removeElementFromArray(arr, 2), [ 1, 2, 4 ]);
- });
- it("Checks if variable is a string", function () {
- let arr = [ "1", "2" ];
- assert.isTrue(dfd.utils.isString(arr[0]));
- });
- it("Checks if variable is a number", function () {
- let arr = [ 1, 2, 3, 4 ];
- assert.isTrue(dfd.utils.isNumber(arr[0]));
- });
- it("Checks if value is null", function () {
- let val = null;
- let val2 = 1;
- assert.isTrue(dfd.utils.isNull(val));
- assert.isFalse(dfd.utils.isNull(val2));
- });
-
- it("Checks if value is undefined", function () {
- let arr;
- assert.isTrue(dfd.utils.isUndefined(arr));
- });
-
- it("Generate number betwee two set of values", function () {
-
- let start = 0;
- let end = 5;
- let data = [ 0, 1, 2, 3, 4, 5 ];
- assert.deepEqual(dfd.utils.range(start, end), data);
- });
-
- describe("inferDtype", function () {
- it("Returns string type present in an 1D array", function () {
- let data = [ 'Alice', 'Boy', 'Girl', "39" ];
- let result = [ 'string' ];
- assert.deepEqual(dfd.utils.inferDtype(data), result);
- });
- it("Returns float type present in an 1D array", function () {
- let data = [ 1.1, 2.1, 3.2, 4.4 ];
- let result = [ 'float32' ];
- assert.deepEqual(dfd.utils.inferDtype(data), result);
- });
- it("Returns int type present in an 1D array", function () {
- let data = [ 1, 2, 3, 45 ];
- let result = [ 'int32' ];
- assert.deepEqual(dfd.utils.inferDtype(data), result);
- });
- it("Returns float when there's a mixture of int and float in a 1D array", function () {
- let data = [ 1, 2.1, 3, 45 ];
- let result = [ 'float32' ];
- assert.deepEqual(dfd.utils.inferDtype(data), result);
- });
- it("Returns float type when NaN is present in an 1D array", function () {
- let data = [ 1, 2, 3, 45, NaN ];
- let result = [ 'float32' ];
- assert.deepEqual(dfd.utils.inferDtype(data), result);
- });
- });
-
- describe("__map_int_to_bool", function () {
- it("map ints to bools in array of arrays", function () {
- let data = [ [ 1, 0, 1 ], [ 1, 1, 0 ] ];
- assert.deepEqual(dfd.utils.mapIntegersToBooleans(data, 2), [ [ true, false, true ], [ true, true, false ] ]);
- });
- it("map ints to bools in array", function () {
- let data = [ 1, 0, 0, 1, 1 ];
- assert.deepEqual(dfd.utils.mapIntegersToBooleans(data, 1), [ true, false, false, true, true ]);
- });
- });
-
- describe("__round", function () {
- it("round elements in array to 1 dp", function () {
- let data = [ 10.01, 2.2, 3.11, 20.505, 20.22, 40.0909 ];
- assert.deepEqual(dfd.utils.round(data, 1, true), [ 10.0, 2.2, 3.1, 20.5, 20.2, 40.1 ]);
- });
- it("round elements in array to 2 dp", function () {
- let data = [ 10.019, 2.2099, 3.1145, 20.506, 20.22, 40.0909 ];
- assert.deepEqual(dfd.utils.round(data, 2, true), [ 10.02, 2.21, 3.11, 20.51, 20.22, 40.09 ]);
- });
- });
-
- describe("__replace_undefined_with_NaN", function () {
- it("replace undefined in Series with NaN", function () {
- let data = [ 10.01, 2.2, undefined, 20.505, 20.22, undefined ];
- assert.deepEqual(dfd.utils.replaceUndefinedWithNaN(data, true), [ 10.01, 2.2, NaN, 20.505, 20.22, NaN ]);
- });
- it("replace undefined in DataFrame with NaN", function () {
- let data = [ [ 10.01, 2.2, undefined, 20.505, 20.22, undefined ],
- [ 10.01, undefined, undefined, 20.505, 20, undefined ] ];
-
- let result = [ [ 10.01, 2.2, NaN, 20.505, 20.22, NaN ],
- [ 10.01, NaN, NaN, 20.505, 20, NaN ] ];
- assert.deepEqual(dfd.utils.replaceUndefinedWithNaN(data, false), result);
- });
- it("replace null in Series with NaN", function () {
- let data = [ 10.01, 2.2, null, 20.505, 20.22, null ];
- assert.deepEqual(dfd.utils.replaceUndefinedWithNaN(data, true), [ 10.01, 2.2, NaN, 20.505, 20.22, NaN ]);
- });
- it("replace null in DataFrame with NaN", function () {
- let data = [ [ 10.01, 2.2, null, 20.505, 20.22, null ],
- [ 10.01, null, null, 20.505, 20, null ] ];
-
- let result = [ [ 10.01, 2.2, NaN, 20.505, 20.22, NaN ],
- [ 10.01, NaN, NaN, 20.505, 20, NaN ] ];
- assert.deepEqual(dfd.utils.replaceUndefinedWithNaN(data, false), result);
- });
- });
-
- describe("__convert_2D_to_1D", function () {
- it("convert 2D array of array to 1D of string values", function () {
- let data = [ [ 10.01, 2.2, "a" ], [ 20.505, 20.22, "boy" ] ];
- assert.deepEqual(dfd.utils.convert2DArrayToSeriesArray(data), [ "10.01,2.2,a", "20.505,20.22,boy" ]);
- });
-
- });
-
- // describe("_throw_wrong_params_error", function () {
- // it("check if the right params are passed to a function", function () {
- // let params_needed = ["replace", "with", "inplace"]
- // let kwargs = { "replae": 2, "with": 12, "inplace": true }
- // assert.equal(dfd.utils._throw_wrong_params_error(kwargs, params_needed), false)
- // })
- // it("check if the right params are passed to a function 2", function () {
- // let params_needed = ["replace", "with", "inplace"]
- // let kwargs = { "replace": 2, "with": 12, "inplace": true }
- // assert.equal(dfd.utils._throw_wrong_params_error(kwargs, params_needed), true)
- // })
-
- // })
-
- describe("_get_row_and_col_values", function () {
- it("retreive rows and labels from column object", function () {
- let data = { "Alpha": [ "A", "B", "C", "D" ], count: [ 1, 2, 3, 4 ], sum: [ 20.3, 30.456, 40.90, 90.1 ] };
- let res = [ [ "A", 1, 20.3 ], [ "B", 2, 30.456 ], [ "C", 3, 40.90 ], [ "D", 4, 90.1 ] ];
- assert.deepEqual(dfd.utils.getRowAndColValues(data)[0], res);
- assert.deepEqual(dfd.utils.getRowAndColValues(data)[1], [ "Alpha", "count", "sum" ]);
-
- });
-
-
- });
-
-
- describe("_get_duplicate", function(){
- it("obtain duplicate and their index", function(){
-
- let data = [ 1, 2, 3, 4, 5, 3, 4, 6, 4, 5 ];
- let res = { '3': { count: 2, index: [ 2, 5 ] },
- '4': { count: 3, index: [ 3, 6, 8 ] },
- '5': { count: 2, index: [ 4, 9 ] } };
-
- assert.deepEqual(dfd.utils.getDuplicate(data), res);
- });
- });
-
-});
diff --git a/danfojs-browser/tests/io/reader.js b/danfojs-browser/tests/io/reader.js
deleted file mode 100644
index 9a0c484c..00000000
--- a/danfojs-browser/tests/io/reader.js
+++ /dev/null
@@ -1,57 +0,0 @@
-/* eslint-disable no-undef */
-
-describe("read_csv", async function () {
- this.timeout(100000); // all tests in this suite get 10 seconds before timeout
- it("reads a csv file from source over the internet", async function () {
- const csvUrl =
- "https://storage.googleapis.com/tfjs-examples/multivariate-linear-regression/data/boston-housing-train.csv";
-
- const df = await dfd.read_csv(csvUrl);
- const num_of_columns = df.columns.length;
- assert.equal(num_of_columns, 13);
-
- });
-});
-
-describe("read_json", async function () {
- this.timeout(100000); // all tests in this suite get 10 seconds before timeout
- it("reads a json file from source over the internet", async function () {
- const jUrl =
- "https://raw.githubusercontent.com/opensource9ja/danfojs/dev/danfojs-node/tests/samples/book.json";
-
- const df = await dfd.read_json(jUrl);
- assert.deepEqual(df.columns, [
- 'book_id',
- 'title',
- 'image_url',
- 'authors'
- ]);
- assert.deepEqual(df.dtypes, [
- 'int32', 'string',
- 'string', 'string'
- ]);
-
- });
-
-});
-
-describe("read_excel", async function () {
- this.timeout(100000); // all tests in this suite get 10 seconds before timeout
- it("reads an excel file from source over the internet", async function () {
- const remote_url =
- "https://raw.githubusercontent.com/opensource9ja/danfojs/dev/danfojs-node/tests/samples/SampleData.xlsx";
- const df = await dfd.read_excel(remote_url);
- assert.deepEqual(df.columns, [
- 'Year',
- 'Stocks',
- 'T.Bills',
- 'T.Bonds'
- ]);
- assert.deepEqual(df.dtypes, [
- 'int32', 'float32',
- 'float32', 'float32'
- ]);
- assert.deepEqual(df.shape, [ 82, 4 ]);
- });
-
-});
diff --git a/danfojs-browser/tests/preprocessing/encodings.js b/danfojs-browser/tests/preprocessing/encodings.js
deleted file mode 100644
index fff0015f..00000000
--- a/danfojs-browser/tests/preprocessing/encodings.js
+++ /dev/null
@@ -1,95 +0,0 @@
-/* eslint-disable no-undef */
-
-describe("Encodings", function () {
-
- describe("LabelEncoder", function () {
-
- it("test the label encoding on array", function () {
- let data = [ "dog", "cat", "man", "dog", "cat", "man", "man", "cat" ];
- let encode = new dfd.LabelEncoder();
- let fit_data = [
- 0, 1, 2, 0,
- 1, 2, 2, 1
- ];
- assert.deepEqual(encode.fit(data).values, fit_data);
- assert.deepEqual(encode.transform([ "dog", "man" ]).values, [ 0, 2 ]);
- });
- it("test the label encoding on Series", function () {
- let data = [ "dog", "cat", "man", "dog", "cat", "man", "man", "cat" ];
- let series = new dfd.Series(data);
- let encode = new dfd.LabelEncoder();
- let fit_data = [
- 0, 1, 2, 0,
- 1, 2, 2, 1
- ];
- assert.deepEqual(encode.fit(series).values, fit_data);
- assert.deepEqual(encode.transform([ "dog", "man" ]).values, [ 0, 2 ]);
- });
- it("label encoding directly from a Series", function () {
- let data = new dfd.Series([ "dog", "cat", "man", "dog", "cat", "man", "man", "cat" ]);
- let to_label_encode = new dfd.Series([ "dog", "man" ]);
- let encode = new dfd.LabelEncoder();
- let fit_data = [
- 0, 1, 2, 0,
- 1, 2, 2, 1
- ];
- assert.deepEqual(encode.fit(data).values, fit_data);
- assert.deepEqual(encode.transform(to_label_encode).values, [ 0, 2 ]);
- });
- // it("Label encoding on Series", function () {
- // let data = ["dog", "cat", "man", "dog", "cat", "man", "man", "cat"]
- // let series = new dfd.Series(data)
- // let encode = new dfd.LabelEncoder()
- // let fit_data = [
- // 0, 1, 2, 0,
- // 1, 2, 2, 1
- // ]
- // assert.deepEqual(encode.fit(series).values, fit_data)
- // assert.deepEqual(encode.transform(["dog", "man"]).values, [0, 2])
- // });
-
- });
-
- describe("OneHotEncoder", function () {
-
- it("test onehotencoding on array", function () {
- let data = [ "dog", "cat", "man", "dog", "cat", "man", "man", "cat" ];
- let encode = new dfd.OneHotEncoder();
- let fit_data = [
- [ 1, 0, 0 ],
- [ 0, 1, 0 ],
- [ 0, 0, 1 ],
- [ 1, 0, 0 ],
- [ 0, 1, 0 ],
- [ 0, 0, 1 ],
- [ 0, 0, 1 ],
- [ 0, 1, 0 ]
- ];
- let transform_data = [ [ 0, 0, 1 ], [ 0, 1, 0 ] ];
-
- assert.deepEqual(encode.fit(data).values, fit_data);
- assert.deepEqual(encode.transform([ "man", "cat" ]).values, transform_data);
- });
- it("test onehotencoding on Series", function () {
- let data = [ "dog", "cat", "man", "dog", "cat", "man", "man", "cat" ];
- let series = new dfd.Series(data);
- let encode = new dfd.OneHotEncoder();
- let fit_data = [
- [ 1, 0, 0 ],
- [ 0, 1, 0 ],
- [ 0, 0, 1 ],
- [ 1, 0, 0 ],
- [ 0, 1, 0 ],
- [ 0, 0, 1 ],
- [ 0, 0, 1 ],
- [ 0, 1, 0 ]
- ];
- let transform_data = [ [ 0, 0, 1 ], [ 0, 1, 0 ] ];
-
- assert.deepEqual(encode.fit(series).values, fit_data);
- assert.deepEqual(encode.transform(new dfd.Series([ "man", "cat" ])).values, transform_data);
- });
- });
-
-
-});
diff --git a/danfojs-browser/tests/preprocessing/scaler.js b/danfojs-browser/tests/preprocessing/scaler.js
deleted file mode 100644
index 2cb4d7bb..00000000
--- a/danfojs-browser/tests/preprocessing/scaler.js
+++ /dev/null
@@ -1,85 +0,0 @@
-/* eslint-disable no-undef */
-
-
-describe("Preprocessing", function(){
-
- describe("MinMaxscaler", function(){
-
- it("Standardize values in a DataFrame", function(){
- let data = [ [ -1, 2 ], [ -0.5, 6 ], [ 0, 10 ], [ 1, 18 ] ];
- let scaler = new dfd.MinMaxScaler();
- let fit_data = [ [ 0, 0 ], [ 0.25, 0.25 ], [ 0.5, 0.5 ], [ 1, 1 ] ];
- let transform_data = [ [ 1.5, 0. ] ];
- scaler.fit(new dfd.DataFrame(data));
- assert.deepEqual(scaler.transform(new dfd.DataFrame(data)).values, fit_data);
- assert.deepEqual(scaler.transform([ [ 2, 2 ] ]).values, transform_data);
- });
- it("Standardize values in a Series", function(){
- let data = [ -1, 2, -0.5, 60, 101, 18 ];
- let scaler = new dfd.MinMaxScaler();
- let result = [ 0, 0.029411764815449715, 0.0049019609577953815, 0.5980392098426819, 1, 0.18627451360225677 ];
- let transform_data = [ 0.029411764815449715, 0.029411764815449715 ];
- scaler.fit(new dfd.Series(data));
- assert.deepEqual(scaler.transform(new dfd.Series(data)).values, result);
- assert.deepEqual(scaler.transform([ 2, 2 ]).values, transform_data);
- });
- it("should be able to inverse the normalization of a Series", function() {
- let data = [ -1, 2, -0.5, 60, 101, 18 ];
- let result = [ 0, 0.029411764815449715, 0.0049019609577953815, 0.5980392098426819, 1, 0.18627451360225677 ];
- let scaler = new dfd.MinMaxScaler();
- scaler.fit(new dfd.Series(data));
- assert.deepEqual(scaler.inverse_transform(new dfd.Series(result)).values, data);
- });
- it("should be able to inverse the normalization of a DataFrame", function(){
- let data = [ [ -1, 2 ], [ -0.5, 6 ], [ 0, 10 ], [ 1, 18 ] ];
- let result = [ [ 0, 0 ], [ 0.25, 0.25 ], [ 0.5, 0.5 ], [ 1, 1 ] ];
- let scaler = new dfd.MinMaxScaler();
- scaler.fit(new dfd.DataFrame(data));
- assert.deepEqual(scaler.inverse_transform(new dfd.DataFrame(result)).values, data);
- });
- });
-
- describe("StandardScaler", function(){
-
- it("basic test", function(){
- let data = [ [ 0, 0 ], [ 0, 0 ], [ 1, 1 ], [ 1, 1 ] ];
-
- let scaler = new dfd.StandardScaler();
- let fit_data = [ [ -1, -1 ], [ -1, -1 ], [ 1, 1 ], [ 1, 1 ] ];
- let transform_data = [ [ 3, 3 ] ];
-
- assert.deepEqual(scaler.fit(new dfd.DataFrame(data)).round().values, fit_data);
- assert.deepEqual(scaler.transform([ [ 2, 2 ] ]).round().values, transform_data);
- });
- it("should be able to inverse the normalization of a Series", function() {
- let data = [ -1, 2, -0.5, 60, 101, 18 ];
- let result = [ 0, 0.029411764815449715, 0.0049019609577953815, 0.5980392098426819, 1, 0.18627451360225677 ];
- let scaler = new dfd.MinMaxScaler();
- scaler.fit(new dfd.Series(data));
- assert.deepEqual(scaler.inverse_transform(new dfd.Series(result)).values, data);
- });
- it("should be able to inverse the normalization of a DataFrame", function(){
- let data = [ [ -1, 2 ], [ -0.5, 6 ], [ 0, 10 ], [ 1, 18 ] ];
- let result = [ [ 0, 0 ], [ 0.25, 0.25 ], [ 0.5, 0.5 ], [ 1, 1 ] ];
- let scaler = new dfd.MinMaxScaler();
- scaler.fit(new dfd.DataFrame(data));
- assert.deepEqual(scaler.inverse_transform(new dfd.DataFrame(result)).values, data);
- });
- });
-
-
- // describe("RobustScaler", function(){
- // it("basic test", function(){
-
- // let data = [[3,1], [7,3], [8,4], [5,6], [12,5], [14,12], [21,23], [15,15], [18,2], [14,15]]
- // // let data2 = [[4,100,900],[5,110,800],[21,220,890],[20,300,500]]
- // let data2 = [0, 4, 4, 4, 7, 10, 11, 12, 14, 16, 17, 25]
-
-
- // let scaler = new RobustScaler()
-
- // // console.log(scaler.quantile(data2,true))
-
- // });
- // });
-});
diff --git a/danfojs-browser/types/core/concat.d.ts b/danfojs-browser/types/core/concat.d.ts
deleted file mode 100644
index 269f31e2..00000000
--- a/danfojs-browser/types/core/concat.d.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-export class Concat {
- constructor(kwargs?: any);
-}
-export function concat(kwargs?: any): DataFrame;
-import { DataFrame } from "./frame";
diff --git a/danfojs-browser/types/core/date_range.d.ts b/danfojs-browser/types/core/date_range.d.ts
deleted file mode 100644
index 58f7060a..00000000
--- a/danfojs-browser/types/core/date_range.d.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-/**
- * Generate date range between a specified set of date
- * @param {kwargs} kwargs {
- * start : string
- * end : string
- * period: int
- * freq : string
- * }
- * @returns Array
- */
-export class date_range {
- constructor(kwargs?: any);
- offset: number;
- start?: any;
- end?: any;
- period?: any;
- freq?: any;
- freq_list: string[];
- range(start?: any, end?: any, period?: any, offset?: any): any;
- freq_type(date?: any, ftype?: any): any;
- offset_count(d_array?: any, offset?: any): any[];
- set_dateProps(date?: any, ftype?: any, val?: any): Date;
- toLocalString(d_array?: any): any;
- month_end(start_date?: any, end_date?: any): any;
- month_range(range?: any): any;
- day_end(start_date?: any, end_date?: any): number;
-}
diff --git a/danfojs-browser/types/core/datetime.d.ts b/danfojs-browser/types/core/datetime.d.ts
deleted file mode 100644
index 77c6d23e..00000000
--- a/danfojs-browser/types/core/datetime.d.ts
+++ /dev/null
@@ -1,188 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import Series from "./series";
-import { ArrayType1D, DateTime } from "../shared/types";
-/**
- * Format and handle all datetime operations on Series or Array of date strings
- * @param data Series or Array of date strings
- */
-export default class TimeSeries implements DateTime {
- private $dateObjectArray;
- constructor(data: Series | ArrayType1D);
- /**
- * Processed the data values into internal structure for easy access
- * @param dateArray An array of date strings
- */
- private processData;
- /**
- * Returns the month, in local time.
- * @example
- * ```
- * import { Dataframe } from "danfojs-node"
- * const data = [
- * "2019-01-01",
- * "2019-02-01",
- * "2019-03-01",
- * "2019-04-01",
- * ]
- * const df = new Dataframe(data)
- * const dfNew = df.dt.month()
- * console.log(dfNew.values)
- * // [1, 2, 3, 4]
- * ```
- */
- month(): Series;
- /**
- * Returns the day of the week, in local time
- * @example
- * ```
- * import { Dataframe } from "danfojs-node"
- * const data = [
- * "2019-01-01",
- * "2019-02-01",
- * "2019-03-01",
- * "2019-04-01",
- * ]
- * const df = new Dataframe(data)
- * const dayOfWeek = df.dt.day()
- * console.log(day.values)
- * ```
- */
- day(): Series;
- /**
- * Returns the year, in local time
- * @example
- * ```
- * import { Dataframe } from "danfojs-node"
- * const data = [
- * "2019-01-01",
- * "2019-02-01",
- * "2021-03-01",
- * "2020-04-01",
- * ]
- * const df = new Dataframe(data)
- * const year = df.dt.year()
- * console.log(year.values)
- * // [2019, 2019, 2021, 2020]
- * ```
- */
- year(): Series;
- /**
- * Returns the name of the month, in local time
- * @example
- * ```
- * import { Dataframe } from "danfojs-node"
- * const data = [
- * "2019-01-01",
- * "2019-02-01",
- * "2021-03-01",
- * "2020-04-01",
- * ]
- * const df = new Dataframe(data)
- * const monthName = df.dt.month_name().values
- * console.log(monthName)
- * // ["January", "February", "March", "April"]
- * ```
- */
- month_name(): Series;
- /**
- * Returns the name of the day, of the week, in local time
- * @example
- * ```
- * import { Dataframe } from "danfojs-node"
- * const data = [
- * "2019-01-01",
- * "2019-02-01",
- * "2021-03-01",
- * "2020-04-01",
- * ]
- * const df = new Dataframe(data)
- * const dayOfWeekName = df.dt.weekdays().values
- * console.log(dayOfWeekName)
- * ```
- */
- weekdays(): Series;
- /**
- * Returns the day of the month, in local time
- * @example
- * ```
- * import { Dataframe } from "danfojs-node"
- * const data = [
- * "2019-01-01",
- * "2019-02-05",
- * "2021-03-02",
- * "2020-04-01",
- * ]
- * const df = new Dataframe(data)
- * const dayOfMonth = df.dt.monthday().values
- * console.log(dayOfMonth)
- * // [1, 5, 2, 1]
- * ```
- */
- monthday(): Series;
- /**
- * Returns the hour of the day, in local time
- * @example
- * ```
- * import { Dataframe } from "danfojs-node"
- * const data = [
- * "2019-01-01",
- * "2019-02-05",
- * "2021-03-02",
- * "2020-04-01",
- * ]
- * const df = new Dataframe(data)
- * const hour = df.dt.hour().values
- * console.log(hour)
- * // [0, 0, 0, 0]
- * ```
- */
- hours(): Series;
- /**
- * Returns the second of the day, in local time
- * @example
- * ```
- * import { Dataframe } from "danfojs-node"
- * const data = [
- * "2019-01-01",
- * "2019-02-05",
- * "2021-03-02",
- * "2020-04-01",
- * ]
- * const df = new Dataframe(data)
- * const second = df.dt.second().values
- * console.log(second)
- * ```
- */
- seconds(): Series;
- /**
- * Returns the minute of the day, in local time
- * @example
- * ```
- * import { Dataframe } from "danfojs-node"
- * const data = [
- * "2019-01-01",
- * "2019-02-05",
- * "2021-03-02",
- * "2020-04-01",
- * ]
- * const df = new Dataframe(data)
- * const minute = df.dt.minute().values
- * console.log(minute)
- * ```
- */
- minutes(): Series;
-}
-export declare const toDateTime: (data: Series | ArrayType1D) => TimeSeries;
diff --git a/danfojs-browser/types/core/frame.d.ts b/danfojs-browser/types/core/frame.d.ts
deleted file mode 100644
index d115dbd4..00000000
--- a/danfojs-browser/types/core/frame.d.ts
+++ /dev/null
@@ -1,555 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { ArrayType1D, ArrayType2D, DataFrameInterface, BaseDataOptionType } from "../shared/types";
-import NDframe from "./generic";
-import Series from './series';
-import { GroupBy } from "./groupby";
-
-
-/**
- * A 2D frame object that stores data in structured tabular format
- * @param {data} data, JSON, Array, 2D Tensor
- * @param {kwargs} Object {columns: Array of column names, defaults to ordered numbers when not specified
- * dtypes: strings of data types, automatically inferred when not specified
- * index: row index for subseting array, defaults to ordered numbers when not specified}
- *
- * @returns DataFrame
- */
-export class DataFrame extends NDframe implements DataFrameInterface {
- [key: string]: any;
- constructor(data: any, options?: BaseDataOptionType);
-
- /**
- * Drop columns or rows with missing values. Missing values are NaN, undefined or null.
- * @param options.columns Array of column names to drop
- * @param options.index Array of index to drop
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- drop(options?: {
- columns?: string | Array;
- index?: Array;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * 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)
- */
- loc({ rows, columns }: {
- rows?: Array | Series;
- columns?: Array;
- }): DataFrame;
- /**
- * 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.
- */
- iloc({ rows, columns }: {
- rows?: Array | Series;
- columns?: Array;
- }): DataFrame;
- /**
- * Prints the first n values in a dataframe
- * @param {rows} rows --> int
- * @returns DataFrame
- */
- head(rows?: number): DataFrame;
- /**
- * Prints the last n values in a dataframe
- * @param {rows} rows --> int
- * @returns DataFrame
- */
- tail(rows?: number): DataFrame;
- /**
- * 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.
- */
- sample(num?: number, options?: {
- seed?: number;
- }): Promise;
-
- /**
- * Return Addition of DataFrame and other, element-wise (binary operator add).
- * @param other DataFrame, Series, Array or Scalar number to add
- * @param options.axis 0 or 1. If 0, add column-wise, if 1, add row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- add(other: DataFrame | Series | number[] | number, options?: {
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Return substraction between DataFrame and other.
- * @param other DataFrame, Series, Array or Scalar number to substract from DataFrame
- * @param options.axis 0 or 1. If 0, compute the subtraction column-wise, if 1, row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- sub(other: DataFrame | Series | number[] | number, options?: {
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Return multiplciation between DataFrame and other.
- * @param other DataFrame, Series, Array or Scalar number to multiply with.
- * @param options.axis 0 or 1. If 0, compute the multiplication column-wise, if 1, row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- mul(other: DataFrame | Series | number[] | number, options?: {
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Return division of DataFrame with other.
- * @param other DataFrame, Series, Array or Scalar number to divide with.
- * @param options.axis 0 or 1. If 0, compute the division column-wise, if 1, row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- div(other: DataFrame | Series | number[] | number, options?: {
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Return DataFrame raised to the power of other.
- * @param other DataFrame, Series, Array or Scalar number to to raise to.
- * @param options.axis 0 or 1. If 0, compute the power column-wise, if 1, row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- pow(other: DataFrame | Series | number[] | number, options?: {
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Return modulus between DataFrame and other.
- * @param other DataFrame, Series, Array or Scalar number to modulus with.
- * @param options.axis 0 or 1. If 0, compute the mod column-wise, if 1, row-wise
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- mod(other: DataFrame | Series | number[] | number, options?: {
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * 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
- */
- mean(options?: {
- axis?: 0 | 1;
- }): Series;
- /**
- * 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
- */
- median(options?: {
- axis?: 0 | 1;
- }): Series;
- /**
- * 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
- */
- mode(options?: {
- axis?: 0 | 1;
- keep?: number;
- }): Series;
- /**
- * 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
- */
- min(options?: {
- axis?: 0 | 1;
- }): Series;
- /**
- * 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
- */
- max(options?: {
- axis?: 0 | 1;
- }): Series;
- /**
- * 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
- */
- std(options?: {
- axis?: 0 | 1;
- }): Series;
- /**
- * 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
- */
- var(options?: {
- axis?: 0 | 1;
- }): Series;
- /**
- * 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
- */
- lt(other: DataFrame | Series | number | Array, options?: {
- axis?: 0 | 1;
- }): DataFrame;
- /**
- * 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
- */
- gt(other: DataFrame | Series | number | Array, options?: {
- axis?: 0 | 1;
- }): DataFrame;
- /**
- * 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
- */
- eq(other: DataFrame | Series | number | Array, options?: {
- axis?: 0 | 1;
- }): DataFrame;
- /**
- * 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
- */
- ne(other: DataFrame | Series | number | Array, options?: {
- axis?: 0 | 1;
- }): DataFrame;
- /**
- * 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
- */
- le(other: DataFrame | Series | number | Array, options?: {
- axis?: 0 | 1;
- }): DataFrame;
- /**
- * 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
- */
- ge(other: DataFrame | Series | number | Array, options?: {
- axis?: 0 | 1;
- }): DataFrame;
- /**
- * 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
- */
- count(options?: {
- axis?: 0 | 1;
- }): Series;
- /**
- * 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
- */
- sum(options?: {
- axis?: 0 | 1;
- }): Series;
- /**
- * Return the absolute value of elements in a DataFrame.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- abs(options?: {
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Rounds all element in the DataFrame to specified number of decimal places.
- * @param dp Number of decimal places to round to. Defaults to 1
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- round(dp?: number, options?: {
- inplace: boolean;
- }): DataFrame | void;
- /**
- * Returns cumulative product accross specified axis.
- * @param options.axis 0 or 1. If 0, count column-wise, if 1, add row-wise. Defaults to 1
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- cumprod(options?: {
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Returns cumulative sum accross specified axis.
- * @param options.axis 0 or 1. If 0, count column-wise, if 1, add row-wise. Defaults to 1
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- cumsum(options?: {
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Returns cumulative minimum accross specified axis.
- * @param options.axis 0 or 1. If 0, count column-wise, if 1, add row-wise. Defaults to 1
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- cummin(options?: {
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Returns cumulative maximum accross specified axis.
- * @param options.axis 0 or 1. If 0, count column-wise, if 1, add row-wise. Defaults to 1
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- cummax(options?: {
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
-
- /**
- * Generate descriptive statistics for all numeric columns
- * Descriptive statistics include those that summarize the central tendency,
- * dispersion and shape of a dataset’s distribution, excluding NaN values.
- * @returns {Series}
- */
- describe(): DataFrame;
- /**
- * Drops all rows or columns with missing values (NaN)
- * @param axis 0 or 1. If 0, drop columns with NaNs, if 1, drop rows with NaNs
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- dropna(axis?: 0 | 1, options?: {
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Adds a new column to the DataFrame. If column exists, then the column values is replaced.
- * @param column The name of the column to add or replace.
- * @param values An array of values to be inserted into the DataFrame. Must be the same length as the columns
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- addColumn(options: {
- column: string,
- values: Series | ArrayType1D,
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Makes a new copy of a DataFrame
- */
- copy(): DataFrame;
- /**
- * Return a boolean same-sized object indicating where elements are empty (NaN, undefined, null).
- * NaN, undefined and null values gets mapped to true, and everything else gets mapped to false.
- */
- isna(): DataFrame;
- /**
- * Replace all empty elements with a specified value. Replace params expect columns array to map to values array.
- * @param columns The list of column names to be replaced
- * @param options.values The list of values to use for replacement.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- fillna(values: number | string | boolean | ArrayType1D, options?: {
- columns?: Array;
- inplace?: boolean;
- }): DataFrame | void;
-
- /**
- * Sorts a Dataframe by a specified column values
- * @param options.column Column name to sort by
- * @param options.ascending Whether to sort values in ascending order or not. Defaults to true
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- sort_values(options?: {
- by: string,
- ascending?: boolean;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Sets the index of the DataFrame to the specified index.
- * @param options.index An array of index values to set
- * @param options.column A column name to set the index to
- * @param options.drop Whether to drop the column whose index was set. Defaults to false
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- set_index(options: {
- index?: Array;
- column?: string;
- drop?: boolean;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Resets the index of the DataFrame to the default index.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- reset_index(options?: {
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Apply a function along an axis of the DataFrame. To apply a function element-wise, use `applyMap`.
- * Objects passed to the function are Series values whose
- * index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1)
- * @param callable Function to apply to each column or row
- * @param options.axis 0 or 1. If 0, compute the power column-wise, if 1, row-wise
- */
- apply(callable: any, options?: {
- axis?: 0 | 1;
- }): DataFrame | Series;
- /**
- * Apply a function to a Dataframe values element-wise.
- * @param callable Function to apply to each column or row
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- apply_map(callable: any, options?: {
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Returns the specified column data as a Series object.
- * @param column The name of the column to return
- */
- column(column: string): Series;
- /**
- * Return a subset of the DataFrame’s columns based on the column dtypes.
- * @param include An array of dtypes or strings to be included.
- */
- select_dtypes(include: Array): DataFrame;
- /**
- * Returns the transposes the DataFrame.
- **/
- transpose(options?: {
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Returns the Transpose of the DataFrame. Similar to `transpose`, but does not change the original DataFrame.
- **/
- get T(): DataFrame;
- /**
- * Replace all occurence of a value with a new value
- * @param oldValue The value you want to replace
- * @param newValue The new value you want to replace the old value with
- * @param options.columns An array of column names you want to replace. If not provided replace accross all columns.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- replace(oldValue: number | string | boolean, newValue: number | string | boolean, options?: {
- columns?: Array;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Cast the values of a column to specified data type
- * @param column The name of the column to cast
- * @param dtype Data type to cast to. One of [float32, int32, string, boolean]
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- astype(options?: {
- column: string,
- dtype: "float32" | "int32" | "string" | "boolean",
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Return the number of unique elements in a across the specified axis.
- * To get the values use `.unique()` instead.
- * @param axis The axis to count unique elements across. Defaults to 1
- */
- nunique(axis?: 0 | 1): Series;
- /**
- * Renames a column or index.
- * @param mapper An object that maps each column or index in the DataFrame to a new value
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- * @param options.axis The axis to perform the operation on. Defaults to 1
- */
- rename(options?: {
- mapper: any,
- axis?: 0 | 1;
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Sorts the Dataframe by the index.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- * @param options.ascending Whether to sort values in ascending order or not. Defaults to true
- */
- sort_index(options?: {
- inplace?: boolean;
- ascending?: boolean;
- }): DataFrame | void;
- /**
- * Add new rows to the end of the DataFrame.
- * @param newValues Array, Series or DataFrame to append to the DataFrame
- * @param index The new index value(s) to append to the Series. Must contain the same number of values as `newValues`
- * as they map `1 - 1`.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- append(newValues: ArrayType1D | ArrayType2D | Series | DataFrame, index: Array | number | string, options?: {
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Queries the DataFrame for rows that meet the boolean criteria.
- * @param condition An array of boolean mask, one for each row in the DataFrame. Rows where the value are true will be returned.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- **/
- query(options: {
- column?: string,
- is?: string,
- to?: any,
- condition?: Series | Array,
- inplace?: boolean;
- }): DataFrame | void;
- /**
- * Returns the data types for each column as a Series.
- */
- get ctypes(): Series;
- /**
- * One-hot encode specified columns in the DataFrame. If columns are not specified, all columns of dtype string will be encoded.
- * @param options Options for the operation. The following options are available:
- * - `columns`: A single column name or an array of column names to encode. Defaults to all columns of dtype string.
- * - `prefix`: Prefix to add to the column names. Defaults to unique labels.
- * - `prefixSeparator`: Separator to use for the prefix. Defaults to '_'.
- * - `inplace`: Boolean indicating whether to perform the operation inplace or not. Defaults to false
- * @returns A DataFrame with the one-hot encoded columns.
- * @example
- * df.getDummies({ columns: ['a', 'b'] })
- * df.getDummies({ columns: ['a', 'b'], prefix: 'cat' })
- * df.getDummies({ columns: ['a', 'b'], prefix: 'cat', prefixSeparator: '-' })
- * df.getDummies({ columns: ['a', 'b'], prefix: 'cat', prefixSeparator: '-', inplace: true })
- * df.getDummies({ columns: ['a', 'b'], prefix: ['col1', 'col2'], prefixSeparator: '-', inplace: true })
- */
- get_dummies(options?: {
- columns?: string | Array;
- prefix?: string | Array;
- prefixSeparator?: string | Array;
- inplace?: boolean;
- }): DataFrame | void;
-
- groupby(column: Array): GroupBy;
-}
\ No newline at end of file
diff --git a/danfojs-browser/types/core/generic.d.ts b/danfojs-browser/types/core/generic.d.ts
deleted file mode 100644
index 26a40cf5..00000000
--- a/danfojs-browser/types/core/generic.d.ts
+++ /dev/null
@@ -1,188 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import Configs from "../shared/config";
-import {
- NDframeInterface,
- NdframeInputDataType,
- AxisType,
- ArrayType1D,
- ArrayType2D,
- CsvOutputOptionsBrowser
-} from '../shared/types';
-/**
- * N-Dimension data structure. Stores multi-dimensional
- * data in a size-mutable, labeled data structure. Analogous to the Python Pandas DataFrame.
- *
- * @param Object
- *
- * data: 1D or 2D Array, JSON, Tensor, Block of data.
- *
- * index: Array of numeric or string names for subseting array. If not specified, indexes are auto generated.
- *
- * columns: Array of column names. If not specified, column names are auto generated.
- *
- * dtypes: Array of data types for each the column. If not specified, dtypes inferred.
- *
- * config: General configuration object for NDframe
- *
- * @returns NDframe
- */
-export default class NDframe implements NDframeInterface {
- $isSeries: boolean;
- protected $data: any;
- protected $dataIncolumnFormat: ArrayType1D | ArrayType2D;
- protected $index: Array;
- protected $columns: string[];
- protected $dtypes: Array;
- protected $config: Configs;
- constructor({ data, index, columns, dtypes, config, isSeries }: NdframeInputDataType);
- /**
- * Internal function to load array of data into NDFrame
- * @param data The array of data to load into NDFrame
- * @param index Array of numeric or string names for subsetting array.
- * @param columns Array of column names.
- * @param dtypes Array of data types for each the column.
- */
- private loadArrayIntoNdframe;
- /**
- * Internal function to format and load a Javascript object or object of arrays into NDFrame.
- * @param data Object or object of arrays.
- * @param type The type of the object. There are two recognized types:
- *
- * - type 1 object are in JSON format `[{a: 1, b: 2}, {a: 30, b: 20}]`.
- *
- * - type 2 object are of the form `{a: [1,2,3,4], b: [30,20, 30, 20}]}`
- * @param index Array of numeric or string names for subsetting array.
- * @param columns Array of column names.
- * @param dtypes Array of data types for each the column.
- */
- private loadObjectIntoNdframe;
- /**
- * Converts and returns the data in the NDframe as a Tensorflow.js Tensor.
- */
- get tensor(): import("@tensorflow/tfjs-node").Tensor1D | import("@tensorflow/tfjs-node").Tensor2D;
- /**
- * Returns the dtypes of the columns
- */
- get dtypes(): Array;
- /**
- * Internal function to set the Dtypes of the NDFrame from an array. This function
- * performs the necessary checks.
- */
- $setDtypes(dtypes: Array | undefined): void;
- /**
- * Returns the dimension of the data. Series have a dimension of 1,
- * while DataFrames have a dimension of 2.
- */
- get ndim(): number;
- /**
- * Returns the axis labels of the NDFrame.
- */
- get axis(): AxisType;
- /**
- * Returns the configuration object of the NDFrame.
- */
- get config(): Configs;
- /**
- * Internal function to set the configuration of the ndframe
- */
- $setConfig(config: Configs): void;
- /**
- * Returns the indices of the NDFrame
- */
- get index(): Array;
- /**
- * Internal function to set the index of the NDFrame with the specified
- * array of indices. Performs all necessary checks to ensure that the
- * index is valid.
- */
- $setIndex(index: Array | undefined): void;
- /**
- * Internal function to reset the index of the NDFrame using a range of indices.
- */
- $resetIndex(): void;
- /**
- * Returns the column names of the NDFrame
- */
- get columns(): string[];
- /**
- * Internal function to set the column names for the NDFrame. This function
- * performs a check to ensure that the column names are unique, and same length as the
- * number of columns in the data.
- */
- $setColumnNames(columns?: string[]): void;
- /**
- * Returns the shape of the NDFrame. Shape is determined by [row lenght, column length]
- */
- get shape(): Array;
- /**
- * Returns the underlying data in Array format.
- */
- get values(): ArrayType1D | ArrayType2D;
- /**
- * Updates the internal $data property to the specified value
- * @param values An array of values to set
- * @param checkLength Whether to check the length of the new values and the existing row length
- * @param checkColumnLength Whether to check the length of the new values and the existing column length
- * */
- $setValues(values: ArrayType1D | ArrayType2D, checkLength?: boolean, checkColumnLength?: boolean): void;
- /**
- * Returns the size of the NDFrame object
- *
- */
- get size(): number;
- /**
- * Converts a DataFrame or Series to CSV.
- * @param options Configuration object. Supports the following options:
- * - `fileName`: Local file path to write the CSV file. If not specified, the CSV will be returned as a string.
- * - `header`: Boolean indicating whether to include a header row in the CSV file.
- * - `sep`: Character to be used as a separator in the CSV file.
- */
- to_csv(options?: CsvOutputOptionsBrowser): string | void;
- /**
- * Converts a DataFrame or Series to JSON.
- * @param options Configuration object. Supported options:
- * - `fileName`: The file path to write the JSON to. If not specified, the JSON object is returned.
- * - `format`: The format of the JSON. Defaults to `'column'`. E.g for using `column` format:
- * ```
- * [{ "a": 1, "b": 2, "c": 3, "d": 4 },
- * { "a": 5, "b": 6, "c": 7, "d": 8 }]
- * ```
- * and `row` format:
- * ```
- * { "a": [1, 5, 9],
- * "b": [2, 6, 10]
- * }
- * ```
- */
- to_json(options?: {
- format?: "row" | "column";
- fileName?: string;
- }): object | void;
- /**
- * Converts a DataFrame or Series to Excel Sheet.
- * @param options Configuration object. Supported options:
- * - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
- * - `fileName`: The fileName to be written to. Defaults to `'./output.xlsx'`.
- */
- to_excel(options?: {
- fileName?: string;
- sheetName?: string;
- }): void;
- /**
- * Pretty prints a DataFrame or Series to the console
- */
- print(): void;
-}
diff --git a/danfojs-browser/types/core/get_dummies.d.ts b/danfojs-browser/types/core/get_dummies.d.ts
deleted file mode 100644
index b4daef13..00000000
--- a/danfojs-browser/types/core/get_dummies.d.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { DataFrame } from "./frame";
-import Series from "./series";
-/**
- * Generate one-hot encoding for categorical columns in an Array, Series or Dataframe.
- * @param data Series or Dataframe
- * @param columns Columns to encode
- * @param prefix Prefix for the new columns
- * @param prefixSeparator Separator for the prefix and the column name
- * @returns Encoded Dataframe
- * @example
- * import { DataFrame, DummyEncoder }from 'danfojs';
- * const df = new DataFrame([[1,2,3], [4,5,6]], { columns: ['a', 'b', 'c'] });
- * const df2 = new DummyEncoder({data: df, columns: ['a', 'b'], prefix: 'enc', prefixSeparator: '#'}).encode();
- * df2.print();
- */
-declare function dummyEncode(data: Series | DataFrame, options?: {
- columns?: string | Array;
- prefix?: string | Array;
- prefixSeparator?: string | Array;
-}): DataFrame;
-export default dummyEncode;
diff --git a/danfojs-browser/types/core/groupby.d.ts b/danfojs-browser/types/core/groupby.d.ts
deleted file mode 100644
index c8231934..00000000
--- a/danfojs-browser/types/core/groupby.d.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-/**
- * The class performs all groupby operation on a dataframe
- * involveing all aggregate funciton
- * @param {col_dict} col_dict Object of unique keys in the group by column
- * @param {key_col} key_col Array contains the column names
- * @param {data} Array the dataframe data
- * @param {column_name} Array of all column name in the dataframe.
- */
-export class GroupBy {
- constructor(col_dict?: any, key_col?: any, data?: any, column_name?: any);
- key_col?: any;
- col_dict?: any;
- data?: any;
- column_name?: any;
- data_tensors: {};
- /**
- * Group the dataframe by the column by
- * creating an object to store the grouping
- * @returns Groupby data structure
- */
- group(): GroupBy;
- /**
- * obtain the column for each group
- * @param {col_name} col_name [Array]--> array of column names
- * @return Groupby data structure
- */
- col(col_names?: any): GroupBy;
- group_col_name?: any[];
- group_col: {};
- /**
- * Basic root of all column arithemetic in groups
- * @param {operation} operatioin String
- */
- arithemetic(operation?: any): {};
- count(): DataFrame;
- sum(): DataFrame;
- std(): DataFrame;
- var(): DataFrame;
- mean(): DataFrame;
- cumsum(): DataFrame;
- cummax(): DataFrame;
- cumprod(): DataFrame;
- cummin(): DataFrame;
- max(): DataFrame;
- min(): DataFrame;
- /**
- * returns dataframe of a group
- * @param {*} key [Array]
- */
- get_groups(key?: any): any;
- /**
- * Map every column to an operaton
- * @param {kwargs} kwargs {column name: math operation}
- * @example .agg({"A": "mean","B": "sum","C":"count"})
- */
- agg(kwargs?: any): DataFrame;
- to_DataFrame(key_col?: any, col?: any, data?: any, ops?: any): DataFrame;
-}
-import { DataFrame } from "./frame";
diff --git a/danfojs-browser/types/core/indexing.d.ts b/danfojs-browser/types/core/indexing.d.ts
deleted file mode 100644
index c89625e7..00000000
--- a/danfojs-browser/types/core/indexing.d.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import Series from "./series";
-import { DataFrame } from "./frame";
-import { NDframeInterface } from "../shared/types";
-/**
-* Slice a Series/DataFrame by specified string location based labels
-* @param Object
-*/
-export declare function _iloc({ ndFrame, rows, columns }: {
- ndFrame: NDframeInterface;
- rows?: Array | Series;
- columns?: Array;
-}): Series | DataFrame;
-/**
-* Slice a Series/DataFrame by specified string location based labels
-* @param Object
-*/
-export declare function _loc({ ndFrame, rows, columns }: {
- ndFrame: NDframeInterface;
- rows?: Array | Series;
- columns?: Array;
-}): Series | DataFrame | void;
diff --git a/danfojs-browser/types/core/math.ops.d.ts b/danfojs-browser/types/core/math.ops.d.ts
deleted file mode 100644
index dff9a543..00000000
--- a/danfojs-browser/types/core/math.ops.d.ts
+++ /dev/null
@@ -1,30 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import Series from "./series";
-/**
- * Generic function for performing add, sub, pow, mul and mod operation on a series
- * @param object
- *
- * ndframe ==> The current Series
- *
- * other ==> The Series or number to perform math operation with
- *
- * operation ==> The type of operation to perform
-*/
-export declare function _genericMathOp({ ndFrame, other, operation }: {
- ndFrame: Series;
- other: Series | number | Array;
- operation: string;
-}): number[] | undefined;
diff --git a/danfojs-browser/types/core/merge.d.ts b/danfojs-browser/types/core/merge.d.ts
deleted file mode 100644
index 8162a6c9..00000000
--- a/danfojs-browser/types/core/merge.d.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-export class Merge {
- constructor(kwargs?: any);
- how?: any;
- left: DataFrame;
- right: DataFrame;
- on?: any[];
- left_col_index?: any[];
- right_col_index?: any[];
- left_key_dict: {};
- right_key_dict: {};
- __create_columns(): void;
- outer(): any[];
- inner(): any[];
- left_merge(): any[];
- right_merge(): any[];
- basic(keys?: any): any[];
-}
-export function merge(kwargs?: any): DataFrame;
-import { DataFrame } from "./frame";
diff --git a/danfojs-browser/types/core/series.d.ts b/danfojs-browser/types/core/series.d.ts
deleted file mode 100644
index 440360df..00000000
--- a/danfojs-browser/types/core/series.d.ts
+++ /dev/null
@@ -1,462 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { ArrayType1D, BaseDataOptionType, SeriesInterface } from "../shared/types";
-import NDframe from "./generic";
-import Str from './strings';
-import Dt from './datetime';
-import { DataFrame } from "./frame";
-/**
- * One-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.
- */
-export default class Series extends NDframe implements SeriesInterface {
- constructor(data?: any, options?: BaseDataOptionType);
- /**
- * 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
- *
- * 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.
- */
- iloc(rows: Array): Series;
- /**
- * Access a group of rows 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
- *
- * 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)
- */
- loc(rows: Array): Series;
- /**
- * Returns the first n values in a Series
- * @param rows The number of rows to return
- */
- head(rows?: number): Series;
- /**
- * Returns the last n values in a Series
- * @param rows The number of rows to return
- */
- tail(rows?: number): Series;
- /**
- * Returns specified number of random rows in a Series
- * @param num The number of rows to return
- * @param options.seed An integer specifying the random seed that will be used to create the distribution.
- */
- sample(num?: number, options?: {
- seed?: number;
- }): Promise;
- /**
- * Return Addition of series and other, element-wise (binary operator add).
- * Equivalent to series + other
- * @param other Series, Array of same length or scalar number to add
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- add(other: Series | Array | number, options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Returns the subtraction between a series and other, element-wise (binary operator subtraction).
- * Equivalent to series - other
- * @param other Number to subtract
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- sub(other: Series | number | Array, options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Return Multiplication of series and other, element-wise (binary operator mul).
- * Equivalent to series * other
- * @param other Number to multiply with.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- mul(other: Series | number | Array, options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Return division of series and other, element-wise (binary operator div).
- * Equivalent to series / other
- * @param other Series or number to divide with.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- div(other: Series | number | Array, options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Return Exponential power of series and other, element-wise (binary operator pow).
- * Equivalent to series ** other
- * @param other Number to multiply with.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- pow(other: Series | number | Array, options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Return Modulo of series and other, element-wise (binary operator mod).
- * Equivalent to series % other
- * @param other Number to modulo with
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- mod(other: Series | number | Array, options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Checks if the array value passed has a compatible dtype, removes NaN values, and if
- * boolean values are present, converts them to integer values.
- * */
- private $checkAndCleanValues;
- /**
- * Returns the mean of elements in Series
- */
- mean(): number;
- /**
- * Returns the median of elements in Series
- */
- median(): number;
- /**
- * Returns the modal value of elements in Series
- */
- mode(): any;
- /**
- * Returns the minimum value in a Series
- */
- min(): number;
- /**
- * Returns the maximum value in a Series
- * @returns {Number}
- */
- max(): number;
- /**
- * Return the sum of the values in a series.
- */
- sum(): number;
- /**
- * Return number of non-null elements in a Series
- */
- count(): number;
- /**
- * Return maximum of series and other.
- * @param other Series or number to check against
- */
- maximum(other: Series | number | Array): Series;
- /**
- * Return minimum of series and other.
- * @param other Series, Numbers to check against
- */
- minimum(other: Series | number | Array): Series;
- /**
- * Round each value in a Series to the specified number of decimals.
- * @param dp Number of Decimal places to round to
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- round(dp?: number, options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Return sample standard deviation of elements in Series
- */
- std(): number;
- /**
- * Return unbiased variance of elements in a Series.
- */
- var(): number;
- /**
- * Return a boolean same-sized object indicating where elements are NaN.
- * NaN and undefined values gets mapped to true, and everything else gets mapped to false.
- */
- isna(): Series;
- /**
- * Replace all NaN with a specified value
- * @param value The value to replace NaN with
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- fillna(options?: {
- value: number | string | boolean,
- inplace?: boolean;
- }): Series | void;
- /**
- * Sort a Series in ascending or descending order by some criterion.
- * @param options Method options
- * @param ascending Whether to return sorted values in ascending order or not. Defaults to true
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- sort_values(options?: {
- ascending?: boolean,
- inplace?: boolean;
- }): Series | void;
- /**
- * Makes a deep copy of a Series
- */
- copy(): Series;
- /**
- * Generate descriptive statistics.
- * Descriptive statistics include those that summarize the central tendency,
- * dispersion and shape of a dataset’s distribution, excluding NaN values.
- */
- describe(): Series;
- /**
- * Returns Series with the index reset.
- * This is useful when index is meaningless and needs to be reset to the default before another operation.
- */
- reset_index(options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Set the Series index (row labels) using an array of the same length.
- * @param index Array of new index values,
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- set_index(options?: {
- index: Array,
- inplace?: boolean;
- }): Series | void;
- /**
- * map all the element in a column to a variable or function
- * @param callable callable can either be a funtion or an object
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- map(callable: any, options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Applies a function to each element of a Series
- * @param callable Function to apply to each element of the series
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- apply(callable: any, options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Returns a Series with only the unique value(s) in the original Series
- */
- unique(): Series;
- /**
- * Return the number of unique elements in a Series
- */
- nunique(): number;
- /**
- * Returns unique values and their counts in a Series
- */
- value_counts(): Series;
- /**
- * Returns the absolute values in Series
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- abs(options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Returns the cumulative sum over a Series
- */
- cumsum(options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Returns cumulative minimum over a Series
- */
- cummin(options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Returns cumulative maximum over a Series
- */
- cummax(options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Returns cumulative product over a Series
- */
- cumprod(options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * perform cumulative operation on series data
- */
- private cumOps;
- /**
- * Returns less than of series and other. Supports element wise operations
- * @param other Series or number to compare against
- */
- lt(other: Series | number): Series;
- /**
- * Returns Greater than of series and other. Supports element wise operations
- * @param {other} Series, Scalar
- * @return {Series}
- */
- gt(other: Series | number | Array): Series;
- /**
- * Returns Less than or Equal to of series and other. Supports element wise operations
- * @param {other} Series, Scalar
- * @return {Series}
- */
- le(other: Series | number | Array): Series;
- /**
- * Returns Greater than or Equal to of series and other. Supports element wise operations
- * @param {other} Series, Scalar
- * @return {Series}
- */
- ge(other: Series | number | Array): Series;
- /**
- * Returns Not Equal to of series and other. Supports element wise operations
- * @param {other} Series, Scalar
- * @return {Series}
- */
- ne(other: Series | number | Array): Series;
- /**
- * Returns Equal to of series and other. Supports element wise operations
- * @param {other} Series, Scalar
- * @return {Series}
- */
- eq(other: Series | number | Array): Series;
- /**
- * Perform boolean operations on bool values
- * @param other Other Series or number to compare with
- * @param bOps Name of operation to perform [ne, ge, le, gt, lt, eq]
- */
- private boolOps;
- /**
- * Replace all occurence of a value with a new value
- * @param oldValue The value you want to replace
- * @param newValue The new value you want to replace the old value with
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- replace(options?: {
- oldValue: string | number | boolean,
- newValue: string | number | boolean,
- inplace?: boolean;
- }): Series | void;
- /**
- * Drops all missing values (NaN) from a Series.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- dropna(options?: {
- inplace?: boolean;
- }): Series | undefined;
- /**
- * Return the integer indices that would sort the Series.
- * @param ascending boolean true: will sort the Series in ascending order, false: will sort in descending order
- */
- argsort(ascending?: boolean): Series;
- /**
- * Return int position of the largest value in the Series.
- */
- argmax(): number;
- /**
- * Return int position of the smallest value in the Series.
- */
- argmin(): number;
- /**
- * Remove duplicate values from a Series
- * @param keep "first" | "last", which dupliate value to keep. Defaults to "first".
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- drop_duplicates(options?: {
- keep?: "first" | "last";
- inplace?: boolean;
- }): Series | void;
- /**
- * Cast Series to specified data type
- * @param dtype Data type to cast to. One of [float32, int32, string, boolean, undefined]
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- astype(dtype: "float32" | "int32" | "string" | "boolean" | "undefined", options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Add a new value or values to the end of a Series
- * @param newValues Single value | Array | Series to append to the Series
- * @param index The new index value(s) to append to the Series. Must contain the same number of values as `newValues`
- * as they map `1 - 1`.
- * @param options.inplace Boolean indicating whether to perform the operation inplace or not. Defaults to false
- */
- append(newValue: string | number | boolean | Series | ArrayType1D, index: Array | number | string, options?: {
- inplace?: boolean;
- }): Series | void;
- /**
- * Returns dtype of Series
- */
- get dtype(): string;
- /**
- * Exposes numerous string methods to manipulate Series of type string
- */
- get str(): Str;
- /**
- * Returns time class that exposes different date time method
- */
- get dt(): Dt;
- /**
- * Prints Series to console as a grid of row and columns.
- */
- toString(): string;
- /**
- * Returns the logical AND between Series and other. Supports element wise operations and broadcasting.
- * @param other Series, Scalar, Array of Scalars
- */
- and(other: any): Series;
- /**
- * Returns the logical OR between Series and other. Supports element wise operations and broadcasting.
- * @param other Series, Scalar, Array of Scalars
- */
- or(other: any): Series;
- /**
- * One-hot encode values in the Series.
- * @param options Options for the operation. The following options are available:
- * - `prefix`: Prefix to add to the new column. Defaults to unique labels.
- * - `prefixSeparator`: Separator to use for the prefix. Defaults to '_'.
- * @returns A DataFrame with the one-hot encoded columns.
- * @example
- * sf.get_dummies()
- * sf.get_dummies({prefix: 'cat' })
- * sf.get_dummies({ prefix: 'cat', prefixSeparator: '-' })
- */
- get_dummies(options?: {
- columns?: string | Array;
- prefix?: string | Array;
- prefixSeparator?: string;
- }): DataFrame;
-}
diff --git a/danfojs-browser/types/core/strings.d.ts b/danfojs-browser/types/core/strings.d.ts
deleted file mode 100644
index 24b31577..00000000
--- a/danfojs-browser/types/core/strings.d.ts
+++ /dev/null
@@ -1,341 +0,0 @@
-import Series from "./series";
-/**
- * Exposes numerous String methods. All methods are applied Element-wise
- */
-export default class Str {
- private series;
- private values;
- constructor(series: Series);
- /**
- * Converts all characters to lowercase.
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["GooD", "Bad", "CrAzy"])
- * const newSf = sf.str.toLowerCase()
- * console.log(newSf.values)
- * // ["good", "bad", "crazy"]
- * ```
- */
- toLowerCase(options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Converts all characters to uppercase.
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["GooD", "Bad", "CrAzy"])
- * const newSf = sf.str.toUpperCase()
- * console.log(newSf.values)
- * // ["GOOD", "BAD", "CRAZY"]
- * ```
- */
- toUpperCase(options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Capitalize first string
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.capitalize()
- * console.log(newSf.values)
- * // ["Good", "Bad", "Crazy"]
- * ```
- */
- capitalize(options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Returns the character at the specified index (position)
- * @param index position of character
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.charAt(1)
- * console.log(newSf.values)
- * // ["o", "a", "r"]
- * ```
- */
- charAt(index?: number, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Joins specified `other` with values in the Series.
- * @param other string|values to concatenate with.
- * @param position where to concat the string from. O concats from the start, 1 concats from the end. Defaults to 1.
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.concat("_new")
- * console.log(newSf.values)
- * // ["Good_new", "bad_new", "crazy_new"
- * ```
- */
- concat(other: Array | string, position?: number, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Checks whether a string begins with specified characters
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.startsWith("G")
- * console.log(newSf.values)
- * // [true, false, false]
- * ```
- */
- startsWith(str?: string, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Checks whether a string ends with specified characters
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.endsWith("d")
- * console.log(newSf.values)
- * // [true, true, false]
- * ```
- */
- endsWith(str?: string, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Checks whether a string contains the specified string/characters
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.includes("d")
- * console.log(newSf.values)
- * // [true, true, false]
- * ```
- */
- includes(str?: string, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Returns the position of the first occurrence of a specified value in a string.
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "bad", "crazy"])
- * const newSf = sf.str.indexOf("d")
- * console.log(newSf.values)
- * // [3, 2, -1]
- * ```
- */
- indexOf(str?: string, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Returns the position of the last found occurrence of a specified value in a string
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "crazy"])
- * const newSf = sf.str.lastIndexOf("d")
- * console.log(newSf.values)
- * // [3, 2, -1]
- * ```
- */
- lastIndexOf(str?: string, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced
- * @param searchValue String | Character value to replace
- * @param replaceValue String | Character string to replace with
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "crazy"])
- * const newSf = sf.str.replace("d", 7)
- * console.log(newSf.values)
- * // ["Goo7", "o77", "crazy"]
- * ```
- */
- replace(searchValue?: string, replaceValue?: string, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Returns a new string with a specified number of copies of an existing string
- * @param num Number of times to repeat
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "crazy"])
- * const newSf = sf.str.replace(2)
- * console.log(newSf.values)
- * // ["GoodGood", "oddodd", "crazycrazy"]
- * ```
- */
- repeat(num?: number, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Searches a string for a specified value, or regular expression, and returns the position of the match
- * @param str String or Character to check against
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "crazy"])
- * const newSf = sf.str.search("d")
- * console.log(newSf.values)
- * ```
- */
- search(str?: string, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Extracts a part of a string and returns a new string
- * @param startIndex index position of start character
- * @param endIndex index position of last character
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "crazy"])
- * const newSf = sf.str.slice(0,1)
- * console.log(newSf.values)
- * // ["G", "o", "c"]
- * ```
- */
- slice(startIndex?: number, endIndex?: number, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Splits a string into an values of substrings
- * @param splitVal string or character to split at
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "grade"])
- * const newSf = sf.str.split(d)
- * console.log(newSf.values)
- * ```
- */
- split(splitVal?: string, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Extracts the characters from a string, beginning at a specified start position, and through the specified number of character
- * @param startIndex index position of start character
- * @param num number of characters to return
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "grade"])
- * const newSf = sf.str.substr(d)
- * ```
- */
- substr(startIndex?: number, num?: number, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Extracts the characters from a string, between two specified indices
- * @param startIndex index position of start character
- * @param endIndex index position of last character
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "grade"])
- * const newSf = sf.str.substring(d)
- * ```
- */
- substring(startIndex?: number, endIndex?: number, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Removes whitespace from both ends of a string
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series([" Good", "odd ", " grade "])
- * const newSf = sf.str.trim(d)
- * ["Good", "odd", "grade"]
- * ```
- */
- trim(options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Joins strings to specified value
- * @param valToJoin string value to join to the values
- * @param joinChar Character to Join with
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "grade"])
- * const newSf = sf.str.join("new", "_")
- * // ["Good_new", "odd_new", "grade_new"]
- * ```
- */
- join(valToJoin?: string, joinChar?: string, options?: {
- inplace: boolean;
- }): Series | void;
- /**
- * Counts the number of characters in string
- * @param options The following optional parameters are supported:
- * - `inplace` Boolean, indicating whether to perform the operation inplace or not. Defaults to `false`
- * @example
- * ```
- * import { Series } from "danfojs-node"
- * const sf = new Series(["Good", "odd", "grade"])
- * const newSf = sf.str.len(d)
- * // [4,3,5]
- * ```
- */
- len(options?: {
- inplace: boolean;
- }): Series | void;
-}
diff --git a/danfojs-browser/types/index.d.ts b/danfojs-browser/types/index.d.ts
deleted file mode 100644
index c254dda1..00000000
--- a/danfojs-browser/types/index.d.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import NDframe from "./core/generic";
-import Utils from './shared/utils';
-import Series from "./core/series";
-import { DataFrame } from "./core/frame";
-import { concat } from "./core/concat";
-import { merge } from "./core/merge";
-import { LabelEncoder, OneHotEncoder } from "./preprocessing/encodings";
-import { MinMaxScaler, StandardScaler } from "./preprocessing/scalers";
-import { date_range } from "./core/date_range";
-import get_dummies from "./core/get_dummies";
-import Str from "./core/strings";
-import Dt, { toDateTime } from "./core/datetime";
-import {
- readCSV as read_csv,
- toCSV as to_csv,
- readJSON as read_json,
- toJSON as to_json,
- readExcel as read_excel,
- toExcel as to_excel
-} from "./io";
-
-
-export {
- date_range,
- toDateTime,
- concat,
- merge,
- NDframe,
- Utils,
- Str,
- Dt,
- Series,
- DataFrame,
- read_csv,
- to_csv,
- read_json,
- to_json,
- read_excel,
- to_excel,
- MinMaxScaler,
- StandardScaler,
- LabelEncoder,
- OneHotEncoder,
- get_dummies
-};
diff --git a/danfojs-browser/types/io/index.d.ts b/danfojs-browser/types/io/index.d.ts
deleted file mode 100644
index 880a7061..00000000
--- a/danfojs-browser/types/io/index.d.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { $readCSV, $toCSV} from "./io.csv";
-import { $readJSON, $toJSON } from "./io.json";
-import { $readExcel, $toExcel } from "./io.excel";
-
-export {
- $readCSV as readCSV,
- $toCSV as toCSV,
- $readJSON as readJSON,
- $toJSON as toJSON,
- $readExcel as readExcel,
- $toExcel as toExcel
- };
-
\ No newline at end of file
diff --git a/danfojs-browser/types/io/io.csv.d.ts b/danfojs-browser/types/io/io.csv.d.ts
deleted file mode 100644
index 1485484b..00000000
--- a/danfojs-browser/types/io/io.csv.d.ts
+++ /dev/null
@@ -1,60 +0,0 @@
-///
-import { DataFrame, NDframe, Series } from '../index';
-import { CsvInputOptions, CsvOutputOptionsBrowser } from "../shared/types";
-/**
- * Reads a CSV file from local or remote location into a DataFrame.
- * @param filePath URL or local file path to CSV file. `readCSV` uses PapaParse to parse the CSV file,
- * hence all PapaParse options are supported.
- * @param options Configuration object. Supports all Papaparse parse config options.
- * @returns DataFrame containing the parsed CSV file.
- * @example
- * ```
- * import { readCSV } from "danfojs-node"
- * const df = await readCSV("https://raw.githubusercontent.com/test.csv")
- * ```
- * @example
- * ```
- * import { readCSV } from "danfojs-node"
- * const df = await readCSV("https://raw.githubusercontent.com/test.csv", {
- * delimiter: ",",
- * headers: {
- * Accept: "text/csv",
- * Authorization: "Bearer YWRtaW46YWRtaW4="
- * }
- * })
- * ```
- * @example
- * ```
- * import { readCSV } from "danfojs-node"
- * const df = await readCSV("./data/sample.csv")
- * ```
- */
-declare const $readCSV: (filePath: string, options: CsvInputOptions) => Promise;
-
-/**
- * Converts a DataFrame or Series to CSV.
- * @param df DataFrame or Series to be converted to CSV.
- * @param options Configuration object. Supports the following options:
- * - `filePath`: Local file path to write the CSV file. If not specified, the CSV will be returned as a string.
- * - `header`: Boolean indicating whether to include a header row in the CSV file.
- * - `sep`: Character to be used as a separator in the CSV file.
- * @example
- * ```
- * import { toCSV } from "danfojs-node"
- * const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
- * const csv = toCSV(df)
- * ```
- * @example
- * ```
- * import { toCSV } from "danfojs-node"
- * const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
- * toCSV(df, {
- * filePath: "./data/sample.csv",
- * header: true,
- * sep: "+"
- * })
- * ```
- */
-declare const $toCSV: (df: NDframe | DataFrame | Series, options?: CsvOutputOptionsBrowser | undefined) => string | void;
-
-export { $readCSV, $streamCSV, $toCSV };
diff --git a/danfojs-browser/types/io/io.excel.d.ts b/danfojs-browser/types/io/io.excel.d.ts
deleted file mode 100644
index b91d6c90..00000000
--- a/danfojs-browser/types/io/io.excel.d.ts
+++ /dev/null
@@ -1,70 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { HeadersInit } from "node-fetch";
-import { DataFrame, NDframe, Series } from '../index';
-/**
- * Reads a JSON file from local or remote location into a DataFrame.
- * @param filePath URL or local file path to JSON file.
- * @param options Configuration object. Supported options:
- * - `method`: The HTTP method to use. Defaults to `'GET'`.
- * - `headers`: Additional headers to send with the request. Supports the `node-fetch` [HeadersInit]
- * @example
- * ```
- * import { readExcel } from "danfojs-node"
- * const df = await readExcel("https://raw.githubusercontent.com/test.xlsx")
- * ```
- * @example
- * ```
- * import { readExcel } from "danfojs-node"
- * const df = await readExcel("https://raw.githubusercontent.com/test.xlsx", {
- * method: "GET",
- * headers: {
- * Accept: "text/csv",
- * Authorization: "Bearer YWRtaW46YWRtaW4="
- * }
- * })
- * ```
- * @example
- * ```
- * import { readExcel } from "danfojs-node"
- * const df = await readExcel("./data/sample.xlsx")
- * ```
- */
-declare const $readExcel: (filePath: string, options?: {
- sheet?: number | undefined;
- method?: string | undefined;
- headers?: HeadersInit | undefined;
-}) => Promise;
-/**
- * Converts a DataFrame or Series to Excel Sheet.
- * @param df DataFrame or Series to be converted to JSON.
- * @param options Configuration object. Supported options:
- * - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
- * - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`.
- * @example
- * ```
- * import { toExcel } from "danfojs-node"
- * const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
- * toExcel(df, {
- * filePath: "./data/sample.xlsx",
- * sheetName: "MySheet",
- * })
- * ```
- */
-declare const $toExcel: (df: NDframe | DataFrame | Series, options?: {
- filePath?: string | undefined;
- sheetName?: string | undefined;
-} | undefined) => void;
-export { $readExcel, $toExcel };
diff --git a/danfojs-browser/types/io/io.json.d.ts b/danfojs-browser/types/io/io.json.d.ts
deleted file mode 100644
index 49d0ae3f..00000000
--- a/danfojs-browser/types/io/io.json.d.ts
+++ /dev/null
@@ -1,72 +0,0 @@
-import { HeadersInit } from "node-fetch";
-import { DataFrame, NDframe, Series } from '../index';
-/**
- * Reads a JSON file from local or remote location into a DataFrame.
- * @param filePath URL or local file path to JSON file.
- * @param options Configuration object. Supported options:
- * - `method`: The HTTP method to use. Defaults to `'GET'`.
- * - `headers`: Additional headers to send with the request. Supports the `node-fetch` [HeadersInit]
- * @example
- * ```
- * import { readJSON } from "danfojs-node"
- * const df = await readJSON("https://raw.githubusercontent.com/test.json")
- * ```
- * @example
- * ```
- * import { readJSON } from "danfojs-node"
- * const df = await readJSON("https://raw.githubusercontent.com/test.json", {
- * headers: {
- * Accept: "text/json",
- * Authorization: "Bearer YWRtaW46YWRtaW4="
- * }
- * })
- * ```
- * @example
- * ```
- * import { readJSON } from "danfojs-node"
- * const df = await readJSON("./data/sample.json")
- * ```
- */
-declare const $readJSON: (filePath: string, options?: {
- method?: string | undefined;
- headers?: HeadersInit | undefined;
-}) => Promise;
-
-/**
- * Converts a DataFrame or Series to JSON.
- * @param df DataFrame or Series to be converted to JSON.
- * @param options Configuration object. Supported options:
- * - `filePath`: The file path to write the JSON to. If not specified, the JSON object is returned.
- * - `format`: The format of the JSON. Defaults to `'column'`. E.g for using `column` format:
- * ```
- * [{ "a": 1, "b": 2, "c": 3, "d": 4 },
- * { "a": 5, "b": 6, "c": 7, "d": 8 }]
- * ```
- * and `row` format:
- * ```
- * { "a": [1, 5, 9],
- * "b": [2, 6, 10]
- * }
- * ```
- * @example
- * ```
- * import { toJSON } from "danfojs-node"
- * const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
- * const json = toJSON(df)
- * ```
- * @example
- * ```
- * import { toJSON } from "danfojs-node"
- * const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
- * toJSON(df, {
- * filePath: "./data/sample.json",
- * format: "row"
- * })
- * ```
- */
-declare const $toJSON: (df: NDframe | DataFrame | Series, options?: {
- format?: "row" | "column" | undefined;
- fileName?: string | undefined;
- download?: boolean | undefined;
-} | undefined) => object | void;
-export { $readJSON, $toJSON };
diff --git a/danfojs-browser/types/plotting/plot.d.ts b/danfojs-browser/types/plotting/plot.d.ts
deleted file mode 100644
index de3963e9..00000000
--- a/danfojs-browser/types/plotting/plot.d.ts
+++ /dev/null
@@ -1,66 +0,0 @@
-/**
- * Plotting methods and Functions performed on Series and DataFrames
- */
-export class Plot {
- constructor(ndframe?: any, div?: any);
- div?: any;
- ndframe?: any;
- /**
- * Plot Series or DataFrame as lines. This function is useful to plot lines using DataFrame’s values as coordinates.
- * Make plots of Series or DataFrame.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- line(config?: any): void;
- /**
- * Plot Series or DataFrame as Bars.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- bar(config?: any): void;
- /**
- * Plot two or more columns in a DataFrame as scatter points. If Series, plot against its index
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- scatter(config?: any): void;
- /**
- * Plot columns in a Series/DataFrame as Histograms.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- hist(config?: any): void;
- /**
- * Makes Pie Plots from two Columns in a DataFrame.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {string} div Name of the div to show the plot
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- pie(config?: any): void;
- /**
- * Plot Box plots from Series or DataFrame as lines.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- box(config?: any): void;
- /**
- * Plot Violin plots from Series or DataFrame as lines.
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- violin(config?: any): void;
- /**
- * Display DataFrame in a div using D3.js format
- * Uses the Plotly as backend, so supoorts Plotly's configuration parameters
- * @param {Object} config configuration options for making Plots, supports Plotly parameters
- */
- table(config?: any): void;
- __get_plot_params(config?: any): (string[] | {
- layout: {};
- })[];
- ____check_if_cols_exist(cols?: any): any;
-}
diff --git a/danfojs-browser/types/preprocessing/encodings.d.ts b/danfojs-browser/types/preprocessing/encodings.d.ts
deleted file mode 100644
index 77cb5771..00000000
--- a/danfojs-browser/types/preprocessing/encodings.d.ts
+++ /dev/null
@@ -1,23 +0,0 @@
-import Series from "../core/series";
-import { DataFrame } from "../core/frame";
-
-export class LabelEncoder {
- /**
- *
- * @param {data} data [Array|Series]
- * @returns Array.
- */
- fit(data?: any): Series;
- label?: any;
- /**
- * Transform data using the label generated from fitting
- * @param {data} data [Array|Series]
- * @returns Array
- */
- transform(data?: any): Series;
-}
-export class OneHotEncoder {
- fit(data?: any): DataFrame;
- label?: any;
- transform(data?: any): DataFrame;
-}
diff --git a/danfojs-browser/types/preprocessing/scalers.d.ts b/danfojs-browser/types/preprocessing/scalers.d.ts
deleted file mode 100644
index 73e97a7f..00000000
--- a/danfojs-browser/types/preprocessing/scalers.d.ts
+++ /dev/null
@@ -1,39 +0,0 @@
-import Series from "../core/series";
-import { DataFrame } from "../core/frame";
-export class MinMaxScaler {
- /**
- * Fit minmax scaler on data, to obtain their min and max value
- * @param {data} data [DataRame | Series | Array]
- * @returns Array
- */
- fit(data?: any): Series | DataFrame;
- max?: any;
- min?: any;
- /**
- * Transform an array using the min and max generated from the fitting on data
- * @param {data} data [Array]
- * @returns array
- */
- transform(data?: any): Series | DataFrame;
-
- /**
- * Restore a transformed array to their original values,
- * using the min and max generated from the fitting on data
- * @param {Series|Array|DataFrame} data
- * @returns Series|DataFrame
- */
- inverse_transform(data?: any): Series | DataFrame;
-}
-export class StandardScaler {
- /**
- *
- * @param {data} data [DataRame | Series | Array]
- * @returns Array
- */
- fit(data?: any): Series | DataFrame;
- std?: any;
- mean?: any;
- transform(data?: any): Series | DataFrame;
- inverse_transform(data?: any): Series | DataFrame;
-}
-
diff --git a/danfojs-browser/types/shared/config.d.ts b/danfojs-browser/types/shared/config.d.ts
deleted file mode 100644
index 3aac789b..00000000
--- a/danfojs-browser/types/shared/config.d.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { BaseUserConfig, TableUserConfig } from 'table';
-import { ConfigsType } from './types';
-/**
- * Package wide configuration class
- */
-export default class Configs {
- tableDisplayConfig: BaseUserConfig & TableUserConfig;
- tableMaxRow: number;
- tableMaxColInConsole: number;
- dtypeTestLim: number;
- lowMemoryMode: boolean;
- constructor(options: ConfigsType);
- setTableDisplayConfig(config: BaseUserConfig & TableUserConfig): void;
- get getTableDisplayConfig(): BaseUserConfig & TableUserConfig;
- setTableMaxColInConsole(val: number): void;
- get getTableMaxColInConsole(): number;
- setMaxRow(val: number): void;
- get getMaxRow(): number;
- get getDtypeTestLim(): number;
- setDtypeTestLim(val: number): void;
- get isLowMemoryMode(): boolean;
- setIsLowMemoryMode(val: boolean): void;
-}
diff --git a/danfojs-browser/types/shared/errors.d.ts b/danfojs-browser/types/shared/errors.d.ts
deleted file mode 100644
index d7bd325c..00000000
--- a/danfojs-browser/types/shared/errors.d.ts
+++ /dev/null
@@ -1,37 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { DataFrame } from "../core/frame";
-import NDframe from "../core/generic";
-/**
- * Package wide error throwing class
- */
-declare class ErrorThrower {
- throwColumnNamesLengthError: (ndframe: NDframe, columns: Array) => void;
- throwIndexLengthError: (ndframe: NDframe, index: Array) => void;
- throwIndexDuplicateError: () => void;
- throwColumnDuplicateError: () => void;
- throwDtypesLengthError: (ndframe: NDframe, dtypes: Array) => void;
- throwDtypeNotSupportedError: (dtype: string) => void;
- throwColumnLengthError: (ndframe: NDframe | DataFrame, arrLen: number) => void;
- throwRowLengthError: (ndframe: NDframe, arrLen: number) => void;
- throwColumnNotFoundError: (ndframe: DataFrame | NDframe) => void;
- throwNotImplementedError: () => void;
- throwIlocRowIndexError: () => void;
- throwIlocColumnsIndexError: () => void;
- throwStringDtypeOperationError: (operation: string) => void;
- throwSeriesMathOpLengthError: (ndframe: NDframe, other: NDframe) => void;
-}
-declare const _default: ErrorThrower;
-export default _default;
diff --git a/danfojs-browser/types/shared/types.d.ts b/danfojs-browser/types/shared/types.d.ts
deleted file mode 100644
index 407430be..00000000
--- a/danfojs-browser/types/shared/types.d.ts
+++ /dev/null
@@ -1,423 +0,0 @@
-/**
-* @license
-* Copyright 2021, 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.
-* ==========================================================================
-*/
-import { DataFrame } from '../core/frame';
-import { Tensor } from '@tensorflow/tfjs-node';
-import Series from '../core/series';
-import { BaseUserConfig, TableUserConfig } from "table";
-import Str from '../core/strings';
-import Dt from '../core/datetime';
-import { ParseConfig } from 'papaparse';
-import { GroupBy } from '../core/groupby';
-import { Plot } from "../plotting/plot";
-
-export declare type DTYPES = "float32" | "int32" | "string" | "boolean" | "undefined";
-export declare type ArrayType2D = Array;
-export declare type ArrayType1D = Array;
-export declare type ConfigsType = {
- tableDisplayConfig?: BaseUserConfig & TableUserConfig;
- tableMaxRow?: number;
- tableMaxColInConsole?: number;
- dtypeTestLim?: number;
- lowMemoryMode?: boolean;
- tfInstance?: any;
-};
-export interface BaseDataOptionType {
- type?: number;
- index?: Array;
- columns?: string[];
- dtypes?: Array;
- config?: ConfigsType;
-}
-export interface NdframeInputDataType {
- data: any;
- type?: number;
- index?: Array;
- columns?: string[];
- dtypes?: Array;
- config?: ConfigsType;
- isSeries: boolean;
-}
-export interface LoadArrayDataType {
- data: ArrayType1D | ArrayType2D;
- index?: Array;
- columns?: string[];
- dtypes?: Array;
-}
-export interface LoadObjectDataType {
- data: object | Array