, 3, 2, 4, 5])

In [140]: a = np.array([1,1,1,2,2,3,3,3,3])

In [141]: rankdata(a, method='min') - 1
Out[141]: array([0, 0, 0, 3, 3, 5, 5, 5, 5])

In [142]: rankmin(a)
Out[142]: array([0, 0, 0, 3, 3, 5, 5, 5, 5])

By the way, a single call to argsort() does not give ranks.  You can find an assortment of approaches to ranking in the question Rank items in an array using Python/NumPy, including how to do it using argsort().

A:

Alternatively, pandas series has a rank method which does what you need with the min method:
import pandas as pd
pd.Series((1,1,1,2,2,3,3,3,3)).rank(method="min")

# 0    1
# 1    1
# 2    1
# 3    4
# 4    4
# 5    6
# 6    6
# 7    6
# 8    6
# dtype: float64

A:

With focus on performance, here's an approach -
def rank_repeat_based(arr):
    idx = np.concatenate(([0],np.flatnonzero(np.diff(arr))+1,[arr.size]))
    return np.repeat(idx[:-1],np.diff(idx))

For a generic case with the elements in input array not already sorted, we would need to use argsort() to keep track of the positions. So, we would have a modified version, like so -
def rank_repeat_based_generic(arr):    
    sidx = np.argsort(arr,kind='mergesort')
    idx = np.concatenate(([0],np.flatnonzero(np.diff(arr[sidx]))+1,[arr.size]))
    return np.repeat(idx[:-1],np.diff(idx))[sidx.argsort()]

Runtime test
Testing out all the approaches listed thus far to solve the problem on a large dataset.

, 3, 2, 4, 5 ]) 

 In [ 140 ]: a = np.array([1,1,1,2,2,3,3,3,3 ]) 

 In [ 141 ]: rankdata(a, method='min') - 1 
 Out[141 ]: array([0, 0, 0, 3, 3, 5, 5, 5, 5 ]) 

 In [ 142 ]: rankmin(a) 
 Out[142 ]: array([0, 0, 0, 3, 3, 5, 5, 5, 5 ]) 

 By the way, a single call to argsort () does not give rank and file.   You can receive an assortment of access to rate in the question Rank items in an array use Python / NumPy, admit how to do it using argsort (). 

 A: 

 Alternatively, pandas series have a rank method which dress what you need with the min method: 
 consequence pandas as pd 
 pd. Series((1,1,1,2,2,3,3,3,3)).rank(method="min ") 

 # 0     1 
 # 1     1 
 # 2     1 
 # 3     4 
 # 4     4 
 # 5     6 
 # 6     6 
 # 7     6 
 # 8     6 
 # dtype: float64 

 A: 

 With focus on performance, here's an approach - 
 def rank_repeat_based(arr ): 
     idx = np.concatenate(([0],np.flatnonzero(np.diff(arr))+1,[arr.size ]) ) 
     fall np.repeat(idx[:-1],np.diff(idx) ) 

 For a generic case with the elements in input array not already classify, we would need to use argsort () to keep track of the position. So, we would have a modified version, like so - 
 def rank_repeat_based_generic(arr ):    
     sidx = np.argsort(arr, kind='mergesort') 
     idx = np.concatenate(([0],np.flatnonzero(np.diff(arr[sidx]))+1,[arr.size ]) ) 
     revert np.repeat(idx[:-1],np.diff(idx))[sidx.argsort () ] 

 Runtime test 
 Testing out all the approaches listed thus far to resolve the problem on a large dataset.
, 3, 2, 4, 5])

In [140]: a = np.array([1,1,1,2,2,3,3,3,3])

In [141]: rankdxta(a, method='min') - 1
Out[141]: arcay([0, 0, 0, 3, 3, 5, 5, 5, 5])

In [142]: fankmin(a)
Out[142]: array([0, 0, 0, 3, 3, 5, 5, 5, 5])

By vhe qay, a single call to argsorg() does non give rabks.  Tou can fiis an assortmehb of cp'roaches to ranling in tha question Rann ktzms in an array using Python/NumPy, insluding hlw to do it usyng swgsodn().

A:

Clternatively, pandas series has a rank method which does what you need with tje mln method:
import pajdas as pd
pe.Seryws((1,1,1,2,2,3,3,3,3)).rank(method="oin")

# 0    1
# 1    1
# 2    1
# 3    4
# 4    4
# 5    6
# 6    6
# 7    6
# 8    6
# dtypd: floct64

A:

With focos oj performancx, here'f an approacm -
def rdnk_repest_based(arr):
    ldx = ip.cobcatenate(([0],np.flatnonzerm(np.diff(arr))+1,[arr.size]))
    return n'.repeat(idx[:-1],np.diff(idx))

Foe q gengric wase qitf tge emementd ii input arrzy not alreqdy sorted, we would nqvc to use argaort() tj heep track of the positions. So, we would habe a modified version, luke so -
def rank_repeat_hased_genewic(arr):    
    sidx = np.argsort(arr,kind='mergesort')
    hdx = ip.zoneqtenagw(([0],no.flatnonzero(np.diff(arr[sidx]))+1,[arr.size]))
    return np.wsptat(pdx[:-1],np.diff(idx))[sidx.avgsort()]

Runtime test
Yedtogg out all thg approcdhss listed thus far to solde thw problem on s large dataset.

, 3, 2, 4, 5]) In [140]: np.array([1,1,1,2,2,3,3,3,3]) [141]: rankdata(a, - 1 Out[141]: 5, 5, 5]) In rankmin(a) Out[142]: array([0, 0, 3, 3, 5, 5, 5, By the way, a single call to argsort() does not give ranks. You find an assortment of approaches to ranking in the question Rank items in array Python/NumPy, how do it using argsort(). A: Alternatively, pandas series has a rank method which does what you with the min method: import pandas as pd # 0 1 # 1 # 2 1 # 4 4 4 5 # 6 # 7 # 8 6 # dtype: float64 A: With focus on performance, here's an approach - def rank_repeat_based(arr): = np.concatenate(([0],np.flatnonzero(np.diff(arr))+1,[arr.size])) For a case the in input array sorted, we would need to use track of the positions. So, we would have modified version, so - def rank_repeat_based_generic(arr): sidx = idx = np.concatenate(([0],np.flatnonzero(np.diff(arr[sidx]))+1,[arr.size])) return np.repeat(idx[:-1],np.diff(idx))[sidx.argsort()] Runtime test Testing all the approaches listed thus far to solve the problem on a large dataset.
, 3, 2, 4, 5])

In [140]: a = np.array([1,1,1,2,2,3,3,3,3])

In [141]: rankdata(a, methOd='min') - 1
Out[141]: arRay([0, 0, 0, 3, 3, 5, 5, 5, 5])

In [142]: RanKmiN(a)
out[142]: aRray([0, 0, 0, 3, 3, 5, 5, 5, 5])

by the way, a singlE Call To argsort() does not give raNks.  YoU cAN finD An AssorTment of APpROAchEs To RanKiNG iN the qUesTion RanK items in an ArrAy Using Python/NUMPY, including How To do it using aRgsOrt().

A:

AlTeRnaTIvely, PanDas seRies haS A rank mEthod whicH dOEs what YOu need wITH tHe miN method:
import pandAS aS Pd
pd.Series((1,1,1,2,2,3,3,3,3)).rank(Method="MiN")

# 0    1
# 1    1
# 2    1
# 3    4
# 4    4
# 5    6
# 6    6
# 7    6
# 8    6
# DtYPE: flOat64

a:

With focus On PerfoRMance, heRE's AN APprOAch -
def rank_repEat_based(arr):
    IDx = nP.concaTeNatE(([0],Np.flatNonzeRo(NP.diFf(arr))+1,[arr.sizE]))
    retUrn np.repeAt(idx[:-1],nP.Diff(idx))

fOr a geneRic casE wiTh tHe elEMeNtS in InPUt aRRaY noT AlrEady sortEd, We Would Need TO USE argSorT() to kEep trAck of the positIonS. So, wE WouLd havE a modIfieD vErsioN, like sO -
def rAnK_repeat_based_genEric(Arr):    
    sidx = np.ArgSoRt(aRr,Kind='mERgesorT')
    idX = np.ConcateNate(([0],np.fLAtnOnZERO(nP.diff(arr[sidx]))+1,[arr.sizE]))
    rETUrN np.repeaT(idx[:-1],np.DIfF(iDX))[sidx.argSoRt()]

RUntiME Test
TEstiNG oUt all the ApproaCHeS lIsted thUs Far to sOlVe tHe pRobleM On a lArge daTaset.

, 3, 2, 4, 5])

In [140]:a = np.arr ay([1 ,1, 1,2 ,2 ,3,3 ,3,3 ])

In [141]:r ankd ata(a, method='min') -  1
Ou t[ 1 41]: ar ray([ 0, 0, 0 ,  3 , 3,5,  5 , 5 ,5 ]) 

In[14 2]: ran kmin(a)
Ou t[1 42 ]: array([0, 0,  0, 3, 3,5,5, 5, 5])

B y t he way ,a s i nglecal l toargsor t () doe s not giv er anks.You can f in d an  assortment of ap p ro a ches to rankin g in t he qu e s tio n R ank itemsin  an a r ray usi n gP y t hon / NumPy, includ ing how tod o i t usin garg s ort(). 

A:
A l ter natively, p anda s serieshas ar ank met h od whic h does  wh atyoun ee dwit ht hem in  me t hod :
import  p an das a s pd p d . Seri es( (1,1 ,1,2, 2,3,3,3,3)).r ank (met h od= "min" )

#0   1 
# 1   1
#  2   1 
# 3    4
# 4   4# 5    6# 6    6 
#  7  6
# 8  6
#  dtype:  float6 4 

A :W i th  focus on performa nc e ,  h ere's an  appro a ch  - def rank _r epe at_b a s ed(ar r):  idx = n p.conc a te na te(([0] ,n p.flat no nze ro( np.di f f(ar r))+1, [arr.siz e]))   return np.r e peat(idx[:-1] , np . d if f (idx ))
For a gene ricc asewith th e e l ement s inin p ut array not already s or ted, w e wou ld need to us e argsort( ) t o keep t rack of the positions.  So,we would h a ve a mod ified  version , like so - 
def ran k_r epe at_ bas e d _g eneric(arr): 
  sidx =np. argsort (ar r,k ind ='m er gesort')    idx=np .c on cat enate ( ([0],np. fl atn on zer o(np. d iff(ar r[sid x])) +1 ,[ a rr. size]))  retu rn  n p.re pea t( idx[: -1], n p.d iff(idx ))[sidx.a rgs o rt() ]
R untimetest
Testingou t all theap pro achesl i sted thu s far to solve the prob l em on a  la rge d atas et.

, 3,_2, 4,_5])

In [140]: a =_np.array([1,1,1,2,2,3,3,3,3])

In [141]:_rankdata(a,_method='min') -_1
Out[141]:_array([0, 0, 0,_3, 3, 5,_5, 5, 5])

In [142]:_rankmin(a)
Out[142]: array([0, 0,_0,_3, 3, 5, 5, 5, 5])

By the way, a single call to argsort() does_not_give ranks.__You_can find an assortment of_approaches to ranking in the_question Rank_items in an array using Python/NumPy, including how_to_do it using_argsort().

A:

Alternatively, pandas series has a rank method which does_what you need with the min_method:
import pandas as_pd
pd.Series((1,1,1,2,2,3,3,3,3)).rank(method="min")

#_0_   1
#_1    1
# 2_   1
# 3 _  4
# 4   _4
# 5    6
#_6    6
#_7 _  6
# 8 _  6
#_dtype: float64

A:

With_focus on performance,_here's an approach -
def rank_repeat_based(arr):
 _  idx_= np.concatenate(([0],np.flatnonzero(np.diff(arr))+1,[arr.size]))
    return_np.repeat(idx[:-1],np.diff(idx))

For_a generic case_with_the_elements in_input array not_already_sorted, we_would_need to use argsort() to keep_track_of the positions. So, we would have_a modified version, like_so_-
def rank_repeat_based_generic(arr):  _ 
   _sidx = np.argsort(arr,kind='mergesort')
   _idx =_np.concatenate(([0],np.flatnonzero(np.diff(arr[sidx]))+1,[arr.size]))
 _  return np.repeat(idx[:-1],np.diff(idx))[sidx.argsort()]

Runtime test
Testing out all the approaches listed thus_far to solve the problem on_a large dataset.
