In [1]:
import pandas as pd
df = pd.DataFrame([
('2015-08-25 16:52:10', 90),
('2015-08-25 16:52:12', 90),
('2015-08-25 16:52:14', 65),
('2015-08-25 16:52:16', 65),
('2015-08-25 16:52:18', 65), # time isn't at least 10 consecutive seconds so it shouldn't coulnt
('2015-08-25 16:52:20', 65),
('2015-08-25 16:52:22', 70),
('2015-08-25 16:52:24', 70),
('2015-08-25 16:52:26', 70),
('2015-08-25 16:52:48', 70), #time gap check, 88 lasts for >10 sec so it should count
('2015-08-25 16:52:50', 88),
('2015-08-25 16:52:52', 88),
('2015-08-25 16:52:54', 88),
('2015-08-25 16:52:56', 88),
('2015-08-25 16:52:58', 88),
('2015-08-25 16:53:00', 88),
('2015-08-25 16:53:02', 88),
('2015-08-25 16:53:04', 88),
('2015-08-25 16:53:06', 88),
('2015-08-25 16:53:08', 88),
('2015-08-25 16:53:10', 88)],
columns=['timestamp', 'x'])
In [2]:
#Find count of these ranges
below = 0 # v <=80
middle = 0 #v >= 81 and v<=84
above = 0 #v >=85 and v<=89
ls = []
b_dict = {}
m_dict = {}
a_dict = {}
for i, v in df['x'].iteritems():
if v <= 80: #below block
if not ls:
ls.append(v)
else:
if ls[0] >= 81: #if the range before was not below 80
if len(ls) >= 5: #if the range was greater than 10 seconds, set to 5 because data points are every 2
if ls[0] <= 84: #was it in the middle range?
m_dict[middle] = ls
middle += 1
ls = [v]
elif ls[0] >= 85 and ls[0] <=89: #was it in the above range?
a_dict[above] = ls
above += 1
ls = [v]
else: #old list wasn't long enough to count
ls = [v]
else: #if in the same range
ls.append(v)
elif v >= 81 and v<= 84: #middle block
if not ls:
ls.append(v)
else:
if ls[0] <= 80 or (ls[0]>=85 and ls[0]<= 89): #if not in the middle range
if len(ls) >= 5: #if range was greater than 10 seconds
if ls[0] <= 80: #was it in the below range?
b_dict[below] = ls
below += 1
ls = [v]
elif ls[0] >= 85 and ls[0] <=89: #was it in the above range?
a_dict[above] = ls
above += 1
ls = [v]
else: #old list wasn't long enough to count
ls = [v]
else:
ls.append(v)
elif v >= 85 and v <=89: #above block
if not ls:
ls.append(v)
else:
if ls[0] <=84 : #if not in the above range
if len(ls) >= 5: #if range was greater than
if ls[0] <= 80: #was it in the below range?
b_dict[below] = ls
below += 1
ls = [v]
elif ls[0] >= 81 and ls[0] <=84: #was it in the middle range?
m_dict[middle] = ls
middle += 1
ls = [v]
else: #old list wasn't long enough to count
ls = [v]
else:
ls.append(v)
else: #v>90 or something else weird. start the list over
ls = []
#final list check
if len(ls) >= 5:
if ls[0] <= 80: #was it in the below range?
b_dict[below] = ls
below += 1
ls = [v]
elif ls[0] >= 81 and ls[0] <=84: #was it in the middle range?
m_dict[middle] = ls
middle += 1
ls = [v]
elif ls[0] >= 85 and ls[0] <=89: #was it in the above range?
a_dict[above] = ls
above += 1
b_len = 0.0
for key, val in b_dict.iteritems():
b_len += len(val)
m_len = 0.0
for key, val in m_dict.iteritems():
m_len += len(val)
a_len = 0.0
for key, val in a_dict.iteritems():
a_len += len(val)
print "Below 80 count: %s" %below
print "Time below 80: %s min" %((b_len*2)/60)
print "Middle (81-84): %s" %middle
print "Time Middle (81-84): %s min" %((m_len*2)/60)
print "Above (85-89): %s" %above
print "Time Above (85-89): %s min" %((a_len*2)/60)
In [1]:
a_len
In [2]:
#did it sort correctly?
print "Above check"
for key, val in a_dict.iteritems():
print all(i >= 85 and i<=89 for i in val)
print "Middle check"
for key, val in m_dict.iteritems():
print all(i >= 81 and i<=84 for i in val)
print "Below check"
for key, val in b_dict.iteritems():
print all(i <=80 for i in val)
In [ ]:
a_dict
In [ ]: