1212* limitations under the License.
1313* ==========================================================================
1414*/
15- import { BaseDataOptionType } from "../../../danfojs-base/shared/types" ;
1615import BaseDataFrame from "../../../danfojs-base/core/frame"
16+ import { toCSVBrowser , toJSONBrowser , toExcelBrowser } from "../../../danfojs-base/io/browser" ;
17+ import {
18+ BaseDataOptionType ,
19+ DataFrameInterface ,
20+ CsvOutputOptionsBrowser ,
21+ JsonOutputOptionsBrowser ,
22+ ExcelOutputOptionsBrowser
23+ } from "../../../danfojs-base/shared/types" ;
1724
25+ type ExtendedDataFrameInterface = DataFrameInterface & {
26+ toCSV ( options ?: CsvOutputOptionsBrowser ) : string | void
27+ toJSON ( options ?: JsonOutputOptionsBrowser ) : object | void
28+ toExcel ( options ?: ExcelOutputOptionsBrowser ) : void
29+ }
1830
1931/**
2032 * Two-dimensional ndarray with axis labels.
@@ -26,9 +38,135 @@ import BaseDataFrame from "../../../danfojs-base/core/frame"
2638 * @param options.dtypes Array of data types for each the column. If not specified, dtypes are/is inferred.
2739 * @param options.config General configuration object for extending or setting NDframe behavior.
2840 */
29- export default class DataFrame extends BaseDataFrame {
41+ export default class DataFrame extends BaseDataFrame implements ExtendedDataFrameInterface {
3042 [ key : string ] : any
3143 constructor ( data ?: any , options : BaseDataOptionType = { } ) {
3244 super ( data , options )
3345 }
46+
47+ /**
48+ * Converts a DataFrame to CSV.
49+ * @param options Configuration object. Supports the following options:
50+ * - `fileName`: Name of the CSV file. Defaults to `data.csv`. Option is only available in Browser.
51+ * - `download`: If true, the CSV will be downloaded. Defaults to false. Option is only available in Browser.
52+ * - `header`: Boolean indicating whether to include a header row in the CSV file.
53+ * - `sep`: Character to be used as a separator in the CSV file.
54+ *
55+ * @example
56+ * ```
57+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
58+ * const csv = df.toCSV()
59+ * console.log(csv)
60+ * //output
61+ * "A","B"
62+ * 1,2
63+ * 3,4
64+ * ```
65+ *
66+ * @example
67+ * ```
68+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
69+ * const csv = df.toCSV({ header: false })
70+ * console.log(csv)
71+ * //output
72+ * 1,2
73+ * 3,4
74+ * ```
75+ *
76+ * @example
77+ * ```
78+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
79+ * const csv = df.toCSV({ sep: ';' })
80+ * console.log(csv)
81+ * //output
82+ * "A";"B"
83+ * 1;2
84+ * 3;4
85+ * ```
86+ *
87+ * @example
88+ * ```
89+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
90+ * df.toCSV({ fileName: 'data.csv', download: true }) //Downloads file in Browser
91+ * ```
92+ *
93+ */
94+ toCSV ( options ?: CsvOutputOptionsBrowser ) : string
95+ toCSV ( options ?: CsvOutputOptionsBrowser ) : string | void {
96+ return toCSVBrowser ( this , options )
97+
98+ }
99+
100+ /**
101+ * Converts a DataFrame to JSON.
102+ * @param options Configuration object. Supported options:
103+ * - `fileName`: The name of the JSON file. Defaults to `data.json`. Option is only available in Browser.
104+ * - `download`: If true, the JSON will be downloaded. Defaults to false. Option is only available in Browser.
105+ * - `format`: The format of the JSON. Supported values are `'column'` and `'row'`. Defaults to `'column'`.
106+ *
107+ * @example
108+ * ```
109+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
110+ * const json = df.toJSON()
111+ * ```
112+ *
113+ * @example
114+ * ```
115+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
116+ * const json = df.toJSON({ format: 'row' })
117+ * console.log(json)
118+ * //output
119+ * [{"A":1,"B":2},{"A":3,"B":4}]
120+ * ```
121+ *
122+ * @example
123+ * ```
124+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
125+ * const json = df.toJSON({ format: "column" })
126+ * console.log(json)
127+ * //output
128+ * {"A":[1,3],"B":[2,4]}
129+ * ```
130+ *
131+ * @example
132+ * ```
133+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
134+ * df.toJSON({ fileName: 'data.json', download: true }) // downloads file browser
135+ * ```
136+ */
137+ toJSON ( options ?: JsonOutputOptionsBrowser ) : object
138+ toJSON ( options ?: JsonOutputOptionsBrowser ) : object | void {
139+ return toJSONBrowser ( this , options )
140+ }
141+
142+
143+ /**
144+ * Converts a DataFrame to Excel file format.
145+ * @param options Configuration object. Supported options:
146+ * - `sheetName`: The sheet name to be written to. Defaults to `'Sheet1'`.
147+ * - `filePath`: The filePath to be written to. Defaults to `'./output.xlsx'`. Option is only available in NodeJs
148+ * - `fileName`: The fileName to be written to. Defaults to `'output.xlsx'`. Option is only available in Browser
149+ *
150+ * @example
151+ * ```
152+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
153+ * df.toExcel({ filePath: './output.xlsx' }) // writes to local file system as output.xlsx in NodeJS
154+ * ```
155+ *
156+ * @example
157+ * ```
158+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
159+ * df.toExcel({ fileName: 'output.xlsx', download: true }) // downloads file browser
160+ * ```
161+ *
162+ * @example
163+ * ```
164+ * const df = new DataFrame([[1, 2], [3, 4]], { columns: ['A', 'B']})
165+ * df.toExcel({ sheetName: 'Sheet2' }) // writes to Sheet2 in Excel
166+ * ```
167+ *
168+ */
169+ toExcel ( options ?: ExcelOutputOptionsBrowser ) : void {
170+ return toExcelBrowser ( this , options )
171+ }
34172}
0 commit comments