[Python] Create a categorical column from a datetime column in Python

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have a dataframe with a column of datetime type that I'd like to create a 2nd categorical column based upon. The second column should consist of string identifiers using the following logic:

    ON: [16:00-09:30)
    FH: [09:30-10:00)
    M: [10:00-15:00)
    SLH: [15:00-15:30)
    LH: [15:30-16:00)


    For instance, for dataframe d:

    date_rng = pd.date_range(start='2024-01-01', periods=48, freq='30min')
    d = pd.DataFrame({'datetime': date_rng})



    I'd like the output below:

    d.head(40)
    Out[186]:
    datetime part
    0 2024-01-01 00:00:00 ON
    1 2024-01-01 00:30:00 ON
    2 2024-01-01 01:00:00 ON
    3 2024-01-01 01:30:00 ON
    4 2024-01-01 02:00:00 ON
    5 2024-01-01 02:30:00 ON
    6 2024-01-01 03:00:00 ON
    7 2024-01-01 03:30:00 ON
    8 2024-01-01 04:00:00 ON
    9 2024-01-01 04:30:00 ON
    10 2024-01-01 05:00:00 ON
    11 2024-01-01 05:30:00 ON
    12 2024-01-01 06:00:00 ON
    13 2024-01-01 06:30:00 ON
    14 2024-01-01 07:00:00 ON
    15 2024-01-01 07:30:00 ON
    16 2024-01-01 08:00:00 ON
    17 2024-01-01 08:30:00 ON
    18 2024-01-01 09:00:00 FH
    19 2024-01-01 09:30:00 FH
    20 2024-01-01 10:00:00 M
    21 2024-01-01 10:30:00 M
    22 2024-01-01 11:00:00 M
    23 2024-01-01 11:30:00 M
    24 2024-01-01 12:00:00 M
    25 2024-01-01 12:30:00 M
    26 2024-01-01 13:00:00 M
    27 2024-01-01 13:30:00 M
    28 2024-01-01 14:00:00 M
    29 2024-01-01 14:30:00 M
    30 2024-01-01 15:00:00 SLH
    31 2024-01-01 15:30:00 LH
    32 2024-01-01 16:00:00 ON
    33 2024-01-01 16:30:00 ON
    34 2024-01-01 17:00:00 ON
    35 2024-01-01 17:30:00 ON
    36 2024-01-01 18:00:00 ON
    37 2024-01-01 18:30:00 ON
    38 2024-01-01 19:00:00 ON
    39 2024-01-01 19:30:00 ON

    Continue reading...

Compartilhe esta Página