How to calculate runtime from state and time using python

I have a circulation pump that I check, tie it on or off, and this is not some kind of fixed interval. For one day, which could give me a dataset similar to this, where β€œvalue” means turning the pump on or off.

data=(
 {'value': 0, 'time': datetime.datetime(2011, 1, 18, 7, 58, 25)},
 {'value': 1, 'time': datetime.datetime(2011, 1, 18, 8, 0, 3)},
 {'value': 0, 'time': datetime.datetime(2011, 1, 18, 8, 32, 10)},
 {'value': 0, 'time': datetime.datetime(2011, 1, 18, 9, 22, 7)},
 {'value': 1, 'time': datetime.datetime(2011, 1, 18, 9, 30, 58)},
 {'value': 1, 'time': datetime.datetime(2011, 1, 18, 12, 2, 23)},
 {'value': 0, 'time': datetime.datetime(2011, 1, 18, 15, 43, 11)},
 {'value': 1, 'time': datetime.datetime(2011, 1, 18, 20, 14, 55)})

The format is not so important and can be changed.

I want to know how to calculate how many minutes (or time intervals or something else) 'value'were 0 or 1 (either ON or OFF)?

This is just a small sample of data, it stretched over several years, so it can be a lot. I use numpy / mathplotlib to plot some graphs, and there might be something in numpy for this, but I'm not good enough at that.

Edit

, , . - ...

0 04:42:13  
1 07:34:17
+5
1

, , ? , , , itertools.groupby :

>>> from itertools import groupby
>>> for i, grp in groupby(data, key=lambda x: x['value']):
    lst = [x['time'] for x in grp]
    print(i, max(lst) - min(lst))


0 0:00:00
1 0:00:00
0 0:49:57
1 2:31:25
0 0:00:00
1 0:00:00

, , ( ).

, , .


: /, :

>>> sums = {0:datetime.timedelta(0), 1:datetime.timedelta(0)}
>>> for cur, nex in zip(data, data[1:]):
    sums[cur['value']] += nex['time'] - cur['time']


>>> for i, j in sums.items():
    print(i, j)


0 5:32:10
1 6:44:20

/, itertools.groupby. py3k, py2k.

+2

All Articles