I have the following pandas dataframe representing some time series data when a certain signal was switched on (True state):
| datetime | state |
|---|---|
| 2021-01-01 01:00:00 | True |
| 2021-01-01 04:00:00 | True |
| 2021-01-01 05:30:00 | False |
| 2021-02-01 23:00:00 | True |
| 2021-03-01 01:30:00 | False |
| 2021-05-10 06:00:00 | True |
I need to calculate for how long was the signal in True state for each day as shown in the table below.
| datetime | duration(1h) |
|---|---|
| 01.01.2021 | 4.5 |
| 02.01.2021 | 1 |
| 03.01.2021 | 1.5 |
| 04.01.2021 | 0 |
| 05.01.2021 | 18 |
I tried to do it on the database level using InfluxQL, but with no result so I decided to do it in Python instead.
Here's the example dataframe:
import pandas as pd
d = {'state': [True, True, False, True, False, True], 'datetime': ['2021-01-01T01:00:00Z', '2021-01-01T04:00:00Z', '2021-01-01T05:30:00Z', '2021-01-02T23:00:00Z', '2021-01-03T01:30:00Z', '2021-01-05T06:00:00Z']}
df = pd.DataFrame(data=d)
df = df.set_index(pd.to_datetime(df['datetime'])) # set datetime as DatetimeIndex
I've tried options with converting the boolean values to integers and then using scipy.integrate to get the area beneath the plot corresponding with the duration. Also tried some approaches with getting the timedelta between the consecutive point and doing some cumulative sum there, but no luck again.
I think the main problem is the grouping by days (notice the True duration between 2021-02-01 23:00:00 and 2021-03-01 01:30:00 - it aggregates to 1h on 2021-02-01 and 1.5h on 2021-03-01).
If anyone has any advice on has to achieve this with pandas, I'd really appreciate it.