@@ -794,92 +794,51 @@ def _pad_inplace_with_limit(values: np.ndarray, mask: np.ndarray, limit: int) ->
794
794
# val = values[j, i]
795
795
796
796
797
- # @cython.boundscheck(False)
798
- # @cython.wraparound(False)
799
- # def is_monotonic(ndarray[algos_t, ndim=1] arr, bint timelike):
800
- # """
801
- # Returns
802
- # -------
803
- # tuple
804
- # is_monotonic_inc : bool
805
- # is_monotonic_dec : bool
806
- # is_unique : bool
807
- # """
808
- # cdef:
809
- # Py_ssize_t i, n
810
- # algos_t prev, cur
811
- # bint is_monotonic_inc = 1
812
- # bint is_monotonic_dec = 1
813
- # bint is_unique = 1
814
- # bint is_strict_monotonic = 1
815
-
816
- # n = len(arr)
817
-
818
- # if n == 1:
819
- # if arr[0] != arr[0] or (timelike and <int64_t>arr[0] == NPY_NAT):
820
- # # single value is NaN
821
- # return False, False, True
822
- # else:
823
- # return True, True, True
824
- # elif n < 2:
825
- # return True, True, True
797
+ def is_monotonic (arr : np .ndarray ) -> tuple [bool , bool , bool ]:
798
+ """
799
+ Returns
800
+ -------
801
+ tuple
802
+ is_monotonic_inc : bool
803
+ is_monotonic_dec : bool
804
+ is_unique : bool
805
+ """
806
+ is_monotonic_inc = True
807
+ is_monotonic_dec = True
808
+ is_unique = True
809
+ is_strict_monotonic = True
826
810
827
- # if timelike and <int64_t>arr[0] == NPY_NAT:
828
- # return False, False, True
811
+ n = len (arr )
829
812
830
- # if algos_t is not object:
831
- # with nogil:
832
- # prev = arr[0]
833
- # for i in range(1, n):
834
- # cur = arr[i]
835
- # if timelike and <int64_t>cur == NPY_NAT:
836
- # is_monotonic_inc = 0
837
- # is_monotonic_dec = 0
838
- # break
839
- # if cur < prev:
840
- # is_monotonic_inc = 0
841
- # elif cur > prev:
842
- # is_monotonic_dec = 0
843
- # elif cur == prev:
844
- # is_unique = 0
845
- # else:
846
- # # cur or prev is NaN
847
- # is_monotonic_inc = 0
848
- # is_monotonic_dec = 0
849
- # break
850
- # if not is_monotonic_inc and not is_monotonic_dec:
851
- # is_monotonic_inc = 0
852
- # is_monotonic_dec = 0
853
- # break
854
- # prev = cur
855
- # else:
856
- # # object-dtype, identical to above except we cannot use `with nogil`
857
- # prev = arr[0]
858
- # for i in range(1, n):
859
- # cur = arr[i]
860
- # if timelike and <int64_t>cur == NPY_NAT:
861
- # is_monotonic_inc = 0
862
- # is_monotonic_dec = 0
863
- # break
864
- # if cur < prev:
865
- # is_monotonic_inc = 0
866
- # elif cur > prev:
867
- # is_monotonic_dec = 0
868
- # elif cur == prev:
869
- # is_unique = 0
870
- # else:
871
- # # cur or prev is NaN
872
- # is_monotonic_inc = 0
873
- # is_monotonic_dec = 0
874
- # break
875
- # if not is_monotonic_inc and not is_monotonic_dec:
876
- # is_monotonic_inc = 0
877
- # is_monotonic_dec = 0
878
- # break
879
- # prev = cur
880
-
881
- # is_strict_monotonic = is_unique and (is_monotonic_inc or is_monotonic_dec)
882
- # return is_monotonic_inc, is_monotonic_dec, is_strict_monotonic
813
+ if n == 1 :
814
+ if arr [0 ] != arr [0 ]:
815
+ # single value is NaN/NaT
816
+ return False , False , True
817
+ else :
818
+ return True , True , True
819
+ elif n < 2 :
820
+ return True , True , True
821
+
822
+ prev = arr [0 ]
823
+ for i in range (1 , n ):
824
+ cur = arr [i ]
825
+ if cur < prev :
826
+ is_monotonic_inc = False
827
+ elif cur > prev :
828
+ is_monotonic_dec = False
829
+ elif cur == prev :
830
+ is_unique = False
831
+ else :
832
+ # cur or prev is NaN/NaT
833
+ is_monotonic_inc = False
834
+ is_monotonic_dec = False
835
+ break
836
+ if not is_monotonic_inc and not is_monotonic_dec :
837
+ break
838
+ prev = cur
839
+
840
+ is_strict_monotonic = is_unique and (is_monotonic_inc or is_monotonic_dec )
841
+ return is_monotonic_inc , is_monotonic_dec , is_strict_monotonic
883
842
884
843
885
844
# # ----------------------------------------------------------------------
0 commit comments