[Python] Pandas continous time periods

Discussão em 'Python' iniciado por Stack, Setembro 1, 2025.

  1. Stack

    Stack Membro Participativo

    Given a table:

    id cost from to
    43 4 2025-09-01 01:00:00 2025-09-01 01:30:00
    42 4 2025-09-01 01:30:00 2025-09-01 02:00:00
    41 4.8 2025-09-01 02:00:00 2025-09-01 02:30:00
    40 4.05 2025-09-01 02:30:00 2025-09-01 03:00:00
    39 4 2025-09-01 03:00:00 2025-09-01 03:30:00
    37 5.16 2025-09-01 04:00:00 2025-09-01 04:30:00
    35 3.72 2025-09-01 05:00:00 2025-09-01 05:30:00
    34 5 2025-09-01 05:30:00 2025-09-01 06:00:00
    33 8.8 2025-09-01 06:00:00 2025-09-01 06:30:00
    24 11.97 2025-09-01 10:30:00 2025-09-01 11:00:00
    23 8.8 2025-09-01 11:00:00 2025-09-01 11:30:00

    import pandas as pd

    data = {
    "cost": {
    43: 4,
    42: 4,
    41: 4.8,
    40: 4.05,
    39: 4,
    37: 5.16,
    35: 3.72,
    34: 5,
    33: 8.8,
    24: 11.97,
    23: 8.8,
    },
    "from": {
    43: "2025-09-01 01:00:00+0100",
    42: "2025-09-01 01:30:00+0100",
    41: "2025-09-01 02:00:00+0100",
    40: "2025-09-01 02:30:00+0100",
    39: "2025-09-01 03:00:00+0100",
    37: "2025-09-01 04:00:00+0100",
    35: "2025-09-01 05:00:00+0100",
    34: "2025-09-01 05:30:00+0100",
    33: "2025-09-01 06:00:00+0100",
    24: "2025-09-01 10:30:00+0100",
    23: "2025-09-01 11:00:00+0100",
    },
    "to": {
    43: "2025-09-01 01:30:00+0100",
    42: "2025-09-01 02:00:00+0100",
    41: "2025-09-01 02:30:00+0100",
    40: "2025-09-01 03:00:00+0100",
    39: "2025-09-01 03:30:00+0100",
    37: "2025-09-01 04:30:00+0100",
    35: "2025-09-01 05:30:00+0100",
    34: "2025-09-01 06:00:00+0100",
    33: "2025-09-01 06:30:00+0100",
    24: "2025-09-01 11:00:00+0100",
    23: "2025-09-01 11:30:00+0100",
    },
    }

    table = pd.DataFrame(data)


    I'd like to know what the continuous periods are. i.e 01:00 to 03:30, 05:00 to 06:30, and 10:30 to 11:30.

    The period 4:00 to 4:30 would not be a continuous period because it does not continue with a time period before it or after it.

    The time periods could be of any length; in this example they are all 30 minute segments.

    How do I do this? Thank you.

    Continue reading...

Compartilhe esta Página