@@ -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
0 commit comments