Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
21 changes: 15 additions & 6 deletions src/danfojs-base/core/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,12 +525,16 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
* ```
*/
head(rows: number = 5): DataFrame {
if (rows > this.shape[0]) {
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
}

if (rows <= 0) {
throw new Error("ParamError: Number of rows cannot be less than 1")
}
if (this.shape[0] <= rows) {
return this.copy()
}
if (this.shape[0] - rows < 0) {
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
}

return this.iloc({ rows: [`0:${rows}`] })
}
Expand All @@ -545,12 +549,17 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
* ```
*/
tail(rows: number = 5): any {
if (rows > this.shape[0]) {
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
}

if (rows <= 0) {
throw new Error("ParamError: Number of rows cannot be less than 1")
}
if (this.shape[0] <= rows) {
return this.copy()
}
if (this.shape[0] - rows < 0) {
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
}

rows = this.shape[0] - rows
return this.iloc({ rows: [`${rows}:`] })
}
Expand Down
19 changes: 19 additions & 0 deletions src/danfojs-base/core/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ export default class Series extends NDframe implements SeriesInterface {
* ```
*/
head(rows: number = 5): Series {
if (rows <= 0) {
throw new Error("ParamError: Number of rows cannot be less than 1")
}
if (this.shape[0] <= rows) {
return this.copy()
}
if (this.shape[0] - rows < 0) {
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
}
return this.iloc([`0:${rows}`])
}

Expand All @@ -176,6 +185,16 @@ export default class Series extends NDframe implements SeriesInterface {
* ```
*/
tail(rows: number = 5): Series {
if (rows <= 0) {
throw new Error("ParamError: Number of rows cannot be less than 1")
}
if (this.shape[0] <= rows) {
return this.copy()
}
if (this.shape[0] - rows < 0) {
throw new Error("ParamError: Number of rows cannot be greater than available rows in data")
}

const startIdx = this.shape[0] - rows
return this.iloc([`${startIdx}:`])
}
Expand Down
3 changes: 2 additions & 1 deletion src/danfojs-base/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,8 @@ export default class Utils {
if (
typeof arr[0] == "number" ||
typeof arr[0] == "string" ||
typeof arr[0] == "boolean"
typeof arr[0] == "boolean" ||
arr[0] === null
) {
return true;
} else {
Expand Down
25 changes: 12 additions & 13 deletions src/danfojs-browser/tests/core/frame.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,19 @@ describe("DataFrame", function () {
const df = new dfd.DataFrame(data, { columns: cols });
assert.deepEqual(df.head(2).values, [ [ 1, 2, 3 ], [ 4, 5, 6 ] ]);
});
it("Throws error if row specified is greater than values", function () {
it("Throws error if row specified is less than 0", function () {
const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
const cols = [ "A", "B", "C" ];
const df = new dfd.DataFrame(data, { columns: cols });
assert.throws(() => df.head(10), Error,
"ParamError: Number of rows cannot be greater than available rows in data");
assert.throws(() => df.head(-1), Error,
"ParamError: Number of rows cannot be less than 1");
});
it("Throws error if row specified is less than 0", function () {

it("Returns all rows when size of DataFrame is less than default (5)", function () {
const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
const cols = [ "A", "B", "C" ];
const df = new dfd.DataFrame(data, { columns: cols });
assert.throws(() => df.head(-1), Error,
"ParamError: Number of rows cannot be less than 1");
assert.deepEqual(df.head().values, data);
});

});
Expand All @@ -328,13 +328,6 @@ describe("DataFrame", function () {
const df = new dfd.DataFrame(data, { columns: cols });
assert.deepEqual(df.tail(2).values, [ [ 20, 30, 40 ], [ 39, 89, 78 ] ]);
});
it("Throws error if row specified is greater than values", function () {
const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
const cols = [ "A", "B", "C" ];
const df = new dfd.DataFrame(data, { columns: cols });
assert.throws(() => df.tail(10), Error,
"ParamError: Number of rows cannot be greater than available rows in data");
});
it("Throws error if row specified is less than 0", function () {
const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
const cols = [ "A", "B", "C" ];
Expand All @@ -353,6 +346,12 @@ describe("DataFrame", function () {
const df = new dfd.DataFrame(data);
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 ] ]);
});
it("Returns all rows when size of DataFrame is less than default (5)", function () {
const data = [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 20, 30, 40 ], [ 39, 89, 78 ] ];
const cols = [ "A", "B", "C" ];
const df = new dfd.DataFrame(data, { columns: cols });
assert.deepEqual(df.tail().values, data);
});
});

describe("sample", function () {
Expand Down
13 changes: 13 additions & 0 deletions src/danfojs-browser/tests/core/generic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,19 @@ describe("Generic (NDFrame)", function () {
"IndexError: Row index must contain unique values";
});
});

it("Successfully create a 2D Frame when first value is empty", function () {
let data = [ [ null, 20, 1 ], [ 20, 25, 3 ] ];
let ndframe = new dfd.NDframe({ data, isSeries: false });
//@ts-ignore
assert.deepEqual(ndframe.values, data);
});
it("Successfully create a 1D Frame when first value is empty", function () {
let data = [ null, 'bval2', 'bval3', 'bval4' ];
let ndframe = new dfd.NDframe({ data, isSeries: true });
//@ts-ignore
assert.deepEqual(ndframe.values, data);
});
});

describe("NDframe Created from JavaScript Object", function () {
Expand Down
26 changes: 12 additions & 14 deletions src/danfojs-browser/tests/core/series.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,16 @@ describe("Series Functions", () => {
assert.deepEqual(sf.head(2).values, [ 1, 2 ]);
assert.deepEqual(sf.head(5).values, [ 1, 2, 3, 4, 5 ]);
});
it("throw error when row specified is greater than values", function () {
const data = [ "Boy", "Girl", "Man", "Woman", "Tall" ];
const cols = [ "Items" ];
const sf = new dfd.Series(data, { columns: cols });
assert.throws(function () { assert.deepEqual(sf.head(10).values, data); }, Error, `row slice [end] index cannot be bigger than 5`);
});
it("Returns all rows when DataFrame length is less than rows", function () {
const data = [ 1, 2, 5 ];
const sf = new dfd.Series(data);
assert.deepEqual(sf.head().values, data);

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

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

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

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

Expand Down
24 changes: 11 additions & 13 deletions src/danfojs-node/test/core/frame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,19 +308,18 @@ describe("DataFrame", function () {
const df = new DataFrame(data, { columns: cols });
assert.deepEqual(df.head(2).values, [[1, 2, 3], [4, 5, 6]]);
});
it("Throws error if row specified is greater than values", function () {
it("Throws error if row specified is less than 0", function () {
const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
const cols = ["A", "B", "C"];
const df = new DataFrame(data, { columns: cols });
assert.throws(() => df.head(10), Error,
"ParamError: Number of rows cannot be greater than available rows in data");
assert.throws(() => df.head(-1), Error,
"ParamError: Number of rows cannot be less than 1");
});
it("Throws error if row specified is less than 0", function () {
it("Returns all rows when size of DataFrame is less than default (5)", function () {
const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
const cols = ["A", "B", "C"];
const df = new DataFrame(data, { columns: cols });
assert.throws(() => df.head(-1), Error,
"ParamError: Number of rows cannot be less than 1");
assert.deepEqual(df.head().values, data);
});

});
Expand All @@ -332,13 +331,6 @@ describe("DataFrame", function () {
const df = new DataFrame(data, { columns: cols });
assert.deepEqual(df.tail(2).values, [[20, 30, 40], [39, 89, 78]]);
});
it("Throws error if row specified is greater than values", function () {
const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
const cols = ["A", "B", "C"];
const df = new DataFrame(data, { columns: cols });
assert.throws(() => df.tail(10), Error,
"ParamError: Number of rows cannot be greater than available rows in data");
});
it("Throws error if row specified is less than 0", function () {
const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
const cols = ["A", "B", "C"];
Expand All @@ -357,6 +349,12 @@ describe("DataFrame", function () {
const df = new DataFrame(data);
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]]);
});
it("Returns all rows when size of DataFrame is less than default (5)", function () {
const data = [[1, 2, 3], [4, 5, 6], [20, 30, 40], [39, 89, 78]];
const cols = ["A", "B", "C"];
const df = new DataFrame(data, { columns: cols });
assert.deepEqual(df.tail().values, data);
});
});

describe("sample", function () {
Expand Down
77 changes: 13 additions & 64 deletions src/danfojs-node/test/core/generic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,19 @@ describe("Generic (NDFrame)", function () {
"IndexError: Row index must contain unique values"
});
});

it("Successfully create a 2D Frame when first value is empty", function () {
let data = [[null, 20, 1], [20, 25, 3]];
let ndframe = new NDframe({ data, isSeries: false });
//@ts-ignore
assert.deepEqual(ndframe.values, data);
});
it("Successfully create a 1D Frame when first value is empty", function () {
let data = [null, 'bval2', 'bval3', 'bval4'];
let ndframe = new NDframe({ data, isSeries: true });
//@ts-ignore
assert.deepEqual(ndframe.values, data);
});
})

describe("NDframe Created from JavaScript Object", function () {
Expand Down Expand Up @@ -123,70 +136,6 @@ describe("Generic (NDFrame)", function () {
let ndframe = new NDframe({ data, isSeries: false });
assert.deepEqual(ndframe.values as any, [["A", NaN], [NaN, 2]]);
});
// it("NDframe created from json takes key position into consideration", function () {
// let json_data = [{ A: "A", B: "B", C: "C" },
// { A: "A", B: "B", C: "C" },
// { C: "C", B: "B", A: "A" },
// { A: "A", C: "C", B: "B" }];

// let output = [
// [
// 'A',
// 'B',
// 'C'
// ],
// [
// 'A',
// 'B',
// 'C'
// ],
// [
// 'A',
// 'B',
// 'C'
// ],
// [
// 'A',
// 'B',
// 'C'
// ]
// ];
// let ndframe = new NDframe({ data: json_data, isSeries: false });
// assert.deepEqual(ndframe.values, output);
// });

// it("NDframe created from json sets value to NaN if not present", function () {
// let json_data = [{ A: "A", B: "B", C: "C" },
// { A: "A", B: "B", C: "C" },
// { C: "C", B: "B", A: "A" },
// { A: "A", C: "C" }];

// let output = [
// [
// 'A',
// 'B',
// 'C'
// ],
// [
// 'A',
// 'B',
// 'C'
// ],
// [
// 'A',
// 'B',
// 'C'
// ],
// [
// 'A',
// 'B',
// NaN
// ]
// ];
// let ndframe = new NDframe({ data: json_data, isSeries: false });
// assert.deepEqual(ndframe.values, output);
// });

});

describe("Replacing row data", function () {
Expand Down
Loading