Question:
Overview:
I am looking for a vectorized way to get a first date that sees a certain state. A condition is found when the price in dfDays is > target price specified in dfWeeks.target . This condition must be removed after the targetโs date of installation.
Is there a way to do the following time series analysis, with apply or similarly, in a vectorized form in Pandas?
Data:
Create freq='D' test data file
np.random.seed(seed=1) rng = pd.date_range('1/1/2000', '2000-07-31',freq='D') weeks = np.random.uniform(low=1.03, high=3, size=(len(rng),)) ts2 = pd.Series(weeks ,index=rng) dfDays = pd.DataFrame({'price':ts2})
Now create a remarketing freq='1W-Mon' dataframe
dfWeeks = dfDays.resample('1W-Mon').first() dfWeeks['target'] = (dfWeeks['price'] + .5).round(2)
Use reindex to align the index like on df:
dfWeeks = dfWeeks.reindex(dfDays.index)
So dfWeeks is a data frame containing the target values โโthat we will use
dfWeeks.dropna().head() price target 2000-01-03 1.851533 2.35 2000-01-10 1.625595 2.13 2000-01-17 1.855813 2.36 2000-01-24 2.130619 2.63 2000-01-31 2.756487 3.26
If we focus on the first goal from dfWeeks
match = dfDays[dfDays.price >= dfWeeks.target.loc['2000-01-03']]
The first match in the past is so invalid, so entry 2000-01-12 is the first valid match:
match.head() price 2000-01-02 2.449039 2000-01-12 2.379882 2000-01-14 2.759891 2000-01-16 2.350821 2000-01-21 2.607467

Is there a way to do this using apply or similar to target entries in dfWeeks vectorized way?
Desired conclusion:
price target target_hit 2000-01-03 1.851533 2.35 2000-01-12 2000-01-10 1.625595 2.13 2000-01-12 2000-01-17 1.855813 2.36 2000-01-21 2000-01-24 2.130619 2.63 2000-01-25 2000-01-31 2.756487 3.26 nan