Skip to content

Commit bdf0a24

Browse files
authored
Merge pull request #395 from javascriptdata/bug-fixes
Bug fixes
2 parents 44f7607 + a919675 commit bdf0a24

12 files changed

Lines changed: 112 additions & 125 deletions

File tree

src/danfojs-base/core/frame.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -525,12 +525,16 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
525525
* ```
526526
*/
527527
head(rows: number = 5): DataFrame {
528-
if (rows > this.shape[0]) {
529-
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
530-
}
528+
531529
if (rows <= 0) {
532530
throw new Error("ParamError: Number of rows cannot be less than 1")
533531
}
532+
if (this.shape[0] <= rows) {
533+
return this.copy()
534+
}
535+
if (this.shape[0] - rows < 0) {
536+
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
537+
}
534538

535539
return this.iloc({ rows: [`0:${rows}`] })
536540
}
@@ -545,12 +549,17 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
545549
* ```
546550
*/
547551
tail(rows: number = 5): any {
548-
if (rows > this.shape[0]) {
549-
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
550-
}
552+
551553
if (rows <= 0) {
552554
throw new Error("ParamError: Number of rows cannot be less than 1")
553555
}
556+
if (this.shape[0] <= rows) {
557+
return this.copy()
558+
}
559+
if (this.shape[0] - rows < 0) {
560+
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
561+
}
562+
554563
rows = this.shape[0] - rows
555564
return this.iloc({ rows: [`${rows}:`] })
556565
}

src/danfojs-base/core/series.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ export default class Series extends NDframe implements SeriesInterface {
162162
* ```
163163
*/
164164
head(rows: number = 5): Series {
165+
if (rows <= 0) {
166+
throw new Error("ParamError: Number of rows cannot be less than 1")
167+
}
168+
if (this.shape[0] <= rows) {
169+
return this.copy()
170+
}
171+
if (this.shape[0] - rows < 0) {
172+
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
173+
}
165174
return this.iloc([`0:${rows}`])
166175
}
167176

@@ -176,6 +185,16 @@ export default class Series extends NDframe implements SeriesInterface {
176185
* ```
177186
*/
178187
tail(rows: number = 5): Series {
188+
if (rows <= 0) {
189+
throw new Error("ParamError: Number of rows cannot be less than 1")
190+
}
191+
if (this.shape[0] <= rows) {
192+
return this.copy()
193+
}
194+
if (this.shape[0] - rows < 0) {
195+
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
196+
}
197+
179198
const startIdx = this.shape[0] - rows
180199
return this.iloc([`${startIdx}:`])
181200
}

src/danfojs-base/shared/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ export default class Utils {
334334
if (
335335
typeof arr[0] == "number" ||
336336
typeof arr[0] == "string" ||
337-
typeof arr[0] == "boolean"
337+
typeof arr[0] == "boolean" ||
338+
arr[0] === null
338339
) {
339340
return true;
340341
} else {

src/danfojs-browser/tests/core/frame.test.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,19 @@ describe("DataFrame", function () {
304304
const df = new dfd.DataFrame(data, { columns: cols });
305305
assert.deepEqual(df.head(2).values, [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]);
306306
});
307-
it("Throws error if row specified is greater than values", function () {
307+
it("Throws error if row specified is less than 0", function () {
308308
const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
309309
const cols = [ "A", "B", "C" ];
310310
const df = new dfd.DataFrame(data, { columns: cols });
311-
assert.throws(() => df.head(10), Error,
312-
"ParamError: Number of rows cannot be greater than available rows in data");
311+
assert.throws(() => df.head(-1), Error,
312+
"ParamError: Number of rows cannot be less than 1");
313313
});
314-
it("Throws error if row specified is less than 0", function () {
314+
315+
it("Returns all rows when size of DataFrame is less than default (5)", function () {
315316
const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
316317
const cols = [ "A", "B", "C" ];
317318
const df = new dfd.DataFrame(data, { columns: cols });
318-
assert.throws(() => df.head(-1), Error,
319-
"ParamError: Number of rows cannot be less than 1");
319+
assert.deepEqual(df.head().values, data);
320320
});
321321

322322
});
@@ -328,13 +328,6 @@ describe("DataFrame", function () {
328328
const df = new dfd.DataFrame(data, { columns: cols });
329329
assert.deepEqual(df.tail(2).values, [ [ 20, 30, 40 ], [ 39, 89, 78 ] ]);
330330
});
331-
it("Throws error if row specified is greater than values", function () {
332-
const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
333-
const cols = [ "A", "B", "C" ];
334-
const df = new dfd.DataFrame(data, { columns: cols });
335-
assert.throws(() => df.tail(10), Error,
336-
"ParamError: Number of rows cannot be greater than available rows in data");
337-
});
338331
it("Throws error if row specified is less than 0", function () {
339332
const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
340333
const cols = [ "A", "B", "C" ];
@@ -353,6 +346,12 @@ describe("DataFrame", function () {
353346
const df = new dfd.DataFrame(data);
354347
assert.deepEqual(df.tail(2).values, [ [ 1, 2, 34, 5, 0, 6, 4, 5, 6, 7 ], [ 20, 30, 40, 39, 89, 78, 45, 56, 56, 45 ] ]);
355348
});
349+
it("Returns all rows when size of DataFrame is less than default (5)", function () {
350+
const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
351+
const cols = [ "A", "B", "C" ];
352+
const df = new dfd.DataFrame(data, { columns: cols });
353+
assert.deepEqual(df.tail().values, data);
354+
});
356355
});
357356

358357
describe("sample", function () {

src/danfojs-browser/tests/core/generic.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ describe("Generic (NDFrame)", function () {
7474
"IndexError: Row index must contain unique values";
7575
});
7676
});
77+
78+
it("Successfully create a 2D Frame when first value is empty", function () {
79+
let data = [ [ null, 20, 1 ], [ 20, 25, 3 ] ];
80+
let ndframe = new dfd.NDframe({ data, isSeries: false });
81+
//@ts-ignore
82+
assert.deepEqual(ndframe.values, data);
83+
});
84+
it("Successfully create a 1D Frame when first value is empty", function () {
85+
let data = [ null, 'bval2', 'bval3', 'bval4' ];
86+
let ndframe = new dfd.NDframe({ data, isSeries: true });
87+
//@ts-ignore
88+
assert.deepEqual(ndframe.values, data);
89+
});
7790
});
7891

7992
describe("NDframe Created from JavaScript Object", function () {

src/danfojs-browser/tests/core/series.test.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@ describe("Series Functions", () => {
1010
assert.deepEqual(sf.head(2).values, [ 1, 2 ]);
1111
assert.deepEqual(sf.head(5).values, [ 1, 2, 3, 4, 5 ]);
1212
});
13-
it("throw error when row specified is greater than values", function () {
14-
const data = [ "Boy", "Girl", "Man", "Woman", "Tall" ];
15-
const cols = [ "Items" ];
16-
const sf = new dfd.Series(data, { columns: cols });
17-
assert.throws(function () { assert.deepEqual(sf.head(10).values, data); }, Error, `row slice [end] index cannot be bigger than 5`);
18-
});
13+
it("Returns all rows when DataFrame length is less than rows", function () {
14+
const data = [ 1, 2, 5 ];
15+
const sf = new dfd.Series(data);
16+
assert.deepEqual(sf.head().values, data);
1917

18+
});
2019
it("throw error when row specified is less than 0", function () {
2120
const data = [ 1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78 ];
2221
const sf = new dfd.Series(data);
23-
assert.throws(function () { assert.deepEqual(sf.head(-1).values, data); }, Error, `ParamError: end must be greater than start`);
22+
assert.throws(function () { assert.deepEqual(sf.head(-1).values, data); }, Error, `ParamError: Number of rows cannot be less than 1`);
2423
});
2524
});
2625

@@ -33,18 +32,17 @@ describe("Series Functions", () => {
3332
assert.deepEqual(sf.tail(4).values, [ 40, 39, 89, 78 ]);
3433

3534
});
36-
it("throw error when row specified is greater than values", function () {
37-
const data = [ "Boy", "Girl", "Man", "Woman", "Tall" ];
38-
const cols = [ "Items" ];
39-
const sf = new dfd.Series(data, { columns: cols });
40-
assert.throws(function () { assert.deepEqual(sf.tail(15).values, data); }, Error, `row slice [start] index cannot be less than 0`);
41-
});
35+
it("Returns all rows when DataFrame length is less than rows", function () {
36+
const data = [ 1, 2, 5 ];
37+
const sf = new dfd.Series(data);
38+
assert.deepEqual(sf.tail().values, data);
4239

40+
});
4341
it("throw error when row specified is less than 0", function () {
4442
const data = [ "Boy", "Girl", "Man", "Woman", "Tall" ];
4543
const cols = [ "Items" ];
4644
const sf = new dfd.Series(data, { columns: cols });
47-
assert.throws(function () { assert.deepEqual(sf.tail(-1).values, data); }, Error, `ParamError: end must be greater than start`);
45+
assert.throws(function () { assert.deepEqual(sf.tail(-1).values, data); }, Error, `ParamError: Number of rows cannot be less than 1`);
4846
});
4947
});
5048

src/danfojs-node/test/core/frame.test.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -308,19 +308,18 @@ describe("DataFrame", function () {
308308
const df = new DataFrame(data, { columns: cols });
309309
assert.deepEqual(df.head(2).values, [[1, 2, 3], [4, 5, 6]]);
310310
});
311-
it("Throws error if row specified is greater than values", function () {
311+
it("Throws error if row specified is less than 0", function () {
312312
const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
313313
const cols = ["A", "B", "C"];
314314
const df = new DataFrame(data, { columns: cols });
315-
assert.throws(() => df.head(10), Error,
316-
"ParamError: Number of rows cannot be greater than available rows in data");
315+
assert.throws(() => df.head(-1), Error,
316+
"ParamError: Number of rows cannot be less than 1");
317317
});
318-
it("Throws error if row specified is less than 0", function () {
318+
it("Returns all rows when size of DataFrame is less than default (5)", function () {
319319
const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
320320
const cols = ["A", "B", "C"];
321321
const df = new DataFrame(data, { columns: cols });
322-
assert.throws(() => df.head(-1), Error,
323-
"ParamError: Number of rows cannot be less than 1");
322+
assert.deepEqual(df.head().values, data);
324323
});
325324

326325
});
@@ -332,13 +331,6 @@ describe("DataFrame", function () {
332331
const df = new DataFrame(data, { columns: cols });
333332
assert.deepEqual(df.tail(2).values, [[20, 30, 40], [39, 89, 78]]);
334333
});
335-
it("Throws error if row specified is greater than values", function () {
336-
const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
337-
const cols = ["A", "B", "C"];
338-
const df = new DataFrame(data, { columns: cols });
339-
assert.throws(() => df.tail(10), Error,
340-
"ParamError: Number of rows cannot be greater than available rows in data");
341-
});
342334
it("Throws error if row specified is less than 0", function () {
343335
const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
344336
const cols = ["A", "B", "C"];
@@ -357,6 +349,12 @@ describe("DataFrame", function () {
357349
const df = new DataFrame(data);
358350
assert.deepEqual(df.tail(2).values, [[1, 2, 34, 5, 0, 6, 4, 5, 6, 7], [20, 30, 40, 39, 89, 78, 45, 56, 56, 45]]);
359351
});
352+
it("Returns all rows when size of DataFrame is less than default (5)", function () {
353+
const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
354+
const cols = ["A", "B", "C"];
355+
const df = new DataFrame(data, { columns: cols });
356+
assert.deepEqual(df.tail().values, data);
357+
});
360358
});
361359

362360
describe("sample", function () {

src/danfojs-node/test/core/generic.test.ts

Lines changed: 13 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,19 @@ describe("Generic (NDFrame)", function () {
7777
"IndexError: Row index must contain unique values"
7878
});
7979
});
80+
81+
it("Successfully create a 2D Frame when first value is empty", function () {
82+
let data = [[null, 20, 1], [20, 25, 3]];
83+
let ndframe = new NDframe({ data, isSeries: false });
84+
//@ts-ignore
85+
assert.deepEqual(ndframe.values, data);
86+
});
87+
it("Successfully create a 1D Frame when first value is empty", function () {
88+
let data = [null, 'bval2', 'bval3', 'bval4'];
89+
let ndframe = new NDframe({ data, isSeries: true });
90+
//@ts-ignore
91+
assert.deepEqual(ndframe.values, data);
92+
});
8093
})
8194

8295
describe("NDframe Created from JavaScript Object", function () {
@@ -123,70 +136,6 @@ describe("Generic (NDFrame)", function () {
123136
let ndframe = new NDframe({ data, isSeries: false });
124137
assert.deepEqual(ndframe.values as any, [["A", NaN], [NaN, 2]]);
125138
});
126-
// it("NDframe created from json takes key position into consideration", function () {
127-
// let json_data = [{ A: "A", B: "B", C: "C" },
128-
// { A: "A", B: "B", C: "C" },
129-
// { C: "C", B: "B", A: "A" },
130-
// { A: "A", C: "C", B: "B" }];
131-
132-
// let output = [
133-
// [
134-
// 'A',
135-
// 'B',
136-
// 'C'
137-
// ],
138-
// [
139-
// 'A',
140-
// 'B',
141-
// 'C'
142-
// ],
143-
// [
144-
// 'A',
145-
// 'B',
146-
// 'C'
147-
// ],
148-
// [
149-
// 'A',
150-
// 'B',
151-
// 'C'
152-
// ]
153-
// ];
154-
// let ndframe = new NDframe({ data: json_data, isSeries: false });
155-
// assert.deepEqual(ndframe.values, output);
156-
// });
157-
158-
// it("NDframe created from json sets value to NaN if not present", function () {
159-
// let json_data = [{ A: "A", B: "B", C: "C" },
160-
// { A: "A", B: "B", C: "C" },
161-
// { C: "C", B: "B", A: "A" },
162-
// { A: "A", C: "C" }];
163-
164-
// let output = [
165-
// [
166-
// 'A',
167-
// 'B',
168-
// 'C'
169-
// ],
170-
// [
171-
// 'A',
172-
// 'B',
173-
// 'C'
174-
// ],
175-
// [
176-
// 'A',
177-
// 'B',
178-
// 'C'
179-
// ],
180-
// [
181-
// 'A',
182-
// 'B',
183-
// NaN
184-
// ]
185-
// ];
186-
// let ndframe = new NDframe({ data: json_data, isSeries: false });
187-
// assert.deepEqual(ndframe.values, output);
188-
// });
189-
190139
});
191140

192141
describe("Replacing row data", function () {

0 commit comments

Comments
 (0)