Skip to content

Commit 182bec1

Browse files
authored
Merge pull request #440 from javascriptdata/ref/io-output
Deprecated toJSON, toCSV, and toExcel methods in frame and series
2 parents 889778b + f490be9 commit 182bec1

File tree

11 files changed

+110
-390
lines changed

11 files changed

+110
-390
lines changed

src/danfojs-base/core/frame.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import {
2929
ArrayType2D,
3030
DataFrameInterface,
3131
BaseDataOptionType,
32+
IPlotlyLib,
3233
} from "../shared/types";
34+
import { PlotlyLib } from "../../danfojs-base/plotting";
3335

3436
const utils = new Utils();
3537

@@ -3401,4 +3403,19 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
34013403
}
34023404
return (this.values as ArrayType2D)[this.index.indexOf(row)][this.columns.indexOf(column)]
34033405
}
3406+
3407+
/**
3408+
* Exposes functions for creating charts from a DataFrame.
3409+
* Charts are created using the Plotly.js library, so all Plotly's configuration parameters are available.
3410+
* @param divId name of the HTML Div to render the chart in.
3411+
*/
3412+
plot(divId: string): IPlotlyLib {
3413+
//TODO: Add support for check plot library to use. So we can support other plot library like d3, vega, etc
3414+
if (utils.isBrowserEnv()) {
3415+
const plt = new PlotlyLib(this, divId);
3416+
return plt;
3417+
} else {
3418+
throw new Error("Not supported in NodeJS");
3419+
}
3420+
}
34043421
}

src/danfojs-base/core/generic.ts

Lines changed: 62 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -427,47 +427,76 @@ export default class NDframe implements NDframeInterface {
427427

428428
/**
429429
* Converts a DataFrame or Series to CSV.
430-
* @param options Configuration object. Supports the following options:
431-
* - `filePath`: Local file path to write the CSV file. If not specified, the CSV will be returned as a string.
432-
* - `header`: Boolean indicating whether to include a header row in the CSV file.
433-
* - `sep`: Character to be used as a separator in the CSV file.
430+
* @deprecated Use `toCSV` function directly instead.
431+
* @example
432+
* ```
433+
* import * as dfd from "danfojs"
434+
* const df = new dfd.DataFrame([[1, 2, 3], [4, 5, 6]])
435+
* const csv = dfd.toCSV(df)
436+
* ```
437+
* @example
438+
* ```
439+
* import { toCSV } from "danfojs-node"
440+
* const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
441+
* toCSV(df, {
442+
* filePath: "./data/sample.csv",
443+
* header: true,
444+
* sep: "+"
445+
* })
434446
*/
435-
// toCSV(options?: CsvOutputOptionsNode): string
436-
// toCSV(options?: CsvOutputOptionsNode): string | void {
437-
// return toCSV(this, options);
438-
// }
447+
toCSV(options?: any): string | void {
448+
throw new Error("`toCSV` function is deprecated. Use `toCSV` function directly instead. e.g. `dfd.toCSV(df)`")
449+
}
450+
451+
/**
452+
* Converts a DataFrame or Series to JSON.
453+
* @deprecated Use `toJSON` function directly instead.
454+
* @example
455+
* ```
456+
* import * as dfd from "danfojs-node"
457+
* const df = new dfd.DataFrame([[1, 2, 3], [4, 5, 6]])
458+
* const json = dfd.toJSON(df)
459+
* ```
460+
* @example
461+
* ```
462+
* import { toJSON } from "danfojs-node"
463+
* const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
464+
* toJSON(df, {
465+
* filePath: "./data/sample.json",
466+
* format: "row"
467+
* })
468+
* ```
469+
*/
470+
toJSON(options?: any): object | void {
471+
throw new Error("`toJSON` function is deprecated. Use `toJSON` function directly instead. e.g. `dfd.toJSON(df, { format: 'row' })`")
472+
}
439473

440474
/**
441-
* Converts a DataFrame or Series to JSON.
442-
* @param options Configuration object. Supported options:
443-
* - `filePath`: The file path to write the JSON to. If not specified, the JSON object is returned.
444-
* - `format`: The format of the JSON. Defaults to `'column'`. E.g for using `column` format:
475+
* Converts a DataFrame or Series to Excel.
476+
* @deprecated Use `toExcel` function directly instead.
477+
* @example
445478
* ```
446-
* [{ "a": 1, "b": 2, "c": 3, "d": 4 },
447-
* { "a": 5, "b": 6, "c": 7, "d": 8 }]
479+
* import * as dfd from "danfojs"
480+
* const df = new dfd.DataFrame([[1, 2, 3], [4, 5, 6]])
481+
* dfd.toExcel(df, {
482+
* filePath: "./data/sample.xlsx",
483+
* sheetName: "MySheet",
484+
* })
448485
* ```
449-
* and `row` format:
486+
*
487+
* @example
450488
* ```
451-
* { "a": [1, 5, 9],
452-
* "b": [2, 6, 10]
453-
* }
489+
* import { toExcel } from "danfojs-node"
490+
* const df = new DataFrame([[1, 2, 3], [4, 5, 6]])
491+
* toExcel(df, {
492+
* filePath: "./data/sample.xlsx",
493+
* sheetName: "MySheet",
494+
* })
454495
* ```
455496
*/
456-
// toJSON(options?: { format?: "row" | "column", filePath?: string }): object
457-
// toJSON(options?: { format?: "row" | "column", filePath?: string }): object | void {
458-
// return toJSON(this, options);
459-
// }
460-
461-
462-
/**
463-
* Converts a DataFrame or Series to Excel Sheet.
464-
* @param options Configuration object. Supported options:
465-
* - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
466-
* - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`.
467-
*/
468-
// toExcel(options?: { filePath?: string, sheetName?: string }): void {
469-
// return toExcel(this, options);
470-
// }
497+
toExcel(options?: any): void {
498+
throw new Error("Deprecated. Use `toExcel` function directly instead. e.g. `dfd.toExcel(df, {filePath: 'path/to/file.xlsx'})`")
499+
}
471500

472501
/**
473502
* Pretty prints a DataFrame or Series to the console

src/danfojs-base/core/series.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ import {
2929
ArrayType1D,
3030
BaseDataOptionType,
3131
SeriesInterface,
32-
mapParam
32+
mapParam,
33+
IPlotlyLib
3334
} from "../shared/types";
35+
import { PlotlyLib } from "../../danfojs-base/plotting";
3436

3537
const utils = new Utils();
3638

@@ -2167,4 +2169,19 @@ export default class Series extends NDframe implements SeriesInterface {
21672169
}
21682170
return (this.values as ArrayType1D)[this.index.indexOf(row)];
21692171
}
2172+
2173+
/**
2174+
* Exposes functions for creating charts from a DataFrame.
2175+
* Charts are created using the Plotly.js library, so all Plotly's configuration parameters are available.
2176+
* @param divId name of the HTML Div to render the chart in.
2177+
*/
2178+
plot(divId: string): IPlotlyLib {
2179+
//TODO: Add support for check plot library to use. So we can support other plot library like d3, vega, etc
2180+
if (utils.isBrowserEnv()) {
2181+
const plt = new PlotlyLib(this, divId);
2182+
return plt;
2183+
} else {
2184+
throw new Error("Not supported in NodeJS");
2185+
}
2186+
}
21702187
}

src/danfojs-base/plotting/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ import {
2525
import Series from "../core/series";
2626
import DataFrame from "../core/frame";
2727
import { PlotConfigObject, IPlotlyLib } from "../shared/types"
28-
import Plotly from "plotly.js-dist-min";
28+
let Plotly: IPlotlyLib;
2929

30+
if (typeof window !== "undefined") {
31+
//check if in browser environment and require "plotly.js-dist-min" module
32+
Plotly = require("plotly.js-dist-min") as IPlotlyLib;
33+
34+
}
3035

3136
class PlotlyLib implements IPlotlyLib {
3237
divId: string;

src/danfojs-base/shared/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ export interface SeriesInterface extends NDframeInterface {
182182
}): DataFrame
183183
iat(index: number): number | string | boolean | undefined
184184
at(index: string | number): number | string | boolean | undefined
185+
plot(divId: string): IPlotlyLib
185186
}
186187

187188
//Start of DataFrame class types
@@ -326,6 +327,7 @@ export interface DataFrameInterface extends NDframeInterface {
326327
}): DataFrame | void
327328
iat(row: number, column: number): number | string | boolean | undefined
328329
at(row: string | number, column: string): number | string | boolean | undefined
330+
plot(divId: string): IPlotlyLib
329331
}
330332

331333
export interface DateTime {

src/danfojs-browser/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"build:clean": "rimraf ./dist && rimraf ./lib && node ./scripts/prebuild.js && yarn run build",
3535
"dev": "nodemon",
3636
"lint": "eslint ./src",
37-
"bundle": "webpack --mode production",
37+
"bundle": "webpack --mode development",
3838
"coveralls": "cat ./coverage/lcov.info | ./node_modules/.bin/coveralls",
3939
"coverage": "nyc report --reporter=text-lcov | coveralls && nyc report --reporter=lcov",
4040
"patch": "npm version patch"

src/danfojs-browser/src/core/frame.ts

Lines changed: 2 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,7 @@
1313
* ==========================================================================
1414
*/
1515
import BaseDataFrame from "../../../danfojs-base/core/frame"
16-
import { PlotlyLib } from "../../../danfojs-base/plotting";
17-
import { toCSVBrowser, toJSONBrowser, toExcelBrowser } from "../../../danfojs-base/io/browser";
18-
import {
19-
BaseDataOptionType,
20-
DataFrameInterface,
21-
CsvOutputOptionsBrowser,
22-
JsonOutputOptionsBrowser,
23-
ExcelOutputOptionsBrowser,
24-
IPlotlyLib
25-
} from "../../../danfojs-base/shared/types";
26-
27-
type ExtendedDataFrameInterface = DataFrameInterface & {
28-
plot(divId: string): IPlotlyLib
29-
toCSV(options?: CsvOutputOptionsBrowser): string | void
30-
toJSON(options?: JsonOutputOptionsBrowser): object | void
31-
toExcel(options?: ExcelOutputOptionsBrowser): void
32-
}
16+
import { BaseDataOptionType } from "../../../danfojs-base/shared/types";
3317

3418
/**
3519
* Two-dimensional ndarray with axis labels.
@@ -41,145 +25,9 @@ type ExtendedDataFrameInterface = DataFrameInterface & {
4125
* @param options.dtypes Array of data types for each the column. If not specified, dtypes are/is inferred.
4226
* @param options.config General configuration object for extending or setting NDframe behavior.
4327
*/
44-
export default class DataFrame extends BaseDataFrame implements ExtendedDataFrameInterface {
28+
export default class DataFrame extends BaseDataFrame {
4529
[key: string]: any
4630
constructor(data?: any, options: BaseDataOptionType = {}) {
4731
super(data, options)
4832
}
49-
50-
/**
51-
* Exposes functions for creating charts from a DataFrame.
52-
* Charts are created using the Plotly.js library, so all Plotly's configuration parameters are available.
53-
* @param divId name of the HTML Div to render the chart in.
54-
*/
55-
plot(divId: string) {
56-
const plt = new PlotlyLib(this, divId);
57-
return plt;
58-
}
59-
60-
/**
61-
* Converts a DataFrame to CSV.
62-
* @param options Configuration object. Supports the following options:
63-
* - `fileName`: Name of the CSV file. Defaults to `data.csv`. Option is only available in Browser.
64-
* - `download`: If true, the CSV will be downloaded. Defaults to false. Option is only available in Browser.
65-
* - `header`: Boolean indicating whether to include a header row in the CSV file.
66-
* - `sep`: Character to be used as a separator in the CSV file.
67-
*
68-
* @example
69-
* ```
70-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
71-
* const csv = df.toCSV()
72-
* console.log(csv)
73-
* //output
74-
* "A","B"
75-
* 1,2
76-
* 3,4
77-
* ```
78-
*
79-
* @example
80-
* ```
81-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
82-
* const csv = df.toCSV({ header: false })
83-
* console.log(csv)
84-
* //output
85-
* 1,2
86-
* 3,4
87-
* ```
88-
*
89-
* @example
90-
* ```
91-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
92-
* const csv = df.toCSV({ sep: ';' })
93-
* console.log(csv)
94-
* //output
95-
* "A";"B"
96-
* 1;2
97-
* 3;4
98-
* ```
99-
*
100-
* @example
101-
* ```
102-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
103-
* df.toCSV({ fileName: 'data.csv', download: true }) //Downloads file in Browser
104-
* ```
105-
*
106-
*/
107-
toCSV(options?: CsvOutputOptionsBrowser): string
108-
toCSV(options?: CsvOutputOptionsBrowser): string | void {
109-
return toCSVBrowser(this, options)
110-
111-
}
112-
113-
/**
114-
* Converts a DataFrame to JSON.
115-
* @param options Configuration object. Supported options:
116-
* - `fileName`: The name of the JSON file. Defaults to `data.json`. Option is only available in Browser.
117-
* - `download`: If true, the JSON will be downloaded. Defaults to false. Option is only available in Browser.
118-
* - `format`: The format of the JSON. Supported values are `'column'` and `'row'`. Defaults to `'column'`.
119-
*
120-
* @example
121-
* ```
122-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
123-
* const json = df.toJSON()
124-
* ```
125-
*
126-
* @example
127-
* ```
128-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
129-
* const json = df.toJSON({ format: 'row' })
130-
* console.log(json)
131-
* //output
132-
* [{"A":1,"B":2},{"A":3,"B":4}]
133-
* ```
134-
*
135-
* @example
136-
* ```
137-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
138-
* const json = df.toJSON({ format: "column" })
139-
* console.log(json)
140-
* //output
141-
* {"A":[1,3],"B":[2,4]}
142-
* ```
143-
*
144-
* @example
145-
* ```
146-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
147-
* df.toJSON({ fileName: 'data.json', download: true }) // downloads file browser
148-
* ```
149-
*/
150-
toJSON(options?: JsonOutputOptionsBrowser): object
151-
toJSON(options?: JsonOutputOptionsBrowser): object | void {
152-
return toJSONBrowser(this, options)
153-
}
154-
155-
156-
/**
157-
* Converts a DataFrame to Excel file format.
158-
* @param options Configuration object. Supported options:
159-
* - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
160-
* - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`. Option is only available in NodeJs
161-
* - `fileName`: The fileName to be written to. Defaults to `'output.xlsx'`. Option is only available in Browser
162-
*
163-
* @example
164-
* ```
165-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
166-
* df.toExcel({ filePath: './output.xlsx' }) // writes to local file system as output.xlsx in NodeJS
167-
* ```
168-
*
169-
* @example
170-
* ```
171-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
172-
* df.toExcel({ fileName: 'output.xlsx', download: true }) // downloads file browser
173-
* ```
174-
*
175-
* @example
176-
* ```
177-
* const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
178-
* df.toExcel({ sheetName: 'Sheet2' }) // writes to Sheet2 in Excel
179-
* ```
180-
*
181-
*/
182-
toExcel(options?: ExcelOutputOptionsBrowser): void {
183-
return toExcelBrowser(this, options)
184-
}
18533
}

0 commit comments

Comments
 (0)