8000 Fix empty dataframe not adding columns information by igonro · Pull Request #417 · javascriptdata/danfojs · GitHub
[go: up one dir, main page]

Skip to content

Fix empty dataframe not adding columns information #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add feature for allowing empty dataframes with column information (is…
…sue #412)
  • Loading branch information
igonro committed Mar 8, 2022
commit b97bc7da8b7f757f030b193a71439213c583388c
13 changes: 10 additions & 3 deletions src/danfojs-base/core/generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ export default class NDframe implements NDframeInterface {
}

if (data === undefined || (Array.isArray(data) && data.length === 0)) {
this.loadArrayIntoNdframe({ data: [], index: [], columns: [], dtypes: [] });
if (columns === undefined) columns = [];
if (dtypes === undefined) dtypes = [];
if (columns.length === 0 && dtypes.length !== 0) ErrorThrower.throwDtypeWithoutColumnError();
this.loadArrayIntoNdframe({ data: [], index: [], columns: columns, dtypes: dtypes });
} else if (utils.is1DArray(data)) {
this.loadArrayIntoNdframe({ data, index, columns, dtypes });
} else {
Expand Down Expand Up @@ -306,6 +309,7 @@ export default class NDframe implements NDframeInterface {
*/
$setColumnNames(columns?: string[]) {

// console.log(columns);
if (this.$isSeries) {
if (columns) {
if (this.$data.length != 0 && columns.length != 1 && typeof columns != 'string') {
Expand All @@ -322,7 +326,7 @@ export default class NDframe implements NDframeInterface {

ErrorThrower.throwColumnNamesLengthError(this, columns)
}
if (Array.from(new Set(columns)).length !== this.shape[1]) {
if (Array.from(new Set(columns)).length !== columns.length) {
ErrorThrower.throwColumnDuplicateError()
}

Expand All @@ -337,7 +341,10 @@ export default class NDframe implements NDframeInterface {
* Returns the shape of the NDFrame. Shape is determined by [row length, column length]
*/
get shape(): Array<number> {
if (this.$data.length === 0) return [0, 0]
if (this.$data.length === 0) {
if (this.$columns.length === 0) return [0, 0];
else return [0, this.$columns.length];
}
if (this.$isSeries) {
return [this.$data.length, 1];
} else {
Expand Down
5 changes: 5 additions & 0 deletions src/danfojs-base/shared/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class ErrorThrower {
throw new Error(msg)
}

throwDtypeWithoutColumnError = (): void => {
const msg = `DtypeError: columns parameter must be provided when dtypes parameter is provided`
throw new Error(msg)
}

throwColumnLengthError = (ndframe: NDframe | DataFrame, arrLen: number): void => {
const msg = `ParamError: Column data length mismatch. You provided data with length ${arrLen} but Ndframe has column of length ${ndframe.shape[1]}`
throw new Error(msg)
Expand Down
0