Skip to content
175 changes: 0 additions & 175 deletions src/danfojs-base/core/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
* limitations under the License.
* ==========================================================================
*/
import { toCSVBrowser, toExcelBrowser, toJSONBrowser } from "../io/browser";
import { toCSVNode, toExcelNode, toJSONNode } from "../io/node";
import dummyEncode from "../transformers/encoders/dummy.encoder";
import { variance, std, median, mode, mean } from 'mathjs';
import tensorflow from '../shared/tensorflowlib'
Expand All @@ -22,7 +20,6 @@ import { _genericMathOp } from "./math.ops";
import Groupby from '../aggregators/groupby';
import ErrorThrower from "../shared/errors"
import { _iloc, _loc } from "./indexing";
import { PlotlyLib } from "../plotting";
import Utils from "../shared/utils"
import NDframe from "./generic";
import { table } from "table";
Expand All @@ -32,12 +29,6 @@ import {
ArrayType2D,
DataFrameInterface,
BaseDataOptionType,
CsvOutputOptionsBrowser,
ExcelOutputOptionsBrowser,
JsonOutputOptionsBrowser,
CsvOutputOptionsNode,
ExcelOutputOptionsNode,
JsonOutputOptionsNode
} from "../shared/types";

const utils = new Utils();
Expand Down Expand Up @@ -3368,172 +3359,6 @@ export default class DataFrame extends NDframe implements DataFrameInterface {

}

/**
* Exposes functions for creating charts from a DataFrame.
* Charts are created using the Plotly.js library, so all Plotly's configuration parameters are available.
* @param divId name of the HTML Div to render the chart in.
*/
plot(divId: string) {
//TODO: Add support for check plot library to use. So we can support other plot library like d3, vega, etc
if (utils.isBrowserEnv()) {
const plt = new PlotlyLib(this, divId);
return plt;
} else {
throw new Error("Not supported in NodeJS");
}
}

/**
* Converts a DataFrame 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. Option is only available in NodeJS.
* - `fileName`: Name of the CSV file. Defaults to `data.csv`. Option is only available in Browser.
* - `download`: If true, the CSV will be downloaded. Defaults to false. Option is only available in Browser.
* - `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
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* const csv = df.toCSV()
* console.log(csv)
* //output
* "A","B"
* 1,2
* 3,4
* ```
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* const csv = df.toCSV({ header: false })
* console.log(csv)
* //output
* 1,2
* 3,4
* ```
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* const csv = df.toCSV({ sep: ';' })
* console.log(csv)
* //output
* "A";"B"
* 1;2
* 3;4
* ```
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* df.toCSV({ filePath: './data.csv' }) //write to local file in NodeJS
* ```
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* df.toCSV({ fileName: 'data.csv', download: true }) //Downloads file in Browser
* ```
*
*/
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string | void {
if (utils.isBrowserEnv()) {
return toCSVBrowser(this, options as CsvOutputOptionsBrowser)
} else {
return toCSVNode(this, options as CsvOutputOptionsNode)
}
}

/**
* Converts a DataFrame 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. Option is only available in NodeJS.
* - `fileName`: The name of the JSON file. Defaults to `data.json`. Option is only available in Browser.
* - `download`: If true, the JSON will be downloaded. Defaults to false. Option is only available in Browser.
* - `format`: The format of the JSON. Supported values are `'column'` and `'row'`. Defaults to `'column'`.
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* const json = df.toJSON()
* ```
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* const json = df.toJSON({ format: 'row' })
* console.log(json)
* //output
* [{"A":1,"B":2},{"A":3,"B":4}]
* ```
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* const json = df.toJSON({ format: "column" })
* console.log(json)
* //output
* {"A":[1,3],"B":[2,4]}
* ```
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* df.toJSON({ filePath: './data.json' }) // downloads to local file system as data.json in NodeJS
* ```
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* df.toJSON({ fileName: 'data.json', download: true }) // downloads file browser
* ```
*/
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object | void {
if (utils.isBrowserEnv()) {
return toJSONBrowser(this, options as JsonOutputOptionsBrowser)
} else {
return toJSONNode(this, options as JsonOutputOptionsNode)
}
}


/**
* Converts a DataFrame to Excel file format.
* @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'`. Option is only available in NodeJs
* - `fileName`: The fileName to be written to. Defaults to `'output.xlsx'`. Option is only available in Browser
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* df.toExcel({ filePath: './output.xlsx' }) // writes to local file system as output.xlsx in NodeJS
* ```
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* df.toExcel({ fileName: 'output.xlsx', download: true }) // downloads file browser
* ```
*
* @example
* ```
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
* df.toExcel({ sheetName: 'Sheet2' }) // writes to Sheet2 in Excel
* ```
*
*/
toExcel(options?: ExcelOutputOptionsBrowser | ExcelOutputOptionsNode): void {
if (utils.isBrowserEnv()) {
toExcelBrowser(this, options as ExcelOutputOptionsBrowser)
} else {
return toExcelNode(this, options as ExcelOutputOptionsNode)
}
}

/**
* Access a single value for a row/column pair by integer position.
* Similar to {@link iloc}, in that both provide integer-based lookups.
Expand Down
141 changes: 1 addition & 140 deletions src/danfojs-base/core/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,13 @@
* limitations under the License.
* ==========================================================================
*/
import { toCSVBrowser, toExcelBrowser, toJSONBrowser } from "../io/browser";
import { toCSVNode, toExcelNode, toJSONNode } from "../io/node";
import dummyEncode from "../transformers/encoders/dummy.encoder";
import { variance, std, median, mode } from 'mathjs';
import tensorflow from '../shared/tensorflowlib'
import { DATA_TYPES } from '../shared/defaults'
import { _genericMathOp } from "./math.ops";
import ErrorThrower from "../shared/errors"
import { _iloc, _loc } from "./indexing";
import { PlotlyLib } from "../plotting";
import Utils from "../shared/utils"
import NDframe from "./generic";
import { table } from "table";
Expand All @@ -32,12 +29,6 @@ import {
ArrayType1D,
BaseDataOptionType,
SeriesInterface,
CsvOutputOptionsBrowser,
ExcelOutputOptionsBrowser,
JsonOutputOptionsBrowser,
CsvOutputOptionsNode,
ExcelOutputOptionsNode,
JsonOutputOptionsNode,
mapParam
} from "../shared/types";

Expand Down Expand Up @@ -194,7 +185,7 @@ export default class Series extends NDframe implements SeriesInterface {
if (this.shape[0] - rows < 0) {
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
}

const startIdx = this.shape[0] - rows
return this.iloc([`${startIdx}:`])
}
Expand Down Expand Up @@ -2137,136 +2128,6 @@ export default class Series extends NDframe implements SeriesInterface {
return dummyEncode(this, options)
}


/**
* Exposes functions for creating charts from a Series.
* Charts are created using the Plotly.js library, so all Plotly's configuration parameters are available.
* @param divId name of the HTML Div to render the chart in.
* @example
* ```
* const sf = new Series([1, 2, 3, 4, 5]);
* sf.plot("myDiv").line() //renders the chart in the div with id "myDiv"
* ```
*/
plot(divId: string) {
//TODO: Add support for check plot library to use
// So we can support other plot library like d3, vega, etc
if (utils.isBrowserEnv()) {
const plt = new PlotlyLib(this, divId);
return plt;
} else {
throw new Error("Not supported in NodeJS");
}
}

/**
* Converts a 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. Option is only available in NodeJS.
* - `fileName`: Name of the CSV file. Defaults to `data.csv`. Option is only available in Browser.
* - `download`: If true, the CSV will be downloaded. Defaults to false. Option is only available in Browser.
*
* @example
* ```
* const df = new Series([1, 2, 3, 4])
* const csv = df.toCSV()
* console.log(csv)
* //output "1,2,3,4"
* ```
*
* @example
* ```
* const df = new Series([1, 2, 3, 4])
* df.toCSV({ filePath: './data.csv' }) //write to local file in NodeJS
* ```
*
* @example
* ```
* const df = new Series([1, 2, 3, 4])
* df.toCSV({ fileName: 'data.csv', download: true }) //Downloads file in Browser
* ```
*
*/
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string
toCSV(options?: CsvOutputOptionsBrowser | CsvOutputOptionsNode): string | void {
if (utils.isBrowserEnv()) {
return toCSVBrowser(this, options as CsvOutputOptionsBrowser)
} else {
return toCSVNode(this, options as CsvOutputOptionsNode)
}
}

/**
* Converts a Series 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. Option is only available in NodeJS.
* - `fileName`: The name of the JSON file. Defaults to `data.json`. Option is only available in Browser.
* - `download`: If true, the JSON will be downloaded. Defaults to false. Option is only available in Browser.
*
* @example
* ```
* const df = new Series([[1, 2, 3, 4]], { columns: ['A']})
* const json = df.toJSON()
* console.log(json)
* //output { A: [ '1,2,3,4' ] }
* ```
*
* @example
* ```
* const df = new Series([1, 2, 3, 4])
* df.toJSON({ filePath: './data.json' }) // downloads to local file system as data.json in NodeJS
* ```
*
* @example
* ```
* const df = new Series([1, 2, 3, 4])
* df.toJSON({ fileName: 'data.json', download: true }) // downloads file browser
* ```
*/
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object
toJSON(options?: JsonOutputOptionsBrowser | JsonOutputOptionsNode): object | void {
if (utils.isBrowserEnv()) {
return toJSONBrowser(this, options as JsonOutputOptionsBrowser)
} else {
return toJSONNode(this, options as JsonOutputOptionsNode)
}
}


/**
* Converts a Series to Excel file format.
* @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'`. Option is only available in NodeJs
* - `fileName`: The fileName to be written to. Defaults to `'output.xlsx'`. Option is only available in Browser
*
* @example
* ```
* const df = new Series([1, 2, 3, 4])
* df.toExcel({ filePath: './output.xlsx' }) // writes to local file system as output.xlsx in NodeJS
* ```
*
* @example
* ```
* const df = new Series([1, 2, 3, 4])
* df.toExcel({ fileName: 'output.xlsx', download: true }) // downloads file browser
* ```
*
* @example
* ```
* const df = new Series([1, 2, 3, 4])
* df.toExcel({ sheetName: 'Sheet2' }) // writes to Sheet2 in Excel
* ```
*
*/
toExcel(options?: ExcelOutputOptionsBrowser | ExcelOutputOptionsNode): void {
if (utils.isBrowserEnv()) {
toExcelBrowser(this, options as ExcelOutputOptionsBrowser)
} else {
return toExcelNode(this, options as ExcelOutputOptionsNode)
}
}

/**
* Access a single value for a row index.
* Similar to iloc, in that both provide index-based lookups.
Expand Down
Loading