pythonpandasdataframeseriestimecodes

How do I sort/group a pandas series of timecode by weekday, month, etc in Python?


I have a pandas series that I have extracted from a dataframe in Python 3.7. It contains a series of timecodes such as this:

17833    Sat, 27 Nov 2010 06:00:00 -0000
851      Fri, 04 Dec 2009 06:07:00 -0000
4806     Fri, 23 Mar 2012 06:02:15 -0000
16341    Sat, 20 Aug 2011 11:48:18 -0000
9444     Mon, 16 May 2011 08:06:53 -0000
                      ...               
3262     Fri, 16 Dec 2011 07:30:00 -0000
37554    Wed, 11 Apr 2012 02:20:34 -0000
37555    Wed, 11 Apr 2012 02:34:00 -0000
28471    Thu, 18 Feb 2010 04:46:00 -0000
30324    Thu, 28 Jun 2012 21:23:40 -0000

The numbers on the left are the indices of the original entries. I would like to be able to sort this series into a variety of alternative time formats such as grouping by weekday (group all "Sat", group all "Wed" etc.) or grouping by month ("Nov","May"). It would even be great to sort by hour on a 24 hour clock using this timecode information (all entries at hour 02, hour 06, etc.).

Target outputs would be (just sorting this sample):

by month

28471    Feb
4806     Mar
37554    Apr
37555    Apr
9444     May
                      ...
30324    Jun
16341    Aug
17833    Nov
851      Dec
3262     Dec

by weekday

9444     Mon
37554    Wed
37555    Wed
28471    Thu
30324    Thu
                      ...
4806     Fri
851      Fri
3262     Fri
16341    Sat
17833    Sat

by time

37554    02
37555    02
28471    04
17833    06
4806     06
                      ...     
851      06
3262     07
9444     08
16341    11
30324    21

I have already tried to use the pd.to_datetime() function but I am not sure what formatting to give to this function so that it can understand the series, clarification here could be helpful.


Solution

  • If you want exactly like your posted output, you can do, considering the column name as 'funded date':

    For month:

    s_month=pd.to_datetime(df['funded date']).dt.month_name().str[:3]
    s_month.reindex(pd.to_datetime(df['funded date']).dt.month.sort_values().index)
    

    28471    Feb
    4806     Mar
    37554    Apr
    37555    Apr
    9444     May
    30324    Jun
    16341    Aug
    17833    Nov
    851      Dec
    3262     Dec
    

    For Day:

    s_day=pd.to_datetime(df['funded date']).dt.day_name().str[:3]
    s_day.reindex(pd.to_datetime(df['funded date']).dt.dayofweek.sort_values().index)
    

    9444     Mon
    37554    Wed
    37555    Wed
    28471    Thu
    30324    Thu
    851      Fri
    4806     Fri
    3262     Fri
    17833    Sat
    16341    Sat