Skip to content

Commit

Permalink
Merge pull request #212 from opensource9ja/fix/iloc-slicing
Browse files Browse the repository at this point in the history
Fix iloc slicing bug
  • Loading branch information
risenW authored May 30, 2021
2 parents ffc598e + 08d16c5 commit 7af31b0
Show file tree
Hide file tree
Showing 14 changed files with 282 additions and 124 deletions.
2 changes: 1 addition & 1 deletion danfojs-browser/lib/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion danfojs-browser/lib/bundle.js.map

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions danfojs-browser/src/core/indexing.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const indexLoc = (ndframe, kwargs) => {
if (Array.isArray(kwargs["rows"])) {

if (kwargs["rows"].length == 1 && typeof kwargs["rows"][0] == "string") {
//console.log("here", kwargs["rows"].length)
if (kwargs["rows"][0].includes(":")) {

let column_split = kwargs["rows"][0].split(":");
Expand Down Expand Up @@ -93,16 +92,24 @@ export const indexLoc = (ndframe, kwargs) => {
let column_split = kwargs["columns"][0].split(":");
let start, end;

if (kwargs["type"] == "iloc" || (column_split[0] == "")) {
start = parseInt(column_split[0]) || 0;
end = parseInt(column_split[1]) - 1 === 0 ? 0 : parseInt(column_split[1]) - 1;
} else {
if (kwargs["type"] == "iloc") {
if (column_split[0] == "") {
start = 0;
} else {
start = parseInt(column_split[0]) || 0;
}

if (column_split[1] == "") {
end = parseInt(ndframe.columns.length - 1);
} else {
end = parseInt(column_split[1]) - 1 === 0 ? 0 : parseInt(column_split[1]) - 1;
}

} else {
start = parseInt(ndframe.columns.indexOf(column_split[0]));
end = parseInt(ndframe.columns.indexOf(column_split[1])) - 1;
}


if (typeof start == "number" && typeof end == "number") {

columns = utils.__range(start, end);
Expand Down Expand Up @@ -136,7 +143,7 @@ export const indexLoc = (ndframe, kwargs) => {
throw new Error(`Specified row index ${row_val} is bigger than maximum row index of ${max_rowIndex}`);
}

if (Array.isArray(data_values[0])){
if (Array.isArray(data_values[0])) {

let value = data_values[row_val];
let row_data = [];
Expand Down
7 changes: 4 additions & 3 deletions danfojs-browser/src/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import NDframe from "./core/generic";
import * as tf from "@tensorflow/tfjs";

export { Series } from "./core/series";
export { DataFrame } from "./core/frame";
export { to_datetime } from "./core/timeseries";
export { read_csv, read_json, read_excel } from "./io/reader";
export { read_csv, read_json, read_excel, read } from "./io/reader";
export { merge } from "./core/merge";
export { concat } from "./core/concat";
export { LabelEncoder, OneHotEncoder } from "./preprocessing/encodings";
Expand All @@ -14,6 +15,6 @@ export { Configs } from "./config/config";
export { NDframe };
export { Str } from "./core/strings";
export { Utils } from "./core/utils";
export * as tf from "@tensorflow/tfjs";
export { tf };

export const _version = "0.2.5";
export const _version = "0.2.6";
62 changes: 62 additions & 0 deletions danfojs-browser/tests/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,68 @@ describe("DataFrame", function () {

});

it("column slice starting with 0 and returning a single result works", function () {
let data = {
"Name": [ "Apples", "Mango", "Banana", "Pear" ],
"Count": [ 21, 5, 30, 10 ],
"Price": [ 200, 300, 40, 250 ]
};
let df = new dfd.DataFrame(data);
let sub_df = df.iloc({ rows: [ "2:3" ], columns: [ "0:1" ] });
const result = [ [ "Banana" ] ];
assert.deepEqual(sub_df.values, result);

});
it("column slice with format '0:' works", function () {
let data = {
"Name": [ "Apples", "Mango", "Banana", "Pear" ],
"Count": [ 21, 5, 30, 10 ],
"Price": [ 200, 300, 40, 250 ]
};
let df = new dfd.DataFrame(data);
let sub_df = df.iloc({ rows: [ "2:3" ], columns: [ "0:" ] });
const result = [ [ "Banana", 30, 40 ] ];
assert.deepEqual(sub_df.values, result);

});
it("column slice with format ':2' works", function () {
let data = {
"Name": [ "Apples", "Mango", "Banana", "Pear" ],
"Count": [ 21, 5, 30, 10 ],
"Price": [ 200, 300, 40, 250 ]
};
let df = new dfd.DataFrame(data);
let sub_df = df.iloc({ rows: [ "2:3" ], columns: [ ":2" ] });
const result = [ [ "Banana", 30 ] ];
assert.deepEqual(sub_df.values, result);

});
it("row slice with format ':2' works", function () {
let data = {
"Name": [ "Apples", "Mango", "Banana", "Pear" ],
"Count": [ 21, 5, 30, 10 ],
"Price": [ 200, 300, 40, 250 ]
};
let df = new dfd.DataFrame(data);
let sub_df = df.iloc({ rows: [ ":2" ], columns: [ ":1" ] });
const result = [ [ 'Apples' ], [ 'Mango' ] ];
assert.deepEqual(sub_df.values, result);

});
it("row slice with format '1:' works", function () {
let data = {
"Name": [ "Apples", "Mango", "Banana", "Pear" ],
"Count": [ 21, 5, 30, 10 ],
"Price": [ 200, 300, 40, 250 ]
};
let df = new dfd.DataFrame(data);
let sub_df = df.iloc({ rows: [ ":2" ], columns: [ ":2" ] });
const result = [ [ 'Apples', 21 ], [ 'Mango', 5 ] ];
assert.deepEqual(sub_df.values, result);

});


});


Expand Down
12 changes: 12 additions & 0 deletions danfojs-browser/tests/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,18 @@ describe("Series", function () {

assert.deepEqual(sf.iloc([ "1:4" ]).values, expected_val);
});
it("indexing by slicing format ':5' works", function () {
let data = [ 1, 2, 3, 4, "a", "b", "c" ];
let sf = new dfd.Series(data);
let expected_val = [ 1, 2, 3, 4, 'a' ];
assert.deepEqual(sf.iloc([ ":5" ]).values, expected_val);
});
it("indexing by slicing format '2:` works", function () {
let data = [ 1, 2, 3, 4, "a", "b", "c" ];
let sf = new dfd.Series(data);
let expected_val = [ 3, 4, "a", "b", "c" ];
assert.deepEqual(sf.iloc([ "2:" ]).values, expected_val);
});
});

describe("append", function () {
Expand Down
16 changes: 12 additions & 4 deletions danfojs-node/dist/core/indexing.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,18 @@ const indexLoc = (ndframe, kwargs) => {
let column_split = kwargs["columns"][0].split(":");
let start, end;

if (kwargs["type"] == "iloc" || column_split[0] == "") {
start = parseInt(column_split[0]) || 0;
end = parseInt(column_split[1]) - 1 === 0 ? 0 : parseInt(column_split[1]) - 1;
console.log(start, end);
if (kwargs["type"] == "iloc") {
if (column_split[0] == "") {
start = 0;
} else {
start = parseInt(column_split[0]) || 0;
}

if (column_split[1] == "") {
end = parseInt(ndframe.columns.length - 1);
} else {
end = parseInt(column_split[1]) - 1 === 0 ? 0 : parseInt(column_split[1]) - 1;
}
} else {
start = parseInt(ndframe.columns.indexOf(column_split[0]));
end = parseInt(ndframe.columns.indexOf(column_split[1])) - 1;
Expand Down
7 changes: 4 additions & 3 deletions danfojs-node/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ exports.tf = exports._version = void 0;

var _generic = _interopRequireDefault(require("./core/generic"));

var tf = _interopRequireWildcard(require("@tensorflow/tfjs-node"));

exports.tf = tf;

var _series = require("./core/series");

var _frame = require("./core/frame");
Expand All @@ -151,8 +155,5 @@ var _strings = require("./core/strings");

var _utils = require("./core/utils");

var _tf = _interopRequireWildcard(require("@tensorflow/tfjs-node"));

exports.tf = _tf;
const _version = "0.2.6";
exports._version = _version;
2 changes: 1 addition & 1 deletion danfojs-node/dist/io/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const read = async (source, configs = {}) => {
let rows, file;

if ((0, _frictionless.isDataset)(source)) {
console.log("datapackage.json found. Loading Dataset package from Datahub.io");
console.info("datapackage.json found. Loading Dataset package from Datahub.io");
const dataset = await _frictionless.Dataset.load(source);
file = dataset.resources[data_num];
rows = await (0, _streamToArray.default)(await file.rows());
Expand Down
24 changes: 15 additions & 9 deletions danfojs-node/src/core/indexing.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const indexLoc = (ndframe, kwargs) => {
if (Array.isArray(kwargs["rows"])) {

if (kwargs["rows"].length == 1 && typeof kwargs["rows"][0] == "string") {
//console.log("here", kwargs["rows"].length)
if (kwargs["rows"][0].includes(":")) {

let column_split = kwargs["rows"][0].split(":");
Expand Down Expand Up @@ -93,17 +92,24 @@ export const indexLoc = (ndframe, kwargs) => {
let column_split = kwargs["columns"][0].split(":");
let start, end;

if (kwargs["type"] == "iloc" || (column_split[0] == "")) {
start = parseInt(column_split[0]) || 0;
end = parseInt(column_split[1]) - 1 === 0 ? 0 : parseInt(column_split[1]) - 1;
console.log(start, end);
} else {
if (kwargs["type"] == "iloc") {
if (column_split[0] == "") {
start = 0;
} else {
start = parseInt(column_split[0]) || 0;
}

if (column_split[1] == "") {
end = parseInt(ndframe.columns.length - 1);
} else {
end = parseInt(column_split[1]) - 1 === 0 ? 0 : parseInt(column_split[1]) - 1;
}

} else {
start = parseInt(ndframe.columns.indexOf(column_split[0]));
end = parseInt(ndframe.columns.indexOf(column_split[1])) - 1;
}


if (typeof start == "number" && typeof end == "number") {

columns = utils.__range(start, end);
Expand Down Expand Up @@ -137,7 +143,7 @@ export const indexLoc = (ndframe, kwargs) => {
throw new Error(`Specified row index ${row_val} is bigger than maximum row index of ${max_rowIndex}`);
}

if (Array.isArray(data_values[0])){
if (Array.isArray(data_values[0])) {

let value = data_values[row_val];
let row_data = [];
Expand Down Expand Up @@ -188,5 +194,5 @@ export const indexLoc = (ndframe, kwargs) => {
rows.forEach((i) => {
final_row.push(ndframe.index[i]);
});
return [ new_data, column_names, final_row ];
return [new_data, column_names, final_row];
};
3 changes: 2 additions & 1 deletion danfojs-node/src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import NDframe from "./core/generic";
import * as tf from "@tensorflow/tfjs-node";

export { Series } from "./core/series";
export { DataFrame } from "./core/frame";
Expand All @@ -14,6 +15,6 @@ export { Configs } from "./config/config";
export { NDframe };
export { Str } from "./core/strings";
export { Utils } from "./core/utils";
export * as tf from "@tensorflow/tfjs-node";
export { tf };

export const _version = "0.2.6";
2 changes: 1 addition & 1 deletion danfojs-node/src/io/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const read = async (
let rows, file;

if (isDataset(source)) {
console.log(
console.info(
"datapackage.json found. Loading Dataset package from Datahub.io"
);
const dataset = await Dataset.load(source);
Expand Down
Loading

0 comments on commit 7af31b0

Please sign in to comment.