[Python] How to represent ranges of time in a pandas index

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

  1. Stack

    Stack Membro Participativo

    I have a collection of user data as follows:

    user​
    start​
    end
    John Doe​
    2025-03-21 11:30:35​
    2025-03-21 13:05:26
    ...​
    ...​
    ...
    Jane Doe​
    2023-12-31 01:02:03​
    2024-01-02 03:04:05​

    Each user has a start and end datetime of some activity. I would like to place this temporal range in the index so I can quickly query the dataframe to see which users were active during a certain date/time range like so:

    df['2024-01-01:2024-01-31']


    Pandas has Period objects, but these seem to only support a specific year, day, or minute, not an arbitrary start and end datetime. Pandas also has MultiIndex indices, but these seem to be designed for hierarchical categorical labels, not for time ranges. Any other ideas for how to represent this time range in an index?

    Continue reading...

Compartilhe esta Página