From e3f3579e50cfe18c708a6f94255ec59eb693b636 Mon Sep 17 00:00:00 2001 From: Rising Odegua Date: Sun, 25 Sep 2022 11:53:53 +0100 Subject: [PATCH 1/2] add support for excel parsing options arg --- src/danfojs-base/core/frame.ts | 2 +- src/danfojs-base/io/browser/io.excel.ts | 20 +++++++++++++++----- src/danfojs-base/io/node/io.excel.ts | 20 +++++++++++++++----- src/danfojs-base/shared/tensorflowlib.ts | 7 ++----- src/danfojs-base/shared/types.ts | 7 +++++-- 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/danfojs-base/core/frame.ts b/src/danfojs-base/core/frame.ts index 5c752569..13d8cc55 100644 --- a/src/danfojs-base/core/frame.ts +++ b/src/danfojs-base/core/frame.ts @@ -2523,7 +2523,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface { * Objects passed to the function are Series values whose * index is either the DataFrame’s index (axis=0) or the DataFrame’s columns (axis=1) * @param callable Function to apply to each column or row. - * @param options.axis 0 or 1. If 0, compute the power column-wise, if 1, row-wise + * @param options.axis 0 or 1. If 0, apply "callable" column-wise, else apply row-wise * * @example * ``` diff --git a/src/danfojs-base/io/browser/io.excel.ts b/src/danfojs-base/io/browser/io.excel.ts index c6a4b821..b62cd5ed 100644 --- a/src/danfojs-base/io/browser/io.excel.ts +++ b/src/danfojs-base/io/browser/io.excel.ts @@ -49,7 +49,13 @@ import { * ``` */ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => { - const { sheet, method, headers, frameConfig } = { sheet: 0, method: "GET", headers: {}, frameConfig: {}, ...options } + const { + sheet, + method, + headers, + frameConfig, + parsingOptions + } = { sheet: 0, method: "GET", headers: {}, frameConfig: {}, parsingOptions: {}, ...options } if (typeof file === "string" && file.startsWith("http")) { @@ -60,7 +66,7 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => { } response.arrayBuffer().then(arrBuf => { const arrBufInt8 = new Uint8Array(arrBuf); - const workbook = read(arrBufInt8, { type: "array" }) + const workbook = read(arrBufInt8, { type: "array", ...parsingOptions }); const worksheet = workbook.Sheets[workbook.SheetNames[sheet]]; const data = utils.sheet_to_json(worksheet); const df = new DataFrame(data, frameConfig); @@ -74,7 +80,7 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => { } else if (file instanceof File) { const arrBuf = await file.arrayBuffer() const arrBufInt8 = new Uint8Array(arrBuf); - const workbook = read(arrBufInt8, { type: "array" }) + const workbook = read(arrBufInt8, { type: "array", ...parsingOptions }); const worksheet = workbook.Sheets[workbook.SheetNames[sheet]]; const data = utils.sheet_to_json(worksheet); const df = new DataFrame(data, frameConfig); @@ -101,7 +107,11 @@ const $readExcel = async (file: any, options?: ExcelInputOptionsBrowser) => { * ``` */ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptionsBrowser) => { - let { fileName, sheetName } = { fileName: "./output.xlsx", sheetName: "Sheet1", ...options } + let { + fileName, + sheetName, + writingOptions + } = { fileName: "./output.xlsx", sheetName: "Sheet1", ...options } if (!(fileName.endsWith(".xlsx"))) { fileName = fileName + ".xlsx" @@ -121,7 +131,7 @@ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptions const worksheet = utils.aoa_to_sheet(data); const wb = utils.book_new(); utils.book_append_sheet(wb, worksheet, sheetName); - writeFile(wb, `${fileName}`) + writeFile(wb, `${fileName}`, writingOptions) }; export { diff --git a/src/danfojs-base/io/node/io.excel.ts b/src/danfojs-base/io/node/io.excel.ts index cbc8841e..0a653c21 100644 --- a/src/danfojs-base/io/node/io.excel.ts +++ b/src/danfojs-base/io/node/io.excel.ts @@ -52,7 +52,13 @@ import fs from 'fs' * ``` */ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {}) => { - const { sheet, method, headers, frameConfig } = { sheet: 0, method: "GET", headers: {}, frameConfig: {}, ...options } + const { + sheet, + method, + headers, + frameConfig, + parsingOptions + } = { sheet: 0, method: "GET", headers: {}, frameConfig: {}, parsingOptions: {}, ...options } if (filePath.startsWith("http") || filePath.startsWith("https")) { @@ -63,7 +69,7 @@ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {}) } response.arrayBuffer().then(arrBuf => { const arrBufInt8 = new Uint8Array(arrBuf); - const workbook = read(arrBufInt8, { type: "array" }) + const workbook = read(arrBufInt8, { type: "array", ...parsingOptions }); const worksheet = workbook.Sheets[workbook.SheetNames[sheet]]; const data = utils.sheet_to_json(worksheet); const df = new DataFrame(data, frameConfig); @@ -81,7 +87,7 @@ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {}) reject("ENOENT: no such file or directory"); } - const workbook = readFile(filePath); + const workbook = readFile(filePath, parsingOptions); const worksheet = workbook.Sheets[workbook.SheetNames[sheet]]; const data = utils.sheet_to_json(worksheet); const df = new DataFrame(data, frameConfig); @@ -108,7 +114,11 @@ const $readExcel = async (filePath: string, options: ExcelInputOptionsNode = {}) * ``` */ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptionsNode) => { - let { filePath, sheetName } = { filePath: "./output.xlsx", sheetName: "Sheet1", ...options } + let { + filePath, + sheetName, + writingOptions + } = { filePath: "./output.xlsx", sheetName: "Sheet1", ...options } if (!(filePath.endsWith(".xlsx"))) { filePath = filePath + ".xlsx" @@ -128,7 +138,7 @@ const $toExcel = (df: NDframe | DataFrame | Series, options?: ExcelOutputOptions const worksheet = utils.aoa_to_sheet(data); const wb = utils.book_new(); utils.book_append_sheet(wb, worksheet, sheetName); - writeFile(wb, `${filePath}`) + writeFile(wb, `${filePath}`, writingOptions); }; export { diff --git a/src/danfojs-base/shared/tensorflowlib.ts b/src/danfojs-base/shared/tensorflowlib.ts index 47ef99ee..476573e8 100644 --- a/src/danfojs-base/shared/tensorflowlib.ts +++ b/src/danfojs-base/shared/tensorflowlib.ts @@ -1,5 +1,2 @@ - - // This file is auto-generated by prebuild.js. Do not edit! - const tf = require("@tensorflow/tfjs") -export default tf - \ No newline at end of file +const tf = require("@tensorflow/tfjs-node") +export default tf \ No newline at end of file diff --git a/src/danfojs-base/shared/types.ts b/src/danfojs-base/shared/types.ts index 2f4916b3..29b74d73 100644 --- a/src/danfojs-base/shared/types.ts +++ b/src/danfojs-base/shared/types.ts @@ -22,6 +22,7 @@ import DataFrame from '../core/frame'; import Series from '../core/series'; import Str from '../core/strings'; import Dt from '../core/datetime'; +import { ParsingOptions, WritingOptions } from "xlsx"; export type DTYPES = "float32" | "int32" | "string" | "boolean" | "undefined" @@ -384,6 +385,7 @@ export type ExcelInputOptionsBrowser = { method?: string, headers?: any, frameConfig?: BaseDataOptionType + parsingOptions?: ParsingOptions } export type JsonInputOptionsBrowser = { method?: string, @@ -400,6 +402,7 @@ export type ExcelInputOptionsNode = { method?: string, headers?: HeadersInit frameConfig?: BaseDataOptionType + parsingOptions?: ParsingOptions } export type JsonInputOptionsNode = { method?: string, @@ -408,9 +411,9 @@ export type JsonInputOptionsNode = { } export type CsvOutputOptionsBrowser = { fileName?: string, sep?: string, header?: boolean, download?: boolean }; -export type ExcelOutputOptionsBrowser = { fileName?: string, sheetName?: string }; +export type ExcelOutputOptionsBrowser = { fileName?: string, sheetName?: string, writingOptions: WritingOptions }; export type JsonOutputOptionsBrowser = { fileName?: string, format?: "row" | "column", download?: boolean }; export type CsvOutputOptionsNode = { filePath?: string, sep?: string, header?: boolean } export type JsonOutputOptionsNode = { format?: "row" | "column", filePath?: string } -export type ExcelOutputOptionsNode = { filePath?: string, sheetName?: string } +export type ExcelOutputOptionsNode = { filePath?: string, sheetName?: string, writingOptions: WritingOptions } From ca671ca8042cff5cd413758854f728446d94999e Mon Sep 17 00:00:00 2001 From: Rising Odegua Date: Sun, 25 Sep 2022 12:13:06 +0100 Subject: [PATCH 2/2] update types --- src/danfojs-base/shared/types.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/danfojs-base/shared/types.ts b/src/danfojs-base/shared/types.ts index 29b74d73..d89be170 100644 --- a/src/danfojs-base/shared/types.ts +++ b/src/danfojs-base/shared/types.ts @@ -411,9 +411,9 @@ export type JsonInputOptionsNode = { } export type CsvOutputOptionsBrowser = { fileName?: string, sep?: string, header?: boolean, download?: boolean }; -export type ExcelOutputOptionsBrowser = { fileName?: string, sheetName?: string, writingOptions: WritingOptions }; +export type ExcelOutputOptionsBrowser = { fileName?: string, sheetName?: string, writingOptions?: WritingOptions }; export type JsonOutputOptionsBrowser = { fileName?: string, format?: "row" | "column", download?: boolean }; export type CsvOutputOptionsNode = { filePath?: string, sep?: string, header?: boolean } export type JsonOutputOptionsNode = { format?: "row" | "column", filePath?: string } -export type ExcelOutputOptionsNode = { filePath?: string, sheetName?: string, writingOptions: WritingOptions } +export type ExcelOutputOptionsNode = { filePath?: string, sheetName?: string, writingOptions?: WritingOptions }