@@ -4177,6 +4177,32 @@ def get(self, key, default=None):
4177
4177
except (KeyError , ValueError , IndexError ):
4178
4178
return default
4179
4179
4180
+ def _sort (self , by : List [Column ], ascending : Union [bool , List [bool ]],
4181
+ inplace : bool , na_position : str ):
4182
+ if isinstance (ascending , bool ):
4183
+ ascending = [ascending ] * len (by )
4184
+ if len (ascending ) != len (by ):
4185
+ raise ValueError ('Length of ascending ({}) != length of by ({})'
4186
+ .format (len (ascending ), len (by )))
4187
+ if na_position not in ('first' , 'last' ):
4188
+ raise ValueError ("invalid na_position: '{}'" .format (na_position ))
4189
+
4190
+ # Mapper: Get a spark column function for (ascending, na_position) combination
4191
+ # Note that 'asc_nulls_first' and friends were added as of Spark 2.4, see SPARK-23847.
4192
+ mapper = {
4193
+ (True , 'first' ): lambda x : Column (getattr (x ._jc , "asc_nulls_first" )()),
4194
+ (True , 'last' ): lambda x : Column (getattr (x ._jc , "asc_nulls_last" )()),
4195
+ (False , 'first' ): lambda x : Column (getattr (x ._jc , "desc_nulls_first" )()),
4196
+ (False , 'last' ): lambda x : Column (getattr (x ._jc , "desc_nulls_last" )()),
4197
+ }
4198
+ by = [mapper [(asc , na_position )](scol ) for scol , asc in zip (by , ascending )]
4199
+ kdf = DataFrame (self ._internal .copy (sdf = self ._sdf .sort (* by ))) # type: ks.DataFrame
4200
+ if inplace :
4201
+ self ._internal = kdf ._internal
4202
+ return None
4203
+ else :
4204
+ return kdf
4205
+
4180
4206
def sort_values (self , by : Union [str , List [str ]], ascending : Union [bool , List [bool ]] = True ,
4181
4207
inplace : bool = False , na_position : str = 'last' ) -> Optional ['DataFrame' ]:
4182
4208
"""
@@ -4253,30 +4279,9 @@ def sort_values(self, by: Union[str, List[str]], ascending: Union[bool, List[boo
4253
4279
"""
4254
4280
if isinstance (by , str ):
4255
4281
by = [by ]
4256
- if isinstance (ascending , bool ):
4257
- ascending = [ascending ] * len (by )
4258
- if len (ascending ) != len (by ):
4259
- raise ValueError ('Length of ascending ({}) != length of by ({})'
4260
- .format (len (ascending ), len (by )))
4261
- if na_position not in ('first' , 'last' ):
4262
- raise ValueError ("invalid na_position: '{}'" .format (na_position ))
4263
-
4264
- # Mapper: Get a spark column function for (ascending, na_position) combination
4265
- # Note that 'asc_nulls_first' and friends were added as of Spark 2.4, see SPARK-23847.
4266
- mapper = {
4267
- (True , 'first' ): lambda x : Column (getattr (x ._jc , "asc_nulls_first" )()),
4268
- (True , 'last' ): lambda x : Column (getattr (x ._jc , "asc_nulls_last" )()),
4269
- (False , 'first' ): lambda x : Column (getattr (x ._jc , "desc_nulls_first" )()),
4270
- (False , 'last' ): lambda x : Column (getattr (x ._jc , "desc_nulls_last" )()),
4271
- }
4272
- by = [mapper [(asc , na_position )](self [colname ]._scol )
4273
- for colname , asc in zip (by , ascending )]
4274
- kdf = DataFrame (self ._internal .copy (sdf = self ._sdf .sort (* by ))) # type: ks.DataFrame
4275
- if inplace :
4276
- self ._internal = kdf ._internal
4277
- return None
4278
- else :
4279
- return kdf
4282
+ by = [self [colname ]._scol for colname in by ]
4283
+ return self ._sort (by = by , ascending = ascending ,
4284
+ inplace = inplace , na_position = na_position )
4280
4285
4281
4286
def sort_index (self , axis : int = 0 ,
4282
4287
level : Optional [Union [int , List [int ]]] = None , ascending : bool = True ,
@@ -4367,14 +4372,14 @@ def sort_index(self, axis: int = 0,
4367
4372
raise ValueError ("Specifying the sorting algorithm is supported at the moment." )
4368
4373
4369
4374
if level is None or (is_list_like (level ) and len (level ) == 0 ): # type: ignore
4370
- by = self ._internal .index_columns
4375
+ by = self ._internal .index_scols
4371
4376
elif is_list_like (level ):
4372
- by = [self ._internal .index_columns [l ] for l in level ] # type: ignore
4377
+ by = [self ._internal .index_scols [l ] for l in level ] # type: ignore
4373
4378
else :
4374
- by = self ._internal .index_columns [level ]
4379
+ by = [ self ._internal .index_scols [level ] ]
4375
4380
4376
- return self .sort_values (by = by , ascending = ascending ,
4377
- inplace = inplace , na_position = na_position )
4381
+ return self ._sort (by = by , ascending = ascending ,
4382
+ inplace = inplace , na_position = na_position )
4378
4383
4379
4384
# TODO: add keep = First
4380
4385
def nlargest (self , n : int , columns : 'Any' ) -> 'DataFrame' :
0 commit comments