Search code examples
pythonpandasdatetimepython-datetime

Categorise hour into four different slots of 15 mins


I am working on a dataframe and I want to group the data for an hour into 4 different slots of 15 mins,

0-15 - 1st slot 15-30 - 2nd slot 30-45 - 3rd slot 45-00(or 60) - 4th slot

I am not even able to think, how to go forward with this

Here is the dataframe

I tried extracting hours, minutes and seconds from the time, but what to do now?


Solution

  • Use integer division by 15 and then add 1:

    df = pd.DataFrame({'M': range(60)})
    
    df['slot'] = df['M'] // 15 + 1
    

    print (df)
         M  slot
    0    0     1
    1    1     1
    2    2     1
    3    3     1
    4    4     1
    5    5     1
    6    6     1
    7    7     1
    8    8     1
    9    9     1
    10  10     1
    11  11     1
    12  12     1
    13  13     1
    14  14     1
    15  15     2
    16  16     2
    17  17     2
    18  18     2
    19  19     2
    20  20     2
    21  21     2
    22  22     2
    23  23     2
    24  24     2
    25  25     2
    26  26     2
    27  27     2
    28  28     2
    29  29     2
    30  30     3
    31  31     3
    32  32     3
    33  33     3
    34  34     3
    35  35     3
    36  36     3
    37  37     3
    38  38     3
    39  39     3
    40  40     3
    41  41     3
    42  42     3
    43  43     3
    44  44     3
    45  45     4
    46  46     4
    47  47     4
    48  48     4
    49  49     4
    50  50     4
    51  51     4
    52  52     4
    53  53     4
    54  54     4
    55  55     4
    56  56     4
    57  57     4
    58  58     4
    59  59     4