@@ -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 } )
@@ -1029,34 +1029,33 @@ export class DataFrame extends Ndframe {
10291029 */
10301030 addColumn ( kwargs ) {
10311031
1032- let data_length = this . shape [ 0 ]
1033-
10341032 utils . __in_object ( kwargs , "column" , "column name not specified" ) ;
10351033 utils . __in_object ( kwargs , "value" , "column value not specified" ) ;
10361034
1037- let value = kwargs [ "value" ]
10381035 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+ }
10391044
10401045 if ( value . length != data_length ) {
10411046 throw new Error ( `Array length ${ value . length } not equal to ${ data_length } ` ) ;
10421047 }
10431048
1044-
10451049 if ( this . columns . includes ( column_name ) ) {
1046-
10471050 let col_idx = this . columns . indexOf ( column_name ) ;
1048-
10491051 let new_data = [ ]
1052+
10501053 this . values . map ( ( val , index ) => {
10511054 let new_val = val . slice ( ) ;
10521055 new_val [ col_idx ] = value [ index ]
10531056 new_data . push ( new_val ) ;
10541057 } )
1055- this . data = new_data ;
1056- // console.log(this.data)
1057- this . col_data [ col_idx ] = value
1058- // this.col_data[col_idx] = utils.__get_t(value)[0]
1059- this . data_tensor = tf . tensor ( new_data )
1058+ this . __update_frame_in_place ( new_data , null , null , null , null )
10601059
10611060
10621061 } else {
@@ -1070,18 +1069,13 @@ export class DataFrame extends Ndframe {
10701069 } ) ;
10711070
10721071 //add new dtype
1073- let old_type_list = [ ...this . dtypes ]
1074- old_type_list . push ( utils . __get_t ( value ) [ 0 ] )
1075- this . col_types = old_type_list
1076- this . data = new_data ;
1077- this . col_data = utils . __get_col_values ( new_data )
1078- this . data_tensor = tf . tensor ( new_data )
1079- let old_col_names = this . columns
1080- old_col_names . push ( column_name )
1081- let new_cols = old_col_names
1082- this . columns = new_cols
1083- this [ column_name ] = new Series ( value )
1084- // 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 )
10851079 Object . defineProperty ( this , column_name , {
10861080 get ( ) {
10871081 return new Series ( value , { columns : column_name , index : this . index } )
@@ -1191,11 +1185,15 @@ export class DataFrame extends Ndframe {
11911185 */
11921186 fillna ( kwargs = { } ) {
11931187
1194- let params_needed = [ "columns" , "values" ]
1188+ let params_needed = [ "columns" , "values" , "inplace" ]
11951189 if ( ! utils . __right_params_are_passed ( kwargs , params_needed ) ) {
11961190 throw Error ( `Params Error: A specified parameter is not supported. Your params must be any of the following [${ params_needed } ], got ${ Object . keys ( kwargs ) } ` )
11971191 }
11981192
1193+ if ( ! utils . __key_in_object ( kwargs , "inplace" ) ) {
1194+ kwargs [ 'inplace' ] = false
1195+ }
1196+
11991197 if ( utils . __key_in_object ( kwargs , "columns" ) ) {
12001198 //check if the column(s) exists
12011199 kwargs [ 'columns' ] . map ( col => {
@@ -1232,7 +1230,12 @@ export class DataFrame extends Ndframe {
12321230 final_data [ this . column_names [ i ] ] = col
12331231 } )
12341232
1235- 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+ }
12361239
12371240 } else {
12381241 //fill all columns using same value
@@ -1873,7 +1876,7 @@ export class DataFrame extends Ndframe {
18731876 * @returns {2D tensor }
18741877 */
18751878 get tensor ( ) {
1876- return this . row_data_tensor
1879+ return tf . tensor ( this . values )
18771880 }
18781881
18791882
@@ -2101,15 +2104,31 @@ export class DataFrame extends Ndframe {
21012104 }
21022105
21032106 //update a DataFrame in place
2104- __update_frame_in_place ( values , column_names , col_data , index , dtypes ) {
2105- if ( values != undefined ) {
2106- this . data = values
2107- if ( col_data == undefined || col_data == null ) {
2108- this . col_data = utils . __get_col_values ( values ) //update column values
2109- } else {
2110- this . col_data = col_data
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 ]
21112117 }
21122118 }
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+
21132132 if ( column_names != undefined ) {
21142133 this . columns = column_names
21152134 }
0 commit comments