Skip to content
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

Fix empty dataframe not adding columns information #417

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
60 changes: 59 additions & 1 deletion src/danfojs-browser/tests/core/generic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,14 +407,24 @@ describe("Generic (NDFrame)", function () {
describe("Empty NDFrame", function () {
it("Can successfully create an empty NDframe from empty array", function () {
let data = [];
let sf = new dfd.NDframe({ data, isSeries: false });
let df = new dfd.NDframe({ data, isSeries: false });
assert.deepEqual(df.shape, [ 0, 0 ]);
assert.deepEqual(df.columns, []);
assert.deepEqual(df.dtypes, []);
assert.deepEqual(df.values, []);
let sf = new dfd.NDframe({ data, isSeries: true });
assert.deepEqual(sf.shape, [ 0, 0 ]);
assert.deepEqual(sf.columns, []);
assert.deepEqual(sf.dtypes, []);
assert.deepEqual(sf.values, []);
});
it("Can successfully create an empty NDframe from undefined data", function () {
let data = undefined;
let df = new dfd.NDframe({ data, isSeries: false });
assert.deepEqual(df.shape, [ 0, 0 ]);
assert.deepEqual(df.columns, []);
assert.deepEqual(df.dtypes, []);
assert.deepEqual(df.values, []);
let sf = new dfd.NDframe({ data, isSeries: true });
assert.deepEqual(sf.shape, [ 0, 0 ]);
assert.deepEqual(sf.columns, []);
Expand All @@ -423,12 +433,60 @@ describe("Generic (NDFrame)", function () {
});

it("Can successfully create an empty NDframe", function () {
let df = new dfd.NDframe({ isSeries: false });
assert.deepEqual(df.shape, [ 0, 0 ]);
assert.deepEqual(df.columns, []);
assert.deepEqual(df.dtypes, []);
assert.deepEqual(df.values, []);
let sf = new dfd.NDframe({ isSeries: true });
assert.deepEqual(sf.shape, [ 0, 0 ]);
assert.deepEqual(sf.columns, []);
assert.deepEqual(sf.dtypes, []);
assert.deepEqual(sf.values, []);
});

it("Can successfully create an empty NDframe with columns names", function () {
let data = [];
let df = new dfd.NDframe({ data, columns: [ "A", "B", "C" ], isSeries: false });
assert.deepEqual(df.shape, [ 0, 3 ]);
assert.deepEqual(df.columns, [ "A", "B", "C" ]);
assert.deepEqual(df.dtypes, []);
assert.deepEqual(df.values, []);
let sf = new dfd.NDframe({ data, columns: [ "A" ], isSeries: true });
assert.deepEqual(sf.shape, [ 0, 1 ]);
assert.deepEqual(sf.columns, [ "A" ]);
assert.deepEqual(sf.dtypes, []);
assert.deepEqual(sf.values, []);
});

it("Can successfully create an empty NDframe with columns names and dtypes", function () {
let data = [];
let df = new dfd.NDframe({ data, columns: [ "A", "B", "C" ], dtypes: [ "string", "string", "int32" ], isSeries: false });
assert.deepEqual(df.shape, [ 0, 3 ]);
assert.deepEqual(df.columns, [ "A", "B", "C" ]);
assert.deepEqual(df.dtypes, [ "string", "string", "int32" ]);
assert.deepEqual(df.values, []);
let sf = new dfd.NDframe({ data, columns: [ "A" ], dtypes: [ "string" ], isSeries: true });
assert.deepEqual(sf.shape, [ 0, 1 ]);
assert.deepEqual(sf.columns, [ "A" ]);
assert.deepEqual(sf.dtypes, [ "string" ]);
assert.deepEqual(sf.values, []);
});

it("Cannot successfully create an empty NDframe with only columns dtypes", function () {
let data = [];
assert.throws(
() => new dfd.NDframe({ data, dtypes: [ "string", "string", "int32" ], isSeries: false }),
Error,
"DtypeError: columns parameter must be provided when dtypes parameter is provided"
);
assert.throws(
() => new dfd.NDframe({ data, dtypes: [ "string" ], isSeries: true }),
Error,
"DtypeError: columns parameter must be provided when dtypes parameter is provided"
);
});

});

});
16 changes: 16 additions & 0 deletions src/danfojs-browser/tests/core/indexing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,22 @@ describe("Iloc and Loc based Indexing", function () {
assert.deepEqual(subDf.values, result);

});

it("loc with no matches create a Empty DataFrame conserving columns information", function () {
const data = {
"Name": [ "Apples", "Mango", "Banana", "Pear" ],
"Count": [ 21, 5, 30, 10 ],
"Price": [ 200, 300, 40, 250 ]
};
const df = new dfd.DataFrame(data);
const subDf = df.loc({ rows: df["Count"].gt(50) });

assert.deepEqual(subDf.values, []);
assert.deepEqual(subDf.shape, [ 0, 3 ]);
assert.deepEqual(subDf.columns, [ "Name", "Count", "Price" ]);
assert.deepEqual(subDf.dtypes, [ "string", "int32", "int32" ]);

});
});

});
60 changes: 59 additions & 1 deletion src/danfojs-node/test/core/generic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,14 +344,24 @@ describe("Generic (NDFrame)", function () {
describe("Empty NDFrame", function () {
it("Can successfully create an empty NDframe from empty array", function () {
let data: any = [];
let sf = new NDframe({ data, isSeries: false });
let df = new NDframe({ data, isSeries: false });
assert.deepEqual(df.shape, [0, 0]);
assert.deepEqual(df.columns, []);
assert.deepEqual(df.dtypes, []);
assert.deepEqual(df.values, []);
let sf = new NDframe({ data, isSeries: true });
assert.deepEqual(sf.shape, [0, 0]);
assert.deepEqual(sf.columns, []);
assert.deepEqual(sf.dtypes, []);
assert.deepEqual(sf.values, []);
});
it("Can successfully create an empty NDframe from undefined data", function () {
let data = undefined;
let df = new NDframe({ data, isSeries: false });
assert.deepEqual(df.shape, [0, 0]);
assert.deepEqual(df.columns, []);
assert.deepEqual(df.dtypes, []);
assert.deepEqual(df.values, []);
let sf = new NDframe({ data, isSeries: true });
assert.deepEqual(sf.shape, [0, 0]);
assert.deepEqual(sf.columns, []);
Expand All @@ -360,11 +370,59 @@ describe("Generic (NDFrame)", function () {
});

it("Can successfully create an empty NDframe", function () {
let df = new NDframe({ data: [], isSeries: false });
assert.deepEqual(df.shape, [0, 0]);
assert.deepEqual(df.columns, []);
assert.deepEqual(df.dtypes, []);
assert.deepEqual(df.values, []);
let sf = new NDframe({ data: [], isSeries: true });
assert.deepEqual(sf.shape, [0, 0]);
assert.deepEqual(sf.columns, []);
assert.deepEqual(sf.dtypes, []);
assert.deepEqual(sf.values, []);
});

it("Can successfully create an empty NDframe with columns names", function () {
let data: any = [];
let df = new NDframe({ data, columns: [ "A", "B", "C" ], isSeries: false });
assert.deepEqual(df.shape, [ 0, 3 ]);
assert.deepEqual(df.columns, [ "A", "B", "C" ]);
assert.deepEqual(df.dtypes, []);
assert.deepEqual(df.values, []);
let sf = new NDframe({ data, columns: [ "A" ], isSeries: true });
assert.deepEqual(sf.shape, [ 0, 1 ]);
assert.deepEqual(sf.columns, [ "A" ]);
assert.deepEqual(sf.dtypes, []);
assert.deepEqual(sf.values, []);
});

it("Can successfully create an empty NDframe with columns names and dtypes", function () {
let data: any = [];
let df = new NDframe({ data, columns: [ "A", "B", "C" ], dtypes: [ "string", "string", "int32" ], isSeries: false });
assert.deepEqual(df.shape, [ 0, 3 ]);
assert.deepEqual(df.columns, [ "A", "B", "C" ]);
assert.deepEqual(df.dtypes, [ "string", "string", "int32" ]);
assert.deepEqual(df.values, []);
let sf = new NDframe({ data, columns: [ "A" ], dtypes: [ "string" ], isSeries: true });
assert.deepEqual(sf.shape, [ 0, 1 ]);
assert.deepEqual(sf.columns, [ "A" ]);
assert.deepEqual(sf.dtypes, [ "string" ]);
assert.deepEqual(sf.values, []);
});

it("Cannot successfully create an empty NDframe with only columns dtypes", function () {
let data: any = [];
assert.throws(
() => new NDframe({ data, dtypes: [ "string", "string", "int32" ], isSeries: false }),
Error,
"DtypeError: columns parameter must be provided when dtypes parameter is provided"
);
assert.throws(
() => new NDframe({ data, dtypes: [ "string" ], isSeries: true }),
Error,
"DtypeError: columns parameter must be provided when dtypes parameter is provided"
);
});

});
});
17 changes: 17 additions & 0 deletions src/danfojs-node/test/core/indexing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,23 @@ describe("Iloc and Loc based Indexing", function () {
assert.deepEqual(subDf.values, result);

});

it("loc with no matches create a Empty DataFrame conserving columns information", function () {
const data = {
"Name": [ "Apples", "Mango", "Banana", "Pear" ],
"Count": [ 21, 5, 30, 10 ],
"Price": [ 200, 300, 40, 250 ]
};
const df = new DataFrame(data);
const subDf = df.loc({ rows: df["Count"].gt(50) });

assert.deepEqual(subDf.values, []);
assert.deepEqual(subDf.shape, [ 0, 3 ]);
assert.deepEqual(subDf.columns, [ "Name", "Count", "Price" ]);
assert.deepEqual(subDf.dtypes, [ "string", "int32", "int32" ]);

});

})

});