diff --git a/src/danfojs-base/core/frame.ts b/src/danfojs-base/core/frame.ts index 0ef51afa..422df249 100644 --- a/src/danfojs-base/core/frame.ts +++ b/src/danfojs-base/core/frame.ts @@ -29,7 +29,9 @@ import { ArrayType2D, DataFrameInterface, BaseDataOptionType, + IPlotlyLib, } from "../shared/types"; +import { PlotlyLib } from "../../danfojs-base/plotting"; const utils = new Utils(); @@ -3401,4 +3403,19 @@ export default class DataFrame extends NDframe implements DataFrameInterface { } return (this.values as ArrayType2D)[this.index.indexOf(row)][this.columns.indexOf(column)] } + + /** + * 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): IPlotlyLib { + //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"); + } + } } diff --git a/src/danfojs-base/core/generic.ts b/src/danfojs-base/core/generic.ts index d5665d1b..5e4bf74b 100644 --- a/src/danfojs-base/core/generic.ts +++ b/src/danfojs-base/core/generic.ts @@ -427,47 +427,76 @@ export default class NDframe implements NDframeInterface { /** * 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. + * @deprecated Use `toCSV` function directly instead. + * @example + * ``` + * import * as dfd from "danfojs" + * const df = new dfd.DataFrame([[1, 2, 3], [4, 5, 6]]) + * const csv = dfd.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: "+" + * }) */ - // toCSV(options?: CsvOutputOptionsNode): string - // toCSV(options?: CsvOutputOptionsNode): string | void { - // return toCSV(this, options); - // } + toCSV(options?: any): string | void { + throw new Error("`toCSV` function is deprecated. Use `toCSV` function directly instead. e.g. `dfd.toCSV(df)`") + } + + /** + * Converts a DataFrame or Series to JSON. + * @deprecated Use `toJSON` function directly instead. + * @example + * ``` + * import * as dfd from "danfojs-node" + * const df = new dfd.DataFrame([[1, 2, 3], [4, 5, 6]]) + * const json = dfd.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" + * }) + * ``` + */ + toJSON(options?: any): object | void { + throw new Error("`toJSON` function is deprecated. Use `toJSON` function directly instead. e.g. `dfd.toJSON(df, { format: 'row' })`") + } /** - * Converts a DataFrame or 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. - * - `format`: The format of the JSON. Defaults to `'column'`. E.g for using `column` format: + * Converts a DataFrame or Series to Excel. + * @deprecated Use `toExcel` function directly instead. + * @example * ``` - * [{ "a": 1, "b": 2, "c": 3, "d": 4 }, - * { "a": 5, "b": 6, "c": 7, "d": 8 }] + * import * as dfd from "danfojs" + * const df = new dfd.DataFrame([[1, 2, 3], [4, 5, 6]]) + * dfd.toExcel(df, { + * filePath: "./data/sample.xlsx", + * sheetName: "MySheet", + * }) * ``` - * and `row` format: + * + * @example * ``` - * { "a": [1, 5, 9], - * "b": [2, 6, 10] - * } + * import { toExcel } from "danfojs-node" + * const df = new DataFrame([[1, 2, 3], [4, 5, 6]]) + * toExcel(df, { + * filePath: "./data/sample.xlsx", + * sheetName: "MySheet", + * }) * ``` */ - // toJSON(options?: { format?: "row" | "column", filePath?: string }): object - // toJSON(options?: { format?: "row" | "column", filePath?: string }): object | void { - // 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'`. - */ - // toExcel(options?: { filePath?: string, sheetName?: string }): void { - // return toExcel(this, options); - // } + toExcel(options?: any): void { + throw new Error("Deprecated. Use `toExcel` function directly instead. e.g. `dfd.toExcel(df, {filePath: 'path/to/file.xlsx'})`") + } /** * Pretty prints a DataFrame or Series to the console diff --git a/src/danfojs-base/core/series.ts b/src/danfojs-base/core/series.ts index f5f9751a..32468d17 100644 --- a/src/danfojs-base/core/series.ts +++ b/src/danfojs-base/core/series.ts @@ -29,8 +29,10 @@ import { ArrayType1D, BaseDataOptionType, SeriesInterface, - mapParam + mapParam, + IPlotlyLib } from "../shared/types"; +import { PlotlyLib } from "../../danfojs-base/plotting"; const utils = new Utils(); @@ -2167,4 +2169,19 @@ export default class Series extends NDframe implements SeriesInterface { } return (this.values as ArrayType1D)[this.index.indexOf(row)]; } + + /** + * 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): IPlotlyLib { + //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"); + } + } } \ No newline at end of file diff --git a/src/danfojs-base/plotting/index.ts b/src/danfojs-base/plotting/index.ts index 6709ca0d..21ba3b53 100644 --- a/src/danfojs-base/plotting/index.ts +++ b/src/danfojs-base/plotting/index.ts @@ -25,8 +25,13 @@ import { import Series from "../core/series"; import DataFrame from "../core/frame"; import { PlotConfigObject, IPlotlyLib } from "../shared/types" -import Plotly from "plotly.js-dist-min"; +let Plotly: IPlotlyLib; +if (typeof window !== "undefined") { + //check if in browser environment and require "plotly.js-dist-min" module + Plotly = require("plotly.js-dist-min") as IPlotlyLib; + +} class PlotlyLib implements IPlotlyLib { divId: string; diff --git a/src/danfojs-base/shared/types.ts b/src/danfojs-base/shared/types.ts index a0132410..2f4916b3 100644 --- a/src/danfojs-base/shared/types.ts +++ b/src/danfojs-base/shared/types.ts @@ -182,6 +182,7 @@ export interface SeriesInterface extends NDframeInterface { }): DataFrame iat(index: number): number | string | boolean | undefined at(index: string | number): number | string | boolean | undefined + plot(divId: string): IPlotlyLib } //Start of DataFrame class types @@ -326,6 +327,7 @@ export interface DataFrameInterface extends NDframeInterface { }): DataFrame | void iat(row: number, column: number): number | string | boolean | undefined at(row: string | number, column: string): number | string | boolean | undefined + plot(divId: string): IPlotlyLib } export interface DateTime { diff --git a/src/danfojs-browser/package.json b/src/danfojs-browser/package.json index 2b10e3d9..06347451 100644 --- a/src/danfojs-browser/package.json +++ b/src/danfojs-browser/package.json @@ -34,7 +34,7 @@ "build:clean": "rimraf ./dist && rimraf ./lib && node ./scripts/prebuild.js && yarn run build", "dev": "nodemon", "lint": "eslint ./src", - "bundle": "webpack --mode production", + "bundle": "webpack --mode development", "coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls", "coverage": "nyc report --reporter=text-lcov | coveralls && nyc report --reporter=lcov", "patch": "npm version patch" diff --git a/src/danfojs-browser/src/core/frame.ts b/src/danfojs-browser/src/core/frame.ts index ad7954f1..7190ad93 100644 --- a/src/danfojs-browser/src/core/frame.ts +++ b/src/danfojs-browser/src/core/frame.ts @@ -13,23 +13,7 @@ * ========================================================================== */ import BaseDataFrame from "../../../danfojs-base/core/frame" -import { PlotlyLib } from "../../../danfojs-base/plotting"; -import { toCSVBrowser, toJSONBrowser, toExcelBrowser } from "../../../danfojs-base/io/browser"; -import { - BaseDataOptionType, - DataFrameInterface, - CsvOutputOptionsBrowser, - JsonOutputOptionsBrowser, - ExcelOutputOptionsBrowser, - IPlotlyLib -} from "../../../danfojs-base/shared/types"; - -type ExtendedDataFrameInterface = DataFrameInterface & { - plot(divId: string): IPlotlyLib - toCSV(options?: CsvOutputOptionsBrowser): string | void - toJSON(options?: JsonOutputOptionsBrowser): object | void - toExcel(options?: ExcelOutputOptionsBrowser): void -} +import { BaseDataOptionType } from "../../../danfojs-base/shared/types"; /** * Two-dimensional ndarray with axis labels. @@ -41,145 +25,9 @@ type ExtendedDataFrameInterface = DataFrameInterface & { * @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 DataFrame extends BaseDataFrame implements ExtendedDataFrameInterface { +export default class DataFrame extends BaseDataFrame { [key: string]: any constructor(data?: any, options: BaseDataOptionType = {}) { super(data, options) } - - /** - * 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) { - const plt = new PlotlyLib(this, divId); - return plt; - } - - /** - * Converts a DataFrame to CSV. - * @param options Configuration object. Supports the following options: - * - `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({ fileName: 'data.csv', download: true }) //Downloads file in Browser - * ``` - * - */ - toCSV(options?: CsvOutputOptionsBrowser): string - toCSV(options?: CsvOutputOptionsBrowser): string | void { - return toCSVBrowser(this, options) - - } - - /** - * Converts a DataFrame to JSON. - * @param options Configuration object. Supported options: - * - `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({ fileName: 'data.json', download: true }) // downloads file browser - * ``` - */ - toJSON(options?: JsonOutputOptionsBrowser): object - toJSON(options?: JsonOutputOptionsBrowser): object | void { - return toJSONBrowser(this, options) - } - - - /** - * 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): void { - return toExcelBrowser(this, options) - } } \ No newline at end of file diff --git a/src/danfojs-browser/src/core/series.ts b/src/danfojs-browser/src/core/series.ts index efd1cb90..f117bf3b 100644 --- a/src/danfojs-browser/src/core/series.ts +++ b/src/danfojs-browser/src/core/series.ts @@ -13,24 +13,7 @@ * ========================================================================== */ import BaseSeries from "../../../danfojs-base/core/series" -import { PlotlyLib } from "../../../danfojs-base/plotting"; -import { toCSVBrowser, toJSONBrowser, toExcelBrowser } from "../../../danfojs-base/io/browser"; -import { - BaseDataOptionType, - SeriesInterface, - CsvOutputOptionsBrowser, - JsonOutputOptionsBrowser, - ExcelOutputOptionsBrowser, - IPlotlyLib -} from "../../../danfojs-base/shared/types"; - -type ExtendedSeriesInterface = SeriesInterface & { - plot(divId: string): IPlotlyLib - toCSV(options?: CsvOutputOptionsBrowser): string | void - toJSON(options?: JsonOutputOptionsBrowser): object | void - toExcel(options?: ExcelOutputOptionsBrowser): void -} - +import { BaseDataOptionType } from "../../../danfojs-base/shared/types"; /** * One-dimensional ndarray with axis labels. @@ -42,127 +25,9 @@ type ExtendedSeriesInterface = SeriesInterface & { * @param options.dtypes Data types of the Series data. If not specified, dtypes is inferred. * @param options.config General configuration object for extending or setting Series behavior. */ -export default class Series extends BaseSeries implements ExtendedSeriesInterface { +export default class Series extends BaseSeries { [key: string]: any constructor(data?: any, options: BaseDataOptionType = {}) { super(data, options) } - - /** - * 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) { - const plt = new PlotlyLib(this, divId); - return plt; - } - - - /** - * Converts a Series to CSV. - * @param options Configuration object. Supports the following options: - * - `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 Series([1, 2, 3, 4]) - * const csv = df.toCSV() - * console.log(csv) - * ``` - * - * @example - * ``` - * const df = new Series([1, 2, 3, 4]) - * const csv = df.toCSV({ header: false }) - * ``` - * - * @example - * ``` - * const df = new Series([1, 2, 3, 4]) - * const csv = df.toCSV({ sep: ';' }) - * ``` - * - * @example - * ``` - * const df = new Series([1, 2, 3, 4]) - * df.toCSV({ fileName: 'data.csv', download: true }) //Downloads file in Browser - * ``` - * - */ - toCSV(options?: CsvOutputOptionsBrowser): string - toCSV(options?: CsvOutputOptionsBrowser): string | void { - return toCSVBrowser(this, options) - - } - - /** - * Converts a Series to JSON. - * @param options Configuration object. Supported options: - * - `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 Series([1, 2, 3, 4]) - * const json = df.toJSON() - * ``` - * - * @example - * ``` - * const df = new Series([1, 2, 3, 4]) - * const json = df.toJSON({ format: 'row' }) - * ``` - * - * @example - * ``` - * const df = new Series([1, 2, 3, 4]) - * const json = df.toJSON({ format: "column" }) - * ``` - * - * @example - * ``` - * const df = new Series([1, 2, 3, 4]) - * df.toJSON({ fileName: 'data.json', download: true }) // downloads file browser - * ``` - */ - toJSON(options?: JsonOutputOptionsBrowser): object - toJSON(options?: JsonOutputOptionsBrowser): object | void { - return toJSONBrowser(this, options) - } - - - /** - * 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): void { - return toExcelBrowser(this, options) - } } \ No newline at end of file diff --git a/src/danfojs-node/test/core/frame.test.ts b/src/danfojs-node/test/core/frame.test.ts index c6f69406..6ce5664d 100644 --- a/src/danfojs-node/test/core/frame.test.ts +++ b/src/danfojs-node/test/core/frame.test.ts @@ -2857,69 +2857,6 @@ describe("DataFrame", function () { }); }); - describe("IO outputs", function () { - it("toExcel works", async function () { - const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] - const df: any = new DataFrame(data, { columns: ["a", "b", "c", "d"] }); - - const filePath = path.join(process.cwd(), "test", "samples", "test.xlsx"); - df.toExcel({ filePath }) - - const dfNew: any = await readExcel(filePath, {}); - assert.equal(fs.existsSync(filePath), true) - assert.deepEqual(dfNew.columns, [ - 'a', - 'b', - 'c', - 'd', - ]); - assert.deepEqual(dfNew.dtypes, [ - 'int32', 'int32', - 'int32', 'int32', - ]); - assert.deepEqual(dfNew.shape, [3, 4]) - }); - - it("toCSV works for specified seperator", async function () { - const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] - let df: any = new DataFrame(data, { columns: ["a", "b", "c", "d"] }); - assert.deepEqual(df.toCSV({ sep: "+" }), `a+b+c+d\n1+2+3+4\n5+6+7+8\n9+10+11+12\n`); - }); - it("toCSV write to local file works", async function () { - const data = [[1, 2, 3, "4"], [5, 6, 7, "8"], [9, 10, 11, "12"]] - let df: any = new DataFrame(data, { columns: ["a", "b", "c", "d"] }); - - const filePath = path.join(process.cwd(), "test", "samples", "test_write.csv"); - - df.toCSV({ sep: ",", filePath }); - assert.equal(fs.existsSync(filePath), true); - }); - it("toJSON works for row format", async function () { - const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] - const df: any = new DataFrame(data, { columns: ["a", "b", "c", "d"] }); - const expected: any = { - "a": [1, 5, 9], - "b": [2, 6, 10], - "c": [3, 7, 11], - "d": [4, 8, 12], - } - const json = df.toJSON({ format: "row" }) - assert.deepEqual(json, expected); - }); - it("toJSON writes file to local path", async function () { - const data = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] - const df: any = new DataFrame(data, { columns: ["a", "b", "c", "d"] }); - - const rowfilePath = path.join(process.cwd(), "test", "samples", "test_row_write.json"); - const colfilePath = path.join(process.cwd(), "test", "samples", "test_col_write.json"); - - df.toJSON({ format: "row", filePath: rowfilePath }) - df.toJSON({ format: "column", filePath: colfilePath }) - assert.equal(fs.existsSync(rowfilePath), true); - assert.equal(fs.existsSync(colfilePath), true); - }); - }) - describe("getDummies", function () { it("getDummies works on DataFrame", function () { diff --git a/src/danfojs-node/test/samples/sampleOut.xlsx b/src/danfojs-node/test/samples/sampleOut.xlsx index e7e63112..60360615 100644 Binary files a/src/danfojs-node/test/samples/sampleOut.xlsx and b/src/danfojs-node/test/samples/sampleOut.xlsx differ diff --git a/src/danfojs-node/test/samples/testSeries.xlsx b/src/danfojs-node/test/samples/testSeries.xlsx index d5f64fe8..c5ccc99c 100644 Binary files a/src/danfojs-node/test/samples/testSeries.xlsx and b/src/danfojs-node/test/samples/testSeries.xlsx differ