The Worldâ€™s Largest Online Community for Developers

';
python - Converting list of Datetimes with missing occurrences to list of check-ins missed and not-missed? - LavOzs.Com
python python-3.x list datetime

So I have an ordered list of datetimes

`T = [t1,t2...tn]`

With the expectation that on any given day, there ought to be two checkins or more, but the data is unreliable. Days could be missing or there could only be one data point per day. Furthermore, if the only timestamp for the first day occurs after midday, that's okay. It's also okay if the only timestamp for the last day occurs before midday.
What I need to do is convert a given list of timestamps into two lists:
`times_had, times_missed`

Such that each of them contains the relative ordering of whatever timestamp they're tracking.

For instance:

```
T = [(01/01 11:30), (01/01 23:30), (01/02 23:50), (01/03 11:00)]
Times_had = [1,2,4,5]
Times_missing = [3]
#And
T = [(01/01 12:30), (01/01 23:30), (01/01 23:45), (01/02 23:50), (01/04 15:30)]
Times_had = [1,2,3,5,9]
Times_missed = [4,6,7,8]
```

I think what I have works, but even then it is unseemly at best.

```
import datetime
from collections import Counter
def parameterize_times(lst,t_day = 2):
#t_day = number of times to fill in for a missing day -- to be implemented
start,end,times_missed,times_had = lst[0], lst[len(lst)-1], [], []
dates = date_array = [i.date() for i in list(start + datetime.timedelta(days=x) for x in range(0, (end-start).days+1))]
date_array = {key : 0 for key in date_array}
vals = dict(Counter([i.date() for i in lst]))
for i in vals: date_array[i] = vals[i]
t = 1
#date array -- dictionary([t1:m1, t2:m2...]) where tn is day n, mn is the number of timestamps at day tn
#date dict -- dictionary([t1:[x1,x2...],t2:[x1...]]), tn is a day, [x1,x2....] list of that days' timestamps
date_dict = {key.date():[] for key in lst}
for i in lst: date_dict[i.date()].append(i)
first = True
last = False
for i in date_array.keys():
if i == lst[len(lst)-1].date(): last = True
if first and date_array[i] == 1:
if date_dict[i][0].hour <12:
times_had.append(t)
t+=1
times_missed.append(t)
t+=1
else:
times_had.append(t)
t+=1
first = False
elif first and date_array[i]>1:
first = False
if last and date_array[i]==1:
if date_dict[i][0].hour>12:
times_missed.append(t)
t+=1
times_had.append(t)
t+=1
else:
times_had.append(t)
t+=1
if not first and not last:
if date_array[i] == 0:
for k in range(0,t_day):
times_missed.append(t)
t+=1
elif date_array[i] > 1:
for k in range(date_array[i]):
times_had.append(t)
t+=1
else:
if date_dict[i][0].hour>=12:
times_missed.append(t)
t+=1
times_had.append(t)
t+=1
else:
times_had.append(t)
t+=1
times_missed.append(t)
t+=1
return times_had,times_missed
```

Related

How do I check if a list is empty?

How do I check whether a file exists without exceptions?

Finding the index of an item in a list

What is the difference between Python's list methods append and extend?

Should I use the datetime or timestamp data type in MySQL?

Converting string into datetime

How to make a flat list out of list of lists?

How do I concatenate two lists in Python?

How can I count the occurrences of a list item?

How to clone or copy a list?

How do I check if a list is empty?

How do I check whether a file exists without exceptions?

Finding the index of an item in a list

What is the difference between Python's list methods append and extend?

Should I use the datetime or timestamp data type in MySQL?

Converting string into datetime

How to make a flat list out of list of lists?

How do I concatenate two lists in Python?

How can I count the occurrences of a list item?

How to clone or copy a list?