@@ -33,7 +33,7 @@ export class DataFrame extends Ndframe {
3333 let col_names = this . column_names
3434
3535 col_vals . forEach ( ( col , i ) => {
36- this [ col_names [ i ] ] = new Series ( col , { columns : col_names [ i ] , index : this . index } )
36+ this [ col_names [ i ] ] = null
3737 Object . defineProperty ( this , col_names [ i ] , {
3838 get ( ) {
3939 return new Series ( this . col_data [ i ] , { columns : col_names [ i ] , index : this . index } )
@@ -961,24 +961,25 @@ export class DataFrame extends Ndframe {
961961 "=="
962962 ]
963963
964- if ( Object . prototype . hasOwnProperty . call ( kwargs , "column" ) ) {
964+ if ( ! utils . __key_in_object ( kwargs , "inplace" ) ) {
965+ kwargs [ 'inplace' ] = false
966+ }
965967
968+ let column_index , operator , value ;
966969
970+ if ( utils . __key_in_object ( kwargs , "column" ) ) {
967971 if ( this . columns . includes ( kwargs [ "column" ] ) ) {
968-
969- var column_index = this . columns . indexOf ( kwargs [ "column" ] ) ;
972+ column_index = this . columns . indexOf ( kwargs [ "column" ] ) ;
970973 } else {
971974 throw new Error ( `column ${ kwargs [ "column" ] } does not exist` ) ;
972975 }
973976 } else {
974977 throw new Error ( "specify the column" ) ;
975978 }
976979
977- if ( Object . prototype . hasOwnProperty . call ( kwargs , "is" ) ) {
978-
980+ if ( utils . __key_in_object ( kwargs , "is" ) ) {
979981 if ( operators . includes ( kwargs [ "is" ] ) ) {
980-
981- var operator = kwargs [ "is" ] ;
982+ operator = kwargs [ "is" ] ;
982983 }
983984 else {
984985 throw new Error ( ` ${ kwargs [ "is" ] } is not a supported logical operator` ) ;
@@ -987,34 +988,37 @@ export class DataFrame extends Ndframe {
987988 throw new Error ( "specify an operator in param [is]" ) ;
988989 }
989990
990- if ( Object . prototype . hasOwnProperty . call ( kwargs , "to" ) ) {
991- var value = kwargs [ "to" ]
991+ if ( utils . __key_in_object ( kwargs , "to" ) ) {
992+ value = kwargs [ "to" ]
992993
993994 } else {
994995 throw new Error ( "specify a value in param [to]" ) ;
995996 }
996997
997998 let data = this . values
998-
999+ let index = this . index
9991000 let new_data = [ ]
1001+ let new_index = [ ]
10001002
10011003 for ( var i = 0 ; i < data . length ; i ++ ) {
10021004 let data_value = data [ i ]
1003-
10041005 let elem = data_value [ column_index ]
1005-
10061006 //use eval function for easy operation
10071007 //eval() takes in a string expression e.g eval('2>5')
10081008 if ( eval ( `${ elem } ${ operator } ${ value } ` ) ) {
10091009 new_data . push ( data_value ) ;
1010- }
1010+ new_index . push ( index [ i ] )
10111011
1012+ }
10121013
10131014 }
1014- let columns = this . columns
1015- let new_df = new DataFrame ( new_data , { "columns" : columns } )
10161015
1017- return new_df ;
1016+ if ( kwargs [ 'inplace' ] ) {
1017+ this . __update_frame_in_place ( new_data , this . columns , null , new_index , null )
1018+ } else {
1019+ let new_df = new DataFrame ( new_data , { "columns" : this . columns , index : new_index } )
1020+ return new_df ;
1021+ }
10181022 }
10191023
10201024
@@ -1025,34 +1029,33 @@ export class DataFrame extends Ndframe {
10251029 */
10261030 addColumn ( kwargs ) {
10271031
1028- let data_length = this . shape [ 0 ]
1029-
10301032 utils . __in_object ( kwargs , "column" , "column name not specified" ) ;
10311033 utils . __in_object ( kwargs , "value" , "column value not specified" ) ;
10321034
1033- let value = kwargs [ "value" ]
10341035 let column_name = kwargs [ "column" ]
1036+ let data_length = this . shape [ 0 ]
1037+ let value ;
1038+
1039+ if ( kwargs [ 'value' ] instanceof Series ) {
1040+ value = kwargs [ 'value' ] . values
1041+ } else {
1042+ value = kwargs [ "value" ]
1043+ }
10351044
10361045 if ( value . length != data_length ) {
10371046 throw new Error ( `Array length ${ value . length } not equal to ${ data_length } ` ) ;
10381047 }
10391048
1040-
10411049 if ( this . columns . includes ( column_name ) ) {
1042-
10431050 let col_idx = this . columns . indexOf ( column_name ) ;
1044-
10451051 let new_data = [ ]
1052+
10461053 this . values . map ( ( val , index ) => {
10471054 let new_val = val . slice ( ) ;
10481055 new_val [ col_idx ] = value [ index ]
10491056 new_data . push ( new_val ) ;
10501057 } )
1051- this . data = new_data ;
1052- // console.log(this.data)
1053- this . col_data [ col_idx ] = value
1054- // this.col_data[col_idx] = utils.__get_t(value)[0]
1055- this . data_tensor = tf . tensor ( new_data )
1058+ this . __update_frame_in_place ( new_data , null , null , null , null )
10561059
10571060
10581061 } else {
@@ -1066,21 +1069,18 @@ export class DataFrame extends Ndframe {
10661069 } ) ;
10671070
10681071 //add new dtype
1069- let old_type_list = [ ...this . dtypes ]
1070- old_type_list . push ( utils . __get_t ( value ) [ 0 ] )
1071- this . col_types = old_type_list
1072- this . data = new_data ;
1073- this . col_data = utils . __get_col_values ( new_data )
1074- this . data_tensor = tf . tensor ( new_data )
1075- let old_col_names = this . columns
1076- old_col_names . push ( column_name )
1077- let new_cols = old_col_names
1078- this . columns = new_cols
1079- this [ column_name ] = new Series ( value )
1080- // this.__set_col_property(this, this.col_data, new_cols, old_col_names,true)
1072+ let new_dtypes = [ ...this . dtypes ]
1073+ new_dtypes . push ( utils . __get_t ( value ) [ 0 ] )
1074+
1075+ let new_col_names = [ ...this . columns ]
1076+ new_col_names . push ( column_name )
1077+
1078+ this . __update_frame_in_place ( new_data , new_col_names , null , null , new_dtypes )
10811079 Object . defineProperty ( this , column_name , {
10821080 get ( ) {
10831081 return new Series ( value , { columns : column_name , index : this . index } )
1082+ } , set ( value ) {
1083+ this . addColumn ( { column : column_name , value : value } ) ;
10841084 }
10851085 } )
10861086 }
@@ -1185,11 +1185,15 @@ export class DataFrame extends Ndframe {
11851185 */
11861186 fillna ( kwargs = { } ) {
11871187
1188- let params_needed = [ "columns" , "values" ]
1188+ let params_needed = [ "columns" , "values" , "inplace" ]
11891189 if ( ! utils . __right_params_are_passed ( kwargs , params_needed ) ) {
11901190 throw Error ( `Params Error: A specified parameter is not supported. Your params must be any of the following [${ params_needed } ], got ${ Object . keys ( kwargs ) } ` )
11911191 }
11921192
1193+ if ( ! utils . __key_in_object ( kwargs , "inplace" ) ) {
1194+ kwargs [ 'inplace' ] = false
1195+ }
1196+
11931197 if ( utils . __key_in_object ( kwargs , "columns" ) ) {
11941198 //check if the column(s) exists
11951199 kwargs [ 'columns' ] . map ( col => {
@@ -1226,7 +1230,12 @@ export class DataFrame extends Ndframe {
12261230 final_data [ this . column_names [ i ] ] = col
12271231 } )
12281232
1229- return new DataFrame ( final_data , { index : this . index } )
1233+ if ( kwargs [ 'inplace' ] ) {
1234+ this . __update_frame_in_place ( null , null , final_data , null , null )
1235+ } else {
1236+ return new DataFrame ( final_data , { index : this . index } )
1237+
1238+ }
12301239
12311240 } else {
12321241 //fill all columns using same value
@@ -1867,7 +1876,7 @@ export class DataFrame extends Ndframe {
18671876 * @returns {2D tensor }
18681877 */
18691878 get tensor ( ) {
1870- return this . row_data_tensor
1879+ return tf . tensor ( this . values )
18711880 }
18721881
18731882
@@ -2021,8 +2030,6 @@ export class DataFrame extends Ndframe {
20212030 if ( ! utils . __key_in_object ( kwargs , "mapper" ) ) {
20222031 throw Error ( "Please specify a mapper object" )
20232032 }
2024- // console.log(kwargs['axis']);
2025- // console.log(kwargs['inplace']);
20262033 if ( kwargs [ 'axis' ] == 1 ) {
20272034 //columns
20282035 let old_col_names = Object . keys ( kwargs [ 'mapper' ] )
@@ -2038,11 +2045,9 @@ export class DataFrame extends Ndframe {
20382045 col_names [ idx ] = new_col_names [ i ]
20392046
20402047 } )
2041-
20422048 if ( kwargs [ 'inplace' ] ) {
20432049 this . columns = col_names
20442050 this . __set_col_property ( this , this . col_data , col_names , old_col_names )
2045-
20462051 } else {
20472052 let df = this . copy ( )
20482053 df . columns = col_names
@@ -2090,10 +2095,49 @@ export class DataFrame extends Ndframe {
20902095 Object . defineProperty ( self , col_names [ i ] , {
20912096 get ( ) {
20922097 return new Series ( col , { columns : col_names [ i ] , index : self . index } )
2098+ } , set ( value ) {
2099+ this . addColumn ( { column : col_names [ i ] , value : value } ) ;
20932100 }
20942101 } )
20952102 } ) ;
20962103
20972104 }
2105+
2106+ //update a DataFrame in place
2107+ __update_frame_in_place ( row_data , column_names , col_obj , index , dtypes ) {
2108+ if ( row_data != undefined ) {
2109+ this . data = row_data
2110+ } else {
2111+ //check column is available and create row from column
2112+ if ( col_obj != undefined ) {
2113+ let _res = utils . __get_row_values ( col_obj )
2114+ this . data = _res [ 0 ]
2115+ this . columns = _res [ 1 ]
2116+ column_names = _res [ 1 ]
2117+ }
2118+ }
2119+
2120+ if ( col_obj != undefined ) {
2121+ this . col_data = Object . values ( col_obj )
2122+ this . columns = Object . keys ( col_obj )
2123+ column_names = Object . keys ( col_obj )
2124+ } else {
2125+ //check if row data is available and create column data from rows
2126+ if ( row_data != undefined ) {
2127+ this . col_data = utils . __get_col_values ( row_data ) //get column data from row
2128+ }
2129+ }
2130+
2131+
2132+ if ( column_names != undefined ) {
2133+ this . columns = column_names
2134+ }
2135+ if ( index != undefined ) {
2136+ this . index_arr = index
2137+ }
2138+ if ( dtypes != undefined ) {
2139+ this . col_types = dtypes
2140+ }
2141+ }
20982142}
20992143
0 commit comments