1

I have dates list with missing dates like

['2018-06-01', '2018-06-02', '2018-06-03', '2018-06-06']

And corresponding values list like

[3,5,3,7]

How can I add missing dates in sorted list and add 0 for corresponding index in values

Above values I parsed from below data

data = defaultdict(Counter)
defaultdict(<class 'collections.Counter'>, {'2018-06-01': Counter({u'Values': 1}), '2018-06-03': Counter({u'Values': 2})}

If I can add missing dates in defaultdict that will work as well.

It's not duplicate I don't just want to create dates I have to updates corresponding values list.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
user2661518
  • 2,677
  • 9
  • 42
  • 79

1 Answers1

3

You create real datetimes from your data, begin and enddate. Then you create all possible dates between and assign a value of 0, then update for existing ones.

import datetime

dates = ['2018-06-01', '2018-06-02', '2018-06-03', '2018-06-06']
occ = [3,5,3,7]

startDate = datetime.datetime.strptime( dates[0], "%Y-%m-%d") # parse first date
endDate   = datetime.datetime.strptime( dates[-1],"%Y-%m-%d") # parse last date 
days = (endDate - startDate).days  # how many days between?

# create a dictionary of all dates with 0 occurences
allDates = {datetime.datetime.strftime(startDate+datetime.timedelta(days=k), 
                                       "%Y-%m-%d"):0 for k in range(days+1)}

# update dictionary with existing occurences (zip creates (date,number) tuples)
allDates.update(  zip(dates,occ) )

# sort the unsorted dict, decompose its items & zip then, wich generates your lists again
datesAfter,occAfter = map(list,zip(*sorted(allDates.items())))
print(datesAfter)
print(occAfter)

print(allDates)

Output:

['2018-06-01', '2018-06-02', '2018-06-03', '2018-06-04', '2018-06-05', '2018-06-06']
[3, 5, 3, 0, 0, 7]

{'2018-06-06': 7, 
 '2018-06-05': 0, 
 '2018-06-04': 0, 
 '2018-06-03': 3, 
 '2018-06-02': 5, 
 '2018-06-01': 3}

Link: zip()

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69