Skip to content

Commit 6d06a5c

Browse files
authored
Merge pull request #505 from javascriptdata/463-cant-parse-dates-from-excel-files-into-dataframe-properly
add support for excel parsing options arg
2 parents 201ddf7 + ca671ca commit 6d06a5c

File tree

5 files changed

+38
-18
lines changed

5 files changed

+38
-18
lines changed

src/danfojs-base/core/frame.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
25232523
* Objects passed to the function are Series values whose
25242524
* index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1)
25252525
* @param callable Function to apply to each column or row.
2526-
* @param options.axis 0 or 1. If 0, compute the power column-wise, if 1, row-wise
2526+
* @param options.axis 0 or 1. If 0, apply "callable" column-wise, else apply row-wise
25272527
*
25282528
* @example
25292529
* ```

src/danfojs-base/io/browser/io.excel.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ import {
4949
* ```
5050
*/
5151
const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => {
52-
const { sheet, method, headers, frameConfig } = { sheet: 0, method: "GET", headers: {}, frameConfig: {}, ...options }
52+
const {
53+
sheet,
54+
method,
55+
headers,
56+
frameConfig,
57+
parsingOptions
58+
} = { sheet: 0, method: "GET", headers: {}, frameConfig: {}, parsingOptions: {}, ...options }
5359

5460
if (typeof file === "string" && file.startsWith("http")) {
5561

@@ -60,7 +66,7 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => {
6066
}
6167
response.arrayBuffer().then(arrBuf => {
6268
const arrBufInt8 = new Uint8Array(arrBuf);
63-
const workbook = read(arrBufInt8, { type: "array" })
69+
const workbook = read(arrBufInt8, { type: "array", ...parsingOptions });
6470
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
6571
const data = utils.sheet_to_json(worksheet);
6672
const df = new DataFrame(data, frameConfig);
@@ -74,7 +80,7 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => {
7480
} else if (file instanceof File) {
7581
const arrBuf = await file.arrayBuffer()
7682
const arrBufInt8 = new Uint8Array(arrBuf);
77-
const workbook = read(arrBufInt8, { type: "array" })
83+
const workbook = read(arrBufInt8, { type: "array", ...parsingOptions });
7884
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
7985
const data = utils.sheet_to_json(worksheet);
8086
const df = new DataFrame(data, frameConfig);
@@ -101,7 +107,11 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => {
101107
* ```
102108
*/
103109
const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptionsBrowser) => {
104-
let { fileName, sheetName } = { fileName: "./output.xlsx", sheetName: "Sheet1", ...options }
110+
let {
111+
fileName,
112+
sheetName,
113+
writingOptions
114+
} = { fileName: "./output.xlsx", sheetName: "Sheet1", ...options }
105115

106116
if (!(fileName.endsWith(".xlsx"))) {
107117
fileName = fileName + ".xlsx"
@@ -121,7 +131,7 @@ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptions
121131
const worksheet = utils.aoa_to_sheet(data);
122132
const wb = utils.book_new();
123133
utils.book_append_sheet(wb, worksheet, sheetName);
124-
writeFile(wb, `${fileName}`)
134+
writeFile(wb, `${fileName}`, writingOptions)
125135
};
126136

127137
export {

src/danfojs-base/io/node/io.excel.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ import fs from 'fs'
5252
* ```
5353
*/
5454
const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {}) => {
55-
const { sheet, method, headers, frameConfig } = { sheet: 0, method: "GET", headers: {}, frameConfig: {}, ...options }
55+
const {
56+
sheet,
57+
method,
58+
headers,
59+
frameConfig,
60+
parsingOptions
61+
} = { sheet: 0, method: "GET", headers: {}, frameConfig: {}, parsingOptions: {}, ...options }
5662

5763
if (filePath.startsWith("http") || filePath.startsWith("https")) {
5864

@@ -63,7 +69,7 @@ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {})
6369
}
6470
response.arrayBuffer().then(arrBuf => {
6571
const arrBufInt8 = new Uint8Array(arrBuf);
66-
const workbook = read(arrBufInt8, { type: "array" })
72+
const workbook = read(arrBufInt8, { type: "array", ...parsingOptions });
6773
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
6874
const data = utils.sheet_to_json(worksheet);
6975
const df = new DataFrame(data, frameConfig);
@@ -81,7 +87,7 @@ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {})
8187
reject("ENOENT: no such file or directory");
8288
}
8389

84-
const workbook = readFile(filePath);
90+
const workbook = readFile(filePath, parsingOptions);
8591
const worksheet = workbook.Sheets[workbook.SheetNames[sheet]];
8692
const data = utils.sheet_to_json(worksheet);
8793
const df = new DataFrame(data, frameConfig);
@@ -108,7 +114,11 @@ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {})
108114
* ```
109115
*/
110116
const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptionsNode) => {
111-
let { filePath, sheetName } = { filePath: "./output.xlsx", sheetName: "Sheet1", ...options }
117+
let {
118+
filePath,
119+
sheetName,
120+
writingOptions
121+
} = { filePath: "./output.xlsx", sheetName: "Sheet1", ...options }
112122

113123
if (!(filePath.endsWith(".xlsx"))) {
114124
filePath = filePath + ".xlsx"
@@ -128,7 +138,7 @@ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptions
128138
const worksheet = utils.aoa_to_sheet(data);
129139
const wb = utils.book_new();
130140
utils.book_append_sheet(wb, worksheet, sheetName);
131-
writeFile(wb, `${filePath}`)
141+
writeFile(wb, `${filePath}`, writingOptions);
132142
};
133143

134144
export {
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
2-
// This file is auto-generated by prebuild.js. Do not edit!
3-
const tf = require("@tensorflow/tfjs")
4-
export default tf
5-
1+
const tf = require("@tensorflow/tfjs-node")
2+
export default tf

src/danfojs-base/shared/types.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import DataFrame from '../core/frame';
2222
import Series from '../core/series';
2323
import Str from '../core/strings';
2424
import Dt from '../core/datetime';
25+
import { ParsingOptions, WritingOptions } from "xlsx";
2526

2627
export type DTYPES = "float32" | "int32" | "string" | "boolean" | "undefined"
2728

@@ -384,6 +385,7 @@ export type ExcelInputOptionsBrowser = {
384385
method?: string,
385386
headers?: any,
386387
frameConfig?: BaseDataOptionType
388+
parsingOptions?: ParsingOptions
387389
}
388390
export type JsonInputOptionsBrowser = {
389391
method?: string,
@@ -400,6 +402,7 @@ export type ExcelInputOptionsNode = {
400402
method?: string,
401403
headers?: HeadersInit
402404
frameConfig?: BaseDataOptionType
405+
parsingOptions?: ParsingOptions
403406
}
404407
export type JsonInputOptionsNode = {
405408
method?: string,
@@ -408,9 +411,9 @@ export type JsonInputOptionsNode = {
408411
}
409412

410413
export type CsvOutputOptionsBrowser = { fileName?: string, sep?: string, header?: boolean, download?: boolean };
411-
export type ExcelOutputOptionsBrowser = { fileName?: string, sheetName?: string };
414+
export type ExcelOutputOptionsBrowser = { fileName?: string, sheetName?: string, writingOptions?: WritingOptions };
412415
export type JsonOutputOptionsBrowser = { fileName?: string, format?: "row" | "column", download?: boolean };
413416

414417
export type CsvOutputOptionsNode = { filePath?: string, sep?: string, header?: boolean }
415418
export type JsonOutputOptionsNode = { format?: "row" | "column", filePath?: string }
416-
export type ExcelOutputOptionsNode = { filePath?: string, sheetName?: string }
419+
export type ExcelOutputOptionsNode = { filePath?: string, sheetName?: string, writingOptions?: WritingOptions }

0 commit comments

Comments
 (0)