Skip to content

Commit 7121812

Browse files
committed
add sort dataframe by index and column
1 parent 6e32f01 commit 7121812

3 files changed

Lines changed: 159 additions & 32 deletions

File tree

danfojs/src/core/frame.js

Lines changed: 102 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -840,39 +840,39 @@ export class DataFrame extends Ndframe {
840840
* inplace (Bool): Whether to perform sorting on the original Series or not}
841841
* @returns {Series}
842842
*/
843-
sort_values(kwargs = {}) {
844-
if (utils.__key_in_object(kwargs, "by")) {
845-
let sort_col = this.column(kwargs["by"])
846-
let sorted_col, sorted_index;
847-
let new_row_data = []
843+
// sort_values(kwargs = {}) {
844+
// if (utils.__key_in_object(kwargs, "by")) {
845+
// let sort_col = this.column(kwargs["by"])
846+
// let sorted_col, sorted_index;
847+
// let new_row_data = []
848+
849+
// if (utils.__key_in_object(kwargs, "inplace") && kwargs['inplace'] == true) {
850+
// sort_col.sort_values(kwargs)
851+
// sorted_index = sort_col.index
852+
853+
// } else {
854+
// sorted_col = sort_col.sort_values(kwargs)
855+
// sorted_index = sorted_col.index
856+
// }
848857

849-
if (utils.__key_in_object(kwargs, "inplace") && kwargs['inplace'] == true) {
850-
sort_col.sort_values(kwargs)
851-
sorted_index = sort_col.index
858+
// sorted_index.map(idx => {
859+
// new_row_data.push(this.values[idx])
860+
// })
861+
862+
// if (utils.__key_in_object(kwargs, "inplace") && kwargs['inplace'] == true) {
863+
// this.data = new_row_data
864+
// this.index_arr = sorted_index
865+
// return null
866+
// } else {
867+
// let df = new DataFrame(new_row_data, { columns: this.column_names, index: sorted_index, dtype: this.dtypes })
868+
// return df
869+
// }
852870

853-
} else {
854-
sorted_col = sort_col.sort_values(kwargs)
855-
sorted_index = sorted_col.index
856-
}
871+
// } else {
872+
// throw Error("Value Error: must specify the column to sort by")
873+
// }
857874

858-
sorted_index.map(idx => {
859-
new_row_data.push(this.values[idx])
860-
})
861-
862-
if (utils.__key_in_object(kwargs, "inplace") && kwargs['inplace'] == true) {
863-
this.data = new_row_data
864-
this.index_arr = sorted_index
865-
return null
866-
} else {
867-
let df = new DataFrame(new_row_data, { columns: this.column_names, index: sorted_index, dtype: this.dtypes })
868-
return df
869-
}
870-
871-
} else {
872-
throw Error("Value Error: must specify the column to sort by")
873-
}
874-
875-
}
875+
// }
876876

877877

878878
/**
@@ -2081,6 +2081,57 @@ export class DataFrame extends Ndframe {
20812081

20822082

20832083
}
2084+
/**
2085+
* Sort DataFrame by index
2086+
* @param {*} kwargs {inplace: Boolean, ascending: Bool}
2087+
* @returns DataFrame
2088+
*/
2089+
sortIndex(kwargs={}){
2090+
2091+
let inplace = typeof kwargs["inplace"] == "undefined" ? false : kwargs["inplace"]
2092+
let asc = typeof kwargs["ascending"] == "undefined" ? true : kwargs["ascending"]
2093+
2094+
let index_val = this.index
2095+
let [data, index] =this.__sort_by(index_val,index_val,asc)
2096+
2097+
if(inplace){
2098+
this.__update_frame_in_place(data,null,null,index,null)
2099+
}else{
2100+
2101+
let df = this.copy()
2102+
df.__update_frame_in_place(data,null,null,index,null)
2103+
return df
2104+
}
2105+
}
2106+
2107+
/**
2108+
* Sort a Dataframe in ascending or descending order by a specified column name.
2109+
* @param {kwargs} Object, {by: Column name to sort by
2110+
* ascending (Bool): Whether to return sorted values in ascending order or not,
2111+
* inplace (Bool): Whether to perform sorting on the original Series or not}
2112+
* @returns {Series}
2113+
*/
2114+
sort_values(kwargs={}){
2115+
2116+
if (!utils.__key_in_object(kwargs,"by")) {
2117+
throw Error(`use col_name to specify column name`)
2118+
}
2119+
2120+
let inplace = typeof kwargs["inplace"] == "undefined" ? false : kwargs["inplace"]
2121+
let asc = typeof kwargs["ascending"] == "undefined" ? true : kwargs["ascending"]
2122+
let index_val = this.index
2123+
let column_val = this.column(kwargs["by"]).values
2124+
let [data, index] = this.__sort_by(column_val,index_val,asc)
2125+
2126+
if(inplace){
2127+
this.__update_frame_in_place(data,null,null,index,null)
2128+
}else{
2129+
2130+
let df = this.copy()
2131+
df.__update_frame_in_place(data,null,null,index,null)
2132+
return df
2133+
}
2134+
}
20842135

20852136

20862137
//set all columns to DataFrame Property. This ensures easy access to columns as Series
@@ -2139,5 +2190,26 @@ export class DataFrame extends Ndframe {
21392190
this.col_types = dtypes
21402191
}
21412192
}
2193+
2194+
__sort_by(col_value,df_index, asc){
2195+
2196+
let values = this.values
2197+
2198+
2199+
let sorted_val = utils.__sort(col_value,asc)
2200+
2201+
let data = []
2202+
let indexs = []
2203+
for(let row_i=0; row_i < sorted_val.length; row_i++){
2204+
2205+
let index = col_value.indexOf(sorted_val[row_i])
2206+
2207+
data.push(values[index])
2208+
indexs.push(df_index[index])
2209+
}
2210+
2211+
return [data, indexs]
2212+
2213+
}
21422214
}
21432215

danfojs/src/core/utils.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,17 @@ export class Utils {
663663
return array.slice(0, num);
664664
}
665665

666-
__sort(arr){
666+
__sort(arr, ascending=true){
667667

668668
let collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
669669
let sorted = arr.slice()
670670

671-
return sorted.sort(collator.compare)
671+
if( ascending){
672+
return sorted.sort(collator.compare)
673+
}else{
674+
return sorted.sort((a,b)=> collator.compare(b,a));
675+
}
676+
672677

673678
}
674679
}

danfojs/tests/core/frame.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,16 @@ describe("DataFrame", function () {
776776
let expected = [[2, 4, 6, "c"], [0, 2, 4, "a"], [360, 180, 1, "b"]]
777777
assert.deepEqual(df.sort_values({ "by": "col3", "ascending": false }).values, expected)
778778
})
779+
it("Sort values in DataFrame by specified column containing alpha(numeric) values", function () {
780+
let data = [[0, 2, 4, "a"],
781+
[360, 180, 1, "b"],
782+
[2, 4, 6, "c"]]
783+
784+
let df = new DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] })
785+
let expected = [ [ 2, 4, 6, 'c' ], [ 360, 180, 1, 'b' ], [ 0, 2, 4, 'a' ] ]
786+
assert.deepEqual(df.sort_values({ "by": "col4", "ascending": false }).values, expected)
787+
})
788+
779789

780790
})
781791

@@ -2071,5 +2081,45 @@ describe("DataFrame", function () {
20712081
})
20722082
})
20732083

2084+
describe("SortIndex", function(){
2085+
2086+
it("sort index in ascending order", function(){
2087+
let data = [[0, 2, 4, "b"],
2088+
[360, 180, 360, "a"],
2089+
[2, 4, 6, "c"]]
2090+
2091+
let df = new DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] })
2092+
df.set_index({ key: ["b","a","c"], inplace: true })
2093+
2094+
let df2 = df.sortIndex()
2095+
let rslt = [ [ 360, 180, 360, 'a' ], [ 0, 2, 4, 'b' ], [ 2, 4, 6, 'c' ] ]
2096+
2097+
assert.deepEqual(df2.values, rslt)
2098+
})
2099+
it("sort index in descending order", function(){
2100+
let data = [[0, 2, 4, "b"],
2101+
[360, 180, 360, "a"],
2102+
[2, 4, 6, "c"]]
2103+
2104+
let df = new DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] })
2105+
df.set_index({ key: ["b","a","c"], inplace: true })
2106+
2107+
let df2 = df.sortIndex({ascending:false})
2108+
let rslt = [ [ 2, 4, 6, 'c' ], [ 0, 2, 4, 'b' ], [ 360, 180, 360, 'a' ] ]
2109+
2110+
assert.deepEqual(df2.values, rslt)
2111+
})
2112+
it("sort index in descending order with inplace set to true", function(){
2113+
let data = [[0, 2, 4, "b"],
2114+
[360, 180, 360, "a"],
2115+
[2, 4, 6, "c"]]
20742116

2117+
let df = new DataFrame(data, { "columns": ["col1", "col2", "col3", "col4"] })
2118+
df.set_index({ key: ["b","a","c"], inplace: true })
2119+
2120+
df.sortIndex({ascending:false,inplace:true})
2121+
let rslt = [ [ 2, 4, 6, 'c' ], [ 0, 2, 4, 'b' ], [ 360, 180, 360, 'a' ] ]
2122+
assert.deepEqual(df.values, rslt)
2123+
})
2124+
});
20752125
});

0 commit comments

Comments
 (0)