Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecated toJSON, toCSV, and toExcel methods in frame and series #440

Merged
merged 1 commit into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/danfojs-base/core/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import {
ArrayType2D,
DataFrameInterface,
BaseDataOptionType,
IPlotlyLib,
} from "../shared/types";
import { PlotlyLib } from "../../danfojs-base/plotting";

const utils = new Utils();

Expand Down Expand Up @@ -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");
}
}
}
95 changes: 62 additions & 33 deletions src/danfojs-base/core/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion src/danfojs-base/core/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ import {
ArrayType1D,
BaseDataOptionType,
SeriesInterface,
mapParam
mapParam,
IPlotlyLib
} from "../shared/types";
import { PlotlyLib } from "../../danfojs-base/plotting";

const utils = new Utils();

Expand Down Expand Up @@ -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");
}
}
}
7 changes: 6 additions & 1 deletion src/danfojs-base/plotting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/danfojs-base/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/danfojs-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
156 changes: 2 additions & 154 deletions src/danfojs-browser/src/core/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
}
Loading