0

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])

1 Answers1

1

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])
mattan
  • 93
  • 5
  • that worked in a beautiful way with the example given. I am using it with intraday stock data so that I need the max value of the previous day but every n days (for example 4) it gives a day of zeros... and I cant't figure out why – Philippus Mercator Jul 03 '22 at 10:28
  • 1
    okay, I figured out that this happens because my data is contained in business days only and when it subtracts a day from monday for example i get no data... any ideas on how to do max_dates[d - one_business_day] ? – Philippus Mercator Jul 03 '22 at 10:52
  • 1
    see https://stackoverflow.com/a/19036752/9039523 – mattan Jul 04 '22 at 08:45