You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/06/07 16:01:17 UTC

[GitHub] [arrow] jorisvandenbossche commented on a diff in pull request #13327: ARROW-16706: [Python] Expose RankOptions

jorisvandenbossche commented on code in PR #13327:
URL: https://github.com/apache/arrow/pull/13327#discussion_r891417467


##########
python/pyarrow/_compute.pyx:
##########
@@ -2047,6 +2047,60 @@ class RandomOptions(_RandomOptions):
         self._set_options(length, initializer)
 
 
+cdef class _RankOptions(FunctionOptions):
+
+    _tiebreaker_map = {
+        "Min": CRankOptionsTiebreaker_MIN,
+        "Max": CRankOptionsTiebreaker_MAX,
+        "First": CRankOptionsTiebreaker_FIRST,
+        "Dense": CRankOptionsTiebreaker_DENSE,
+    }
+
+    def _set_options(self, sort_keys, null_placement, tiebreaker):
+        cdef vector[CSortKey] c_sort_keys
+        for name, order in sort_keys:
+            c_sort_keys.push_back(
+                CSortKey(tobytes(name), unwrap_sort_order(order))
+            )
+        try:
+            self.wrapped.reset(
+                new CRankOptions(c_sort_keys, unwrap_null_placement(null_placement), self._tiebreaker_map[tiebreaker])
+            )
+        except KeyError:
+            _raise_invalid_function_option(tiebreaker, "tiebreaker")
+
+
+class RankOptions(_RankOptions):
+    """
+    Options for the `rank` function.
+
+    Parameters
+    ----------
+    sort_keys : sequence of (name, order) tuples

Review Comment:
   I would take a look at how we do this for the sort kernels, as that has similar problems



##########
python/pyarrow/_compute.pyx:
##########
@@ -2047,6 +2047,60 @@ class RandomOptions(_RandomOptions):
         self._set_options(length, initializer)
 
 
+cdef class _RankOptions(FunctionOptions):
+
+    _tiebreaker_map = {
+        "Min": CRankOptionsTiebreaker_MIN,
+        "Max": CRankOptionsTiebreaker_MAX,
+        "First": CRankOptionsTiebreaker_FIRST,
+        "Dense": CRankOptionsTiebreaker_DENSE,
+    }
+
+    def _set_options(self, sort_keys, null_placement, tiebreaker):
+        cdef vector[CSortKey] c_sort_keys
+        for name, order in sort_keys:
+            c_sort_keys.push_back(
+                CSortKey(tobytes(name), unwrap_sort_order(order))
+            )
+        try:
+            self.wrapped.reset(
+                new CRankOptions(c_sort_keys, unwrap_null_placement(null_placement), self._tiebreaker_map[tiebreaker])
+            )
+        except KeyError:
+            _raise_invalid_function_option(tiebreaker, "tiebreaker")
+
+
+class RankOptions(_RankOptions):
+    """
+    Options for the `rank` function.
+
+    Parameters
+    ----------
+    sort_keys : sequence of (name, order) tuples
+        Names of field/column keys to sort the input on,
+        along with the order each field/column is sorted in.
+        Accepted values for `order` are "ascending", "descending".
+    null_placement : str, default "at_end"
+        Where nulls in input should be sorted, only applying to
+        columns/fields mentioned in `sort_keys`.
+        Accepted values are "at_start", "at_end".
+    tiebreaker : str, default "First"
+        Configure how ties between equal values are handled.
+        Accepted values are:
+
+        - "Min": Ties get the smallest possible rank in sorted order.

Review Comment:
   I think we generally use lower case values in Python for keyword value options, so would take "min", "max", etc here



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org