Skip to content

Commit

Permalink
Merge pull request #505 from javascriptdata/463-cant-parse-dates-from…
Browse files Browse the repository at this point in the history
…-excel-files-into-dataframe-properly

add support for excel parsing options arg
  • Loading branch information
risenW authored Sep 25, 2022
2 parents 201ddf7 + ca671ca commit 6d06a5c
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/danfojs-base/core/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
* ```
Expand Down
20 changes: 15 additions & 5 deletions src/danfojs-base/io/browser/io.excel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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"
Expand All @@ -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 {
Expand Down
20 changes: 15 additions & 5 deletions src/danfojs-base/io/node/io.excel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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"
Expand All @@ -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 {
Expand Down
7 changes: 2 additions & 5 deletions src/danfojs-base/shared/tensorflowlib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@

// This file is auto-generated by prebuild.js. Do not edit!
const tf = require("@tensorflow/tfjs")
export default tf

const tf = require("@tensorflow/tfjs-node")
export default tf
7 changes: 5 additions & 2 deletions src/danfojs-base/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -384,6 +385,7 @@ export type ExcelInputOptionsBrowser = {
method?: string,
headers?: any,
frameConfig?: BaseDataOptionType
parsingOptions?: ParsingOptions
}
export type JsonInputOptionsBrowser = {
method?: string,
Expand All @@ -400,6 +402,7 @@ export type ExcelInputOptionsNode = {
method?: string,
headers?: HeadersInit
frameConfig?: BaseDataOptionType
parsingOptions?: ParsingOptions
}
export type JsonInputOptionsNode = {
method?: string,
Expand All @@ -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 }

0 comments on commit 6d06a5c

Please sign in to comment.