Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Date from data history

How do I grab the date specifically the position of the min to check that the min is not within the last 10 days.

df=pd.DataFrame(data.history(stock, "price", 20, frequency="1d"))  
df.index(np.nanmin(df))

TypeError: 'DatetimeIndex' object is not callable  
3 responses

I found how to grab month ,year ,day
im still not allowed to call timedateindex for position

df=pd.DataFrame(data.history(stock, "price", 20, frequency="1d"))  
               mindf=df.idxmin(axis=0)  
               month_min=mindf[0].month  
               day_min=mindf[0],day  

If one only wants to determine if a stocks min price occurred more than 10 days ago, then compare the datetime returned by the 'idxmin' method to the date 10 days before the end date. One probably wants 'trading days' and not 'calendar days' so use the pandas 'BDay' method. Something like this (the data.history method returns data in a similar format as the get_pricing method in notebooks).

import pandas as pd  
from pandas.tseries.offsets import BDay

start_date = pd.to_datetime("2018-08-01")  
end_date = pd.to_datetime("2018-10-15")

price_series = get_pricing(['AAPL', 'IBM'], start_date=start_date, end_date=end_date, fields='price')  
min_price_date = price_series.idxmin()  
min_before_last_10_days = price_min_date < end_date - BDay(10)

However, there are a couple of issues with this. First, the idxmin method returns the first occurance of the min. There is a small chance that the same min could have occurred later and potentially within the past 10 days. Additionally, the pandas BDay method may not always align perfectly with trading days.

Neither of these issues may be too much of a concern but there is another way to solve this which avoid both of these. Simply compare the min over the past 10 days to the min over the whole timeframe.

min_value_overall = price_series.min()  
min_value_last_10_days = price_series.tail(10).min()  
min_not_in_last_10_days = min_value_overall < min_values_last_10_days

This avoids the idxmin issue of the first occurrence and also assures us we get exactly 10 trading days (by using the tail(10) method). This also seems a little less complicated by not dealing with dates and offsets. See attached notebook.

thanks for the assist
I appreciate it .