holdenk commented on code in PR #58254:
URL: https://github.com/apache/spark/pull/58254#discussion_r3858407201


##########
python/pyspark/pandas/series.py:
##########
@@ -4286,45 +4305,89 @@ def rank(
         y    b
         z    c
         Name: A, dtype: object
+
+        With pct=True, ranks are expressed as percentiles.
+
+        >>> s = ps.Series([1, 2, 2, 3], name='A')
+        >>> s.rank(pct=True)
+        0    0.25
+        1    0.625
+        2    0.625
+        3    1.0
+        Name: A, dtype: float64
+
+        With na_option='top', NaN values are assigned the smallest rank.
+
+        >>> s = ps.Series([1, float('nan'), 2, 3], name='A')
+        >>> s.rank(na_option='top')
+        0    2.0
+        1    1.0
+        2    3.0
+        3    4.0
+        Name: A, dtype: float64
+
+        With na_option='bottom', NaN values are assigned the largest rank.
+
+        >>> s.rank(na_option='bottom')
+        0    1.0
+        1    4.0
+        2    2.0
+        3    3.0
+        Name: A, dtype: float64
         """
+        validate_axis(axis)
         is_numeric = isinstance(self.spark.data_type, (NumericType, 
BooleanType))
         if numeric_only and not is_numeric:
             raise TypeError("Series.rank does not allow numeric_only=True with 
non-numeric dtype.")
         else:
-            return self._rank(method, ascending).spark.analyzed
+            return self._rank(method, ascending, na_option=na_option, 
pct=pct).spark.analyzed
 
     def _rank(
         self,
         method: str = "average",
         ascending: bool = True,
         *,
         part_cols: Sequence["ColumnOrName"] = (),
+        na_option: Literal["keep", "top", "bottom"] = "keep",
+        pct: bool = False,
     ) -> "Series":
         if method not in ["average", "min", "max", "first", "dense"]:
             msg = "method must be one of 'average', 'min', 'max', 'first', 
'dense'"
             raise ValueError(msg)
+        if na_option not in ["keep", "top", "bottom"]:
+            raise ValueError("na_option must be one of 'keep', 'top', 
'bottom'")
 
         if self._internal.index_level > 1:
             raise NotImplementedError("rank do not support MultiIndex now")
 
+        # Determine ordering with null placement based on na_option.
+        # 'top' always assigns the smallest rank to NaN, 'bottom' the largest,
+        # regardless of ascending direction.
         if ascending:
-            asc_func = PySparkColumn.asc
+            sort_col = (
+                self.spark.column.asc_nulls_first()
+                if na_option == "top"
+                else self.spark.column.asc_nulls_last()
+            )
+            nat_order_col = F.col(NATURAL_ORDER_COLUMN_NAME).asc()
         else:
-            asc_func = PySparkColumn.desc
+            sort_col = (
+                self.spark.column.desc_nulls_first()
+                if na_option == "top"
+                else self.spark.column.desc_nulls_last()
+            )
+            nat_order_col = F.col(NATURAL_ORDER_COLUMN_NAME).desc()
 
         if method == "first":
             window = (
-                Window.orderBy(
-                    asc_func(self.spark.column),
-                    asc_func(F.col(NATURAL_ORDER_COLUMN_NAME)),
-                )
+                Window.orderBy(sort_col, nat_order_col)

Review Comment:
   Yeah I think we can defer that to a follow up provided we do that ~soon.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to