Skip to content

Commit b97bc7d

Browse files
committed
Add feature for allowing empty dataframes with column information (issue #412)
1 parent 9b7a7f4 commit b97bc7d

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

src/danfojs-base/core/generic.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ export default class NDframe implements NDframeInterface {
7272
}
7373

7474
if (data === undefined || (Array.isArray(data) && data.length === 0)) {
75-
this.loadArrayIntoNdframe({ data: [], index: [], columns: [], dtypes: [] });
75+
if (columns === undefined) columns = [];
76+
if (dtypes === undefined) dtypes = [];
77+
if (columns.length === 0 && dtypes.length !== 0) ErrorThrower.throwDtypeWithoutColumnError();
78+
this.loadArrayIntoNdframe({ data: [], index: [], columns: columns, dtypes: dtypes });
7679
} else if (utils.is1DArray(data)) {
7780
this.loadArrayIntoNdframe({ data, index, columns, dtypes });
7881
} else {
@@ -306,6 +309,7 @@ export default class NDframe implements NDframeInterface {
306309
*/
307310
$setColumnNames(columns?: string[]) {
308311

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

323327
ErrorThrower.throwColumnNamesLengthError(this, columns)
324328
}
325-
if (Array.from(new Set(columns)).length !== this.shape[1]) {
329+
if (Array.from(new Set(columns)).length !== columns.length) {
326330
ErrorThrower.throwColumnDuplicateError()
327331
}
328332

@@ -337,7 +341,10 @@ export default class NDframe implements NDframeInterface {
337341
* Returns the shape of the NDFrame. Shape is determined by [row length, column length]
338342
*/
339343
get shape(): Array<number> {
340-
if (this.$data.length === 0) return [0, 0]
344+
if (this.$data.length === 0) {
345+
if (this.$columns.length === 0) return [0, 0];
346+
else return [0, this.$columns.length];
347+
}
341348
if (this.$isSeries) {
342349
return [this.$data.length, 1];
343350
} else {

src/danfojs-base/shared/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class ErrorThrower {
5151
throw new Error(msg)
5252
}
5353

54+
throwDtypeWithoutColumnError = (): void => {
55+
const msg = `DtypeError: columns parameter must be provided when dtypes parameter is provided`
56+
throw new Error(msg)
57+
}
58+
5459
throwColumnLengthError = (ndframe: NDframe | DataFrame, arrLen: number): void => {
5560
const msg = `ParamError: Column data length mismatch. You provided data with length ${arrLen} but Ndframe has column of length ${ndframe.shape[1]}`
5661
throw new Error(msg)

0 commit comments

Comments
 (0)