Skip to content

Commit

Permalink
Merge pull request #360 from javascriptdata/fix/308
Browse files Browse the repository at this point in the history
Fixes issue with sort by values
  • Loading branch information
risenW authored Jan 19, 2022
2 parents 133515b + 06a5b51 commit 878e21b
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/danfojs-base/core/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3348,7 +3348,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
* ```
*/
iat(row: number, column: number): string | number | boolean | undefined {
if(typeof row === 'string' || typeof column === 'string') {
if (typeof row === 'string' || typeof column === 'string') {
throw new Error('ParamError: row and column index must be an integer. Use .at to get a row or column by label.')
}

Expand All @@ -3370,7 +3370,7 @@ export default class DataFrame extends NDframe implements DataFrameInterface {
* ```
*/
at(row: string | number, column: string): string | number | boolean | undefined {
if(typeof column !== 'string') {
if (typeof column !== 'string') {
throw new Error('ParamError: column index must be a string. Use .iat to get a row or column by index.')
}
return (this.values as ArrayType2D)[this.index.indexOf(row)][this.columns.indexOf(column)]
Expand Down
20 changes: 14 additions & 6 deletions src/danfojs-base/core/series.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,9 +674,10 @@ export default class Series extends NDframe implements SeriesInterface {
* //output [ 1.23, 2.4, 3.12, 4.12, 5.12 ]
* ```
*/
round(dp?: number, options?: { inplace?: boolean }): Series
round(dp = 1, options?: { inplace?: boolean }): Series | void {
const { inplace } = { inplace: false, ...options }

if (dp === undefined) dp = 1;
const newValues = utils.round(this.values as number[], dp, true);

if (inplace) {
Expand Down Expand Up @@ -816,24 +817,26 @@ export default class Series extends NDframe implements SeriesInterface {
const { ascending, inplace, } = { ascending: true, inplace: false, ...options }

let sortedValues = [];
let sortedIndex = []
const rangeIdx = utils.range(0, this.index.length - 1);
let sortedIdx = utils.sortArrayByIndex(rangeIdx, this.values, this.dtypes[0]);

for (let indx of sortedIdx) {
sortedValues.push(this.values[indx])
sortedIndex.push(this.index[indx])
}

if (ascending) {
sortedValues = sortedValues.reverse();
sortedIdx = sortedIdx.reverse();
sortedIndex = sortedIndex.reverse();
}

if (inplace) {
this.$setValues(sortedValues as ArrayType1D)
this.$setIndex(sortedIdx);
this.$setIndex(sortedIndex);
} else {
const sf = new Series(sortedValues, {
index: sortedIdx,
index: sortedIndex,
dtypes: this.dtypes,
config: this.config
});
Expand Down Expand Up @@ -1828,6 +1831,11 @@ export default class Series extends NDframe implements SeriesInterface {
* //output [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ]
* ```
*/
append(
newValue: string | number | boolean | Series | ArrayType1D,
index: Array<number | string> | number | string,
options?: { inplace?: boolean }
): Series
append(
newValue: string | number | boolean | Series | ArrayType1D,
index: Array<number | string> | number | string,
Expand Down Expand Up @@ -2254,7 +2262,7 @@ export default class Series extends NDframe implements SeriesInterface {
* ```
*/
iat(row: number): number | string | boolean | undefined {
if(typeof row === 'string') {
if (typeof row === 'string') {
throw new Error('ParamError: row index must be an integer. Use .at to get a row by label.')
}
return (this.values as ArrayType1D)[row];
Expand All @@ -2274,7 +2282,7 @@ export default class Series extends NDframe implements SeriesInterface {
* ```
*/
at(row: string): number | string | boolean | undefined {
if(typeof row !== 'string') {
if (typeof row !== 'string') {
throw new Error('ParamError: row index must be a string. Use .iat to get a row by index.')
}
return (this.values as ArrayType1D)[this.index.indexOf(row)];
Expand Down
43 changes: 32 additions & 11 deletions src/danfojs-base/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,27 +774,48 @@ export default class Utils {
arr: Array<{ index: number | string, value: number | string | boolean }>,
ascending: boolean
) {
return arr.sort((obj1, obj2) => {
const a = obj2.value;
const b = obj1.value;
let sortedValues = arr.sort((obj1, obj2) => {
let a = obj2.value;
let b = obj1.value;

if (!ascending) {
if (typeof a === "string" && typeof b === "string") {
return a.charCodeAt(0) - b.charCodeAt(0);
} else if ((typeof a === "number" && typeof b === "number") || (typeof a === "boolean" && typeof b === "boolean")) {
return Number(a) - Number(b);
a = a.toUpperCase();
b = b.toUpperCase();

if (a < b) {
return -1;
}

if (a > b) {
return 1;
}

return 0;

} else {
throw Error('ParamError: column values must be either numbers or strings');
return Number(a) - Number(b);
}
} else {
if (typeof a === "string" && typeof b === "string") {
return b.charCodeAt(0) - a.charCodeAt(0);
} else if ((typeof a === "number" && typeof b === "number") || (typeof a === "boolean" && typeof b === "boolean")) {
return Number(b) - Number(a);
a = a.toUpperCase();
b = b.toUpperCase();

if (a > b) {
return -1;
}

if (a < b) {
return 1;
}

return 0;
} else {
throw Error('ParamError: column values must be either numbers or strings');
return Number(b) - Number(a);;
}
}
});

return sortedValues;
}
}
11 changes: 11 additions & 0 deletions src/danfojs-browser/tests/core/frame.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,17 @@ describe("DataFrame", function () {
[ 6, 9 ] ];
assert.deepEqual(df.sortValues("A", { "ascending": true }).values, expected);
});

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.sortIndex({ ascending: false });
let rslt = [ "c", "b", "a" ];
assert.deepEqual(df2.index, rslt);
});
});

describe("copy", function () {
Expand Down
39 changes: 39 additions & 0 deletions src/danfojs-browser/tests/core/series.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,45 @@ describe("Series Functions", () => {
const sortedSf = sf.sortValues({ ascending: false });
assert.deepEqual(sortedSf.values, result);
});

it("Index is retained after sort (ascending=true)", function () {
let index = [ "apple", "banana", "orange", "grape" ];
let value = [ 3, 6, 2, 9 ];

let sf = new dfd.Series(value, { index });
sf.sortValues().print();
const expectedValues = [ 2, 3, 6, 9 ];
const expectedIndex = [ "orange", "apple", "banana", "grape" ];
const sortedSf = sf.sortValues();
assert.deepEqual(sortedSf.values, expectedValues);
assert.deepEqual(sortedSf.index, expectedIndex);
});
it("Index is retained after sort (ascending=false)", function () {
let index = [ "apple", "banana", "orange", "grape" ];
let value = [ 3, 6, 2, 9 ];

let sf = new dfd.Series(value, { index });
sf.sortValues().print();
const expectedValues = [ 9, 6, 3, 2 ];
const expectedIndex = [ "grape", "banana", "apple", "orange" ];
const sortedSf = sf.sortValues({ ascending: false });
assert.deepEqual(sortedSf.values, expectedValues);
assert.deepEqual(sortedSf.index, expectedIndex);
});

it("Index is retained after inplace sort (ascending=false)", function () {
let index = [ "apple", "banana", "orange", "grape" ];
let value = [ 3, 6, 2, 9 ];

let sf = new dfd.Series(value, { index });
sf.sortValues().print();
const expectedValues = [ 9, 6, 3, 2 ];
const expectedIndex = [ "grape", "banana", "apple", "orange" ];
sf.sortValues({ ascending: false, inplace: true });
assert.deepEqual(sf.values, expectedValues);
assert.deepEqual(sf.index, expectedIndex);
});

});

describe("describe", function () {
Expand Down
22 changes: 17 additions & 5 deletions src/danfojs-node/test/core/frame.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ describe("DataFrame", function () {
[2, 4, 6, "c"]];

const df = new DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] });
const df_sort = df.sortValues("col3") as DataFrame
const df_sort = df.sortValues("col3")
const expected = [[360, 180, 1, "b"], [0, 2, 4, "a"], [2, 4, 6, "c"]];
assert.deepEqual(df_sort.values, expected);
assert.deepEqual(df_sort.index, [1, 0, 2]);
Expand Down Expand Up @@ -1297,9 +1297,21 @@ describe("DataFrame", function () {
[2, 4, 6, "c"]];

const df = new DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] });
const expected = [[2, 4, 6, 'c'], [360, 180, 1, 'b'], [0, 2, 4, 'a']];
assert.deepEqual((df.sortValues("col4", { "ascending": false }) as DataFrame).values, expected);
const expected = [[2, 4, 6, "c"], [360, 180, 1, "b"], [0, 2, 4, "a"]]
assert.deepEqual(df.sortValues("col4", { "ascending": false }).values, expected);
});

it("sort works for Date string", function () {
const data = {
date: ['1974-02-19', '1955-12-06', '1963-11-18']
};
const df = new DataFrame(data);
const expected1 = [['1974-02-19'], ['1963-11-18'], ['1955-12-06']]
const expected2 = [['1955-12-06'], ['1963-11-18'], ['1974-02-19']]
assert.deepEqual(df.sortValues("date", { "ascending": false }).values, expected1);
assert.deepEqual(df.sortValues("date", { "ascending": true }).values, expected2);
});

it("Sort duplicate DataFrame with duplicate columns", function () {

const data = {
Expand All @@ -1320,7 +1332,7 @@ describe("DataFrame", function () {
[5, 8],
[5, 2],
[6, 9]];
assert.deepEqual((df.sortValues("A", { "ascending": true }) as DataFrame).values, expected);
assert.deepEqual(df.sortValues("A", { "ascending": true }).values, expected);
});
it("sort index in descending order and retains index", function () {
let data = [[0, 2, 4, "b"],
Expand All @@ -1330,9 +1342,9 @@ describe("DataFrame", function () {
let df = new DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"], index: ["b", "a", "c"] });
let df2 = df.sortIndex({ ascending: false });
let rslt = ["c", "b", "a"];

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

});

describe("copy", function () {
Expand Down
Loading

0 comments on commit 878e21b

Please sign in to comment.