@@ -502,17 +502,18 @@ def to_dict(
502502 return {ser .name : ser .to_list () for ser in it }
503503
504504 def with_row_index (self , name : str , order_by : Sequence [str ] | None ) -> Self :
505- plx = self .__narwhals_namespace__ ()
506- if order_by is None :
507- import numpy as np # ignore-banned-import
505+ import numpy as np # ignore-banned-import
508506
509- data = pa .array (np .arange (len (self ), dtype = np .int64 ))
507+ plx = self .__narwhals_namespace__ ()
508+ data = pa .array (np .arange (len (self )))
509+ row_index_s = plx ._series .from_iterable (data , context = self , name = name )
510+ row_index = plx ._expr ._from_series (row_index_s )
511+ if order_by :
510512 row_index = plx ._expr ._from_series (
511- plx ._series .from_iterable (data , context = self , name = name )
513+ self .select (row_index , * (plx .col (x ) for x in order_by ))
514+ .sort (* order_by , descending = False , nulls_last = False )
515+ .get_column (name )
512516 )
513- else :
514- rank = plx .col (order_by [0 ]).rank ("ordinal" , descending = False )
515- row_index = (rank .over (partition_by = [], order_by = order_by ) - 1 ).alias (name )
516517 return self .select (row_index , plx .all ())
517518
518519 def filter (self , predicate : ArrowExpr ) -> Self :
0 commit comments