[Python] How to align Pandas Periods of frequency 12 hours to start and end at 00:00 and 12:00

Discussão em 'Python' iniciado por Stack, Abril 18, 2025.

  1. Stack

    Stack Membro Participativo

    I want to divide a large dataset into 12-hour periods of data starting/ending at midnight and noon each day.

    I was planning to use Pandas.Period for this but I noticed that it converts an arbitrary datetime to a 12-hour period beginning in the current hour, whereas what I want is the 12-hour period starting at 00:00 or 12:00 hours.

    import pandas as pd

    dt = pd.to_datetime("2025-04-17 18:35")
    current_period = dt.to_period(freq='12h')
    print(current_period)


    Output:

    2025-04-17 18:00


    What I want is the following period:

    2025-04-17 12:00

    Continue reading...

Compartilhe esta Página