Skip to content

Commit

Permalink
Minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
risenW committed Oct 10, 2021
1 parent a5ba784 commit ec1b16d
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
5 changes: 3 additions & 2 deletions danfojs-browser/src/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -1237,7 +1237,7 @@ export default class DataFrame extends NDframe {
const dfValues = this.values;
for (let i = 0; i < dfValues.length; i++) {
const values = dfValues[i];
if (!values.includes(NaN)) {
if (!values.includes(NaN) && !values.includes(undefined) && !values.includes(null)) {
newData.push(values);
newIndex.push(this.index[i]);
}
Expand Down Expand Up @@ -2100,8 +2100,9 @@ export default class DataFrame extends NDframe {
});

const sortedObjectArr = utils.sortObj(objToSort, ascending);
const sortedIndex = sortedObjectArr.map((obj) => obj.index);
let sortedIndex = sortedObjectArr.map((obj) => obj.index);
const newData = sortedIndex.map((i) => (this.values)[i]);
sortedIndex = sortedIndex.map((i) => index[i]);

if (inplace) {
this.$setValues(newData);
Expand Down
7 changes: 4 additions & 3 deletions danfojs-browser/src/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,9 +789,10 @@ export default class Series extends NDframe {
if (inplace) {
this.$setValues(data);
} else {
const sf = this.copy();
sf.$setValues(data);
return sf;
return new Series(data, {
index: this.index,
config: { ...this.config }
});
}
}

Expand Down
11 changes: 11 additions & 0 deletions danfojs-browser/tests/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -2047,6 +2047,17 @@ describe("DataFrame", function () {
let rslt = [[2, 4, 6, 'c'], [0, 2, 4, 'b'], [360, 180, 360, 'a']];
assert.deepEqual(df.values, rslt);
});
it("sort index in descending order and retains index", function () {
let data = [[0, 2, 4, "b"],
[360, 180, 360, "a"],
[2, 4, 6, "c"]];

let df = new dfd.DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"], index: ["b", "a", "c"] });
let df2 = df.sort_index({ ascending: false });
let rslt = ["c", "b", "a"];

assert.deepEqual(df2.index, rslt);
});
});

describe("append", function () {
Expand Down
5 changes: 3 additions & 2 deletions danfojs-node/src/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,7 @@ export default class DataFrame extends NDframe {
const dfValues = this.values;
for (let i = 0; i < dfValues.length; i++) {
const values = dfValues[i];
if (!values.includes(NaN)) {
if (!values.includes(NaN) && !values.includes(undefined) && !values.includes(null)) {
newData.push(values);
newIndex.push(this.index[i]);
}
Expand Down Expand Up @@ -2102,8 +2102,9 @@ export default class DataFrame extends NDframe {
});

const sortedObjectArr = utils.sortObj(objToSort, ascending);
const sortedIndex = sortedObjectArr.map((obj) => obj.index);
let sortedIndex = sortedObjectArr.map((obj) => obj.index);
const newData = sortedIndex.map((i) => (this.values)[i]);
sortedIndex = sortedIndex.map((i) => index[i]);

if (inplace) {
this.$setValues(newData);
Expand Down
7 changes: 4 additions & 3 deletions danfojs-node/src/core/series.js
Original file line number Diff line number Diff line change
Expand Up @@ -785,9 +785,10 @@ export default class Series extends NDframe {
if (inplace) {
this.$setValues(data);
} else {
const sf = this.copy();
sf.$setValues(data);
return sf;
return new Series(data, {
index: this.index,
config: { ...this.config }
});
}
}

Expand Down
24 changes: 23 additions & 1 deletion danfojs-node/tests/core/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ describe("DataFrame", function () {
let data = [[0, 2, 4], [360, 180, 360]];
let df = new DataFrame(data, { columns: ["col1", "col2", "col3"] });
assert.deepEqual(df.mean().values, [180, 91, 182]);
assert.deepEqual(df.mean().index, ["col1", "col2", "col3"] );
assert.deepEqual(df.mean().index, ["col1", "col2", "col3"]);

});
it("Return mean of a DataFrame along axis 0 (row)", function () {
Expand Down Expand Up @@ -1431,6 +1431,17 @@ describe("DataFrame", function () {
assert.deepEqual(df.values, df_val);

});
it("drop works for undefined values", function () {
let data = [[null, 1, 2, 3], [3, 4, undefined, 9], [5, 6, 7, 8]];
let column = ["A", "B", "C", "D"];
let df = new DataFrame(data, { columns: column });

let df_val = [[5, 6, 7, 8]];

df.dropna(0, { inplace: true });
assert.deepEqual(df.values, df_val);

});
});

describe("isna", function () {
Expand Down Expand Up @@ -2068,6 +2079,17 @@ describe("DataFrame", function () {
let rslt = [[2, 4, 6, 'c'], [0, 2, 4, 'b'], [360, 180, 360, 'a']];
assert.deepEqual(df.values, rslt);
});
it("sort index in descending order and retains index", function () {
let data = [[0, 2, 4, "b"],
[360, 180, 360, "a"],
[2, 4, 6, "c"]];

let df = new DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"], index: ["b", "a", "c"] });
let df2 = df.sort_index({ ascending: false });
let rslt = ["c", "b", "a"];

assert.deepEqual(df2.index, rslt);
});
});

describe("append", function () {
Expand Down

0 comments on commit ec1b16d

Please sign in to comment.