Skip to content

Commit c5c0a0a

Browse files
committed
Fix issue #390
1 parent d3df3a3 commit c5c0a0a

6 files changed

Lines changed: 84 additions & 60 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-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/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/series.test.ts

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

2216
it("throw error when row specified is less than 0", function () {
2317
const data = [1, 2, 3, 4, 5, 620, 30, 40, 39, 89, 78];
2418
const sf = new Series(data);
25-
assert.throws(function () { assert.deepEqual(sf.head(-1).values, data) }, Error, `ParamError: end must be greater than start`);
19+
assert.throws(function () { assert.deepEqual(sf.head(-1).values, data) }, Error, `ParamError: Number of rows cannot be less than 1`);
2620
});
21+
it("Returns all rows when DataFrame length is less than rows", function () {
22+
const data = [ 1, 2, 5 ];
23+
const sf = new Series(data);
24+
assert.deepEqual(sf.head().values, data);
25+
26+
});
2727
});
2828

2929

@@ -35,19 +35,20 @@ describe("Series Functions", () => {
3535
assert.deepEqual(sf.tail(4).values, [40, 39, 89, 78]);
3636

3737
});
38-
it("throw error when row specified is greater than values", function () {
39-
const data = ["Boy", "Girl", "Man", "Woman", "Tall"];
40-
const cols = ["Items"];
41-
const sf = new Series(data, { columns: cols });
42-
assert.throws(function () { assert.deepEqual(sf.tail(15).values, data) }, Error, `row slice [start] index cannot be less than 0`);
43-
});
4438

4539
it("throw error when row specified is less than 0", function () {
4640
const data = ["Boy", "Girl", "Man", "Woman", "Tall"];
4741
const cols = ["Items"];
4842
const sf = new Series(data, { columns: cols });
49-
assert.throws(function () { assert.deepEqual(sf.tail(-1).values, data) }, Error, `ParamError: end must be greater than start`);
43+
assert.throws(function () { assert.deepEqual(sf.tail(-1).values, data) }, Error, `ParamError: Number of rows cannot be less than 1`);
5044
});
45+
46+
it("Returns all rows when DataFrame length is less than rows", function () {
47+
const data = [ 1, 2, 5 ];
48+
const sf = new Series(data);
49+
assert.deepEqual(sf.tail().values, data);
50+
51+
});
5152
});
5253

5354
describe("sample", function () {

0 commit comments

Comments
 (0)