@@ -727,46 +727,58 @@ def matrix_transpose(x, /):
727727 return swapaxes (x , - 1 , - 2 )
728728
729729
730- def _partition_dispatcher (a , kth , axis = None , kind = None , order = None ):
730+ def _partition_dispatcher (a , kth , axis = None , kind = None , order = None , descending = None ):
731731 return (a ,)
732732
733733
734734@array_function_dispatch (_partition_dispatcher )
735- def partition (a , kth , axis = - 1 , kind = 'introselect' , order = None ):
735+ def partition (a , kth , axis = - 1 , kind = np . _NoValue , order = None , descending = np . _NoValue ):
736736 """
737737 Return a partitioned copy of an array.
738738
739739 Creates a copy of the array and partially sorts it in such a way that
740- the value of the element in k-th position is in the position it would be
741- in a sorted array. In the output array, all elements smaller than the k-th
742- element are located to the left of this element and all equal or greater
743- are located to its right. The ordering of the elements in the two
744- partitions on the either side of the k-th element in the output array is
745- undefined.
740+ the value of the element in the k-th position is in the position it would be
741+ in a sorted array. In the output array, all elements that would be to the left
742+ of the k-th element in a sorted array are located to the left of this element and
743+ all that would be to the right are located to its right. The ordering of the
744+ elements in the two partitions on the either side of the k-th element in the
745+ output array is undefined.
746746
747747 Parameters
748748 ----------
749749 a : array_like
750750 Array to be sorted.
751751 kth : int or sequence of ints
752- Element index to partition by. The k-th value of the element
753- will be in its final sorted position and all smaller elements
754- will be moved before it and all equal or greater elements behind
755- it. The order of all elements in the partitions is undefined. If
752+ Element index to partition by. In the returned array, the k-th
753+ value of the array will be in the position it would be in a
754+ sorted array, all elements that are less than this element (or
755+ greater if `descending` is True) will be moved before it, and
756+ all elements that are greater than or equal to this element
757+ (or less than or equal if `descending` is True) will be moved after it.
758+ The order of all elements within each partition is undefined. If
756759 provided with a sequence of k-th it will partition all elements
757- indexed by k-th of them into their sorted position at once.
760+ indexed by k-th of them into their sorted position at once.
758761
759762 axis : int or None, optional
760763 Axis along which to sort. If None, the array is flattened before
761764 sorting. The default is -1, which sorts along the last axis.
762765 kind : {'introselect'}, optional
763- Selection algorithm. Default is 'introselect'.
766+ NumPy currently offers only one selection algorithm, 'introselect',
767+ and this parameter provides no additional functionality. Default
768+ is ``None``.
764769 order : str or list of str, optional
765770 When `a` is an array with fields defined, this argument
766771 specifies which fields to compare first, second, etc. A single
767772 field can be specified as a string. Not all fields need be
768773 specified, but unspecified fields will still be used, in the
769774 order in which they come up in the dtype, to break ties.
775+ descending : bool, optional
776+ Sort order. If ``True``, the array will be partitioned in
777+ descending order. If ``False`` or ``None``, the array will be
778+ partitioned in ascending order. Values that are NaN are partitioned
779+ towards the end of the array regardless of order. Default: ``None``.
780+
781+ .. versionadded:: 2.6.0
770782
771783 Returns
772784 -------
@@ -803,7 +815,8 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
803815 the real parts except when they are equal, in which case the order
804816 is determined by the imaginary parts.
805817
806- The sort order of ``np.nan`` is bigger than ``np.inf``.
818+ Regardless of sort order, `np.nan` is partitioned to the right of
819+ any other value.
807820
808821 Examples
809822 --------
@@ -839,16 +852,24 @@ def partition(a, kth, axis=-1, kind='introselect', order=None):
839852 axis = - 1
840853 else :
841854 a = asanyarray (a ).copy (order = "K" )
842- a .partition (kth , axis = axis , kind = kind , order = order )
855+
856+ # Sanitize for backward compatibility
857+ kwargs = {}
858+ if descending is not np ._NoValue :
859+ kwargs ['descending' ] = descending
860+ if kind is not np ._NoValue :
861+ kwargs ['kind' ] = kind
862+
863+ a .partition (kth , axis = axis , order = order , ** kwargs )
843864 return a
844865
845866
846- def _argpartition_dispatcher (a , kth , axis = None , kind = None , order = None ):
867+ def _argpartition_dispatcher (a , kth , axis = None , kind = None , order = None , descending = None ):
847868 return (a ,)
848869
849870
850871@array_function_dispatch (_argpartition_dispatcher )
851- def argpartition (a , kth , axis = - 1 , kind = 'introselect' , order = None ):
872+ def argpartition (a , kth , axis = - 1 , kind = np . _NoValue , order = None , descending = np . _NoValue ):
852873 """
853874 Perform an indirect partition along the given axis using the
854875 algorithm specified by the `kind` keyword. It returns an array of
@@ -860,24 +881,36 @@ def argpartition(a, kth, axis=-1, kind='introselect', order=None):
860881 a : array_like
861882 Array to sort.
862883 kth : int or sequence of ints
863- Element index to partition by. The k-th element will be in its
864- final sorted position and all smaller elements will be moved
865- before it and all larger elements behind it. The order of all
866- elements in the partitions is undefined. If provided with a
867- sequence of k-th it will partition all of them into their sorted
868- position at once.
884+ Element index to partition by. In the returned array, the k-th
885+ value of the array will be in the position it would be in a
886+ sorted array, all elements that are less than this element (or
887+ greater if `descending` is True) will be moved before it, and
888+ all elements that are greater than or equal to this element
889+ (or less than or equal if `descending` is True) will be moved after it.
890+ The order of all elements within each partition is undefined. If
891+ provided with a sequence of k-th it will partition all elements
892+ indexed by k-th of them into their sorted position at once.
869893
870894 axis : int or None, optional
871895 Axis along which to sort. The default is -1 (the last axis). If
872896 None, the flattened array is used.
873897 kind : {'introselect'}, optional
874- Selection algorithm. Default is 'introselect'
898+ NumPy currently offers only one selection algorithm, 'introselect',
899+ and this parameter provides no additional functionality. Default
900+ is ``None``.
875901 order : str or list of str, optional
876902 When `a` is an array with fields defined, this argument
877903 specifies which fields to compare first, second, etc. A single
878904 field can be specified as a string, and not all fields need be
879905 specified, but unspecified fields will still be used, in the
880906 order in which they come up in the dtype, to break ties.
907+ descending : bool, optional
908+ Sort order. If ``True``, the array will be partitioned in
909+ descending order. If ``False`` or ``None``, the array will be
910+ partitioned in ascending order. Values that are NaN are partitioned
911+ towards the end of the array regardless of order. Default: ``None``.
912+
913+ .. versionadded:: 2.6.0
881914
882915 Returns
883916 -------
@@ -931,7 +964,14 @@ def argpartition(a, kth, axis=-1, kind='introselect', order=None):
931964 [1, 1, 3]])
932965
933966 """
934- return _wrapfunc (a , 'argpartition' , kth , axis = axis , kind = kind , order = order )
967+ # Sanitize for backward compatibility
968+ kwargs = {}
969+ if descending is not np ._NoValue :
970+ kwargs ['descending' ] = descending
971+ if kind is not np ._NoValue :
972+ kwargs ['kind' ] = kind
973+
974+ return _wrapfunc (a , "argpartition" , kth , axis = axis , order = order , ** kwargs )
935975
936976
937977def _sort_dispatcher (
0 commit comments