@@ -2200,26 +2200,27 @@ def where(self, cond, other=np.nan):
22002200 >>> reset_option("compute.ops_on_diff_frames")
22012201 """
22022202 from databricks .koalas .series import Series
2203- tmp_cond_col_name = '__tmp_cond_col_{}__'
2204- tmp_other_col_name = '__tmp_other_col_{}__'
2203+
2204+ tmp_cond_col_name = '__tmp_cond_col_{}__' .format
2205+ tmp_other_col_name = '__tmp_other_col_{}__' .format
2206+
22052207 kdf = self .copy ()
22062208 if isinstance (cond , DataFrame ):
2207- for column in self ._internal .data_columns :
2208- kdf [tmp_cond_col_name . format ( column ) ] = cond .get (column , False )
2209+ for idx in self ._internal .column_index :
2210+ kdf [tmp_cond_col_name ( name_like_string ( idx )) ] = cond .get (idx , False )
22092211 elif isinstance (cond , Series ):
2210- for column in self ._internal .data_columns :
2211- kdf [tmp_cond_col_name . format ( column )] = cond
2212+ for idx in self ._internal .column_index :
2213+ kdf [tmp_cond_col_name ( name_like_string ( idx ) )] = cond
22122214 else :
22132215 raise ValueError ("type of cond must be a DataFrame or Series" )
22142216
22152217 if isinstance (other , DataFrame ):
2216- for column in self ._internal .data_columns :
2217- kdf [tmp_other_col_name . format ( column ) ] = other .get (column , np .nan )
2218+ for idx in self ._internal .column_index :
2219+ kdf [tmp_other_col_name ( name_like_string ( idx )) ] = other .get (idx , np .nan )
22182220 else :
2219- for column in self ._internal .data_columns :
2220- kdf [tmp_other_col_name . format ( column )] = other
2221+ for idx in self ._internal .column_index :
2222+ kdf [tmp_other_col_name ( name_like_string ( idx ) )] = other
22212223
2222- sdf = kdf ._sdf
22232224 # above logic make spark dataframe looks like below:
22242225 # +-----------------+---+---+------------------+-------------------+------------------+--...
22252226 # |__index_level_0__| A| B|__tmp_cond_col_A__|__tmp_other_col_A__|__tmp_cond_col_B__|__...
@@ -2231,22 +2232,18 @@ def where(self, cond, other=np.nan):
22312232 # | 4| 4|500| false| -4| false| ...
22322233 # +-----------------+---+---+------------------+-------------------+------------------+--...
22332234
2234- output = []
2235- for column in self ._internal .data_columns :
2236- data_col_name = self ._internal .column_name_for (column )
2237- output .append (
2235+ column_scols = []
2236+ for idx in self ._internal .column_index :
2237+ column_scols .append (
22382238 F .when (
2239- scol_for (sdf , tmp_cond_col_name .format (column )), scol_for (sdf , data_col_name )
2239+ kdf [tmp_cond_col_name (name_like_string (idx ))]._scol ,
2240+ kdf [idx ]._scol
22402241 ).otherwise (
2241- scol_for ( sdf , tmp_other_col_name . format ( column ))
2242- ).alias (data_col_name ))
2242+ kdf [ tmp_other_col_name ( name_like_string ( idx ))]. _scol
2243+ ).alias (kdf . _internal . column_name_for ( idx ) ))
22432244
2244- index_scols = kdf ._internal .index_scols
2245- sdf = sdf .select (index_scols + output + list (HIDDEN_COLUMNS ))
2246-
2247- return DataFrame (self ._internal .copy (
2248- sdf = sdf ,
2249- column_scols = [scol_for (sdf , column ) for column in self ._internal .data_columns ]))
2245+ return DataFrame (kdf ._internal .with_new_columns (column_scols ,
2246+ column_index = self ._internal .column_index ))
22502247
22512248 def mask (self , cond , other = np .nan ):
22522249 """
0 commit comments