[Python] Convert Date Ranges to Time Series in Pandas

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

  1. Stack

    Stack Membro Participativo

    My raw data looks like the following:

    start_date end_date value
    0 2016-01-01 2016-01-03 2
    1 2016-01-05 2016-01-08 4


    The interpretation is that the data takes a value of 2 between 1/1/2016 and 1/3/2016, and it takes a value of 4 between 1/5/2016 and 1/8/2016. I want to transform the raw data to a daily time series like the following:

    2016-01-01 2
    2016-01-02 2
    2016-01-03 2
    2016-01-04 0
    2016-01-05 4
    2016-01-06 4
    2016-01-07 4
    2016-01-08 4


    Note that if a date in the time series doesn't appear between the start_date and end_date in any row of the raw data, it gets a value of 0 in the time series.

    I can create the time series by looping through the raw data, but that's slow. Is there a faster way to do it?

    Continue reading...

Compartilhe esta Página