Search code examples
pythonpandasdatetimeseriesindices

Return a pandas series of date time in chronological order by the original series' indices


I compiled a pandas series of date time like the following (the below shows part of the series as an example):

0   2002-02-03
1   1979-01-01
2   2006-12-25
3   2008-07-16
4   2005-05-30

Note: the dtype of each cell is 'pandas._libs.tslib.Timestamp'

For the above example, I would like to rank them by chronological order and return a series by the original series' indices like this (the second column):

0   1
1   0
2   3
3   4
4   2

I've tried using a mix of .order(), .sort(), and .index() to achieve this but to no avail so far. What will be the easiest way to do get a series of date time in chronological order by the original series' indices?

Thank you.


Solution

  • You can use Series.rank, subtract 1 and cast to int:

    a = df['date'].rank(method='dense').sub(1).astype(int)
    print (a)
    0    1
    1    0
    2    3
    3    4
    4    2
    Name: date, dtype: int32
    

    Parameter method in Series.rank:

    method : {'average', 'min', 'max', 'first', 'dense'}

    average: average rank of group
    min: lowest rank in group
    max: highest rank in group
    first: ranks assigned in order they appear in the array
    dense: like ‘min’, but rank always increases by 1 between groups