I need for each day to know what the max value of the previous day was, example:
array with dates
date = np.array(['05/12/2017', '05/12/2017', '05/13/2017', '05/13/2017', '05/13/2017',
'05/13/2017', '05/14/2017', '05/15/2017', '05/15/2017', '05/15/2017',
'05/15/2017', '05/15/2017', '05/16/2017', '05/16/2017', '05/16/2017',
'05/16/2017', '05/16/2017' '05/16/2017', '05/17/2017', '05/17/2017'])
array with values:
value = np.array([13, 4, 5, 4, 17, 8, 5, 9, 17, 6, 11, 16, 12, 7, 7, 12, 17, 10, 16, 14])
result I need:
result = np.array([0, 0, 13, 13, 13, 13, 17, 5, 5, 5, 5, 5, 17, 17, 17, 17, 17, 17, 17, 17])
CodePudding user response:
Note that you have a missing comma in the dates array.
import numpy as np
from datetime import datetime, timedelta
from collections import defaultdict
dates = np.array(['05/12/2017', '05/12/2017', '05/13/2017', '05/13/2017', '05/13/2017',
'05/13/2017', '05/14/2017', '05/15/2017', '05/15/2017', '05/15/2017',
'05/15/2017', '05/15/2017', '05/16/2017', '05/16/2017', '05/16/2017',
'05/16/2017', '05/16/2017', '05/16/2017', '05/17/2017', '05/17/2017'])
values = np.array([13, 4, 5, 4, 17, 8, 5, 9, 17, 6, 11, 16, 12, 7, 7, 12, 17, 10, 16, 14])
parsed_dates = np.array([datetime.strptime(_, "%m/%d/%Y") for _ in dates])
dv = zip(parsed_dates, values)
max_dates = defaultdict(lambda: 0)
for date, value in dv:
max_dates[date] = max(value, max_dates[date])
one_day = timedelta(days=1)
result = np.array([max_dates[d - one_day] for d in parsed_dates])