Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Slicing result between two dates

I am not very experienced in python. I have a problem subsetting a multindex dataframe by giving two string dates. Your help is very much appreciated.

result = run_pipeline(pipeline, start_date="2006-01-01", end_date="2008-08-01")  

and I want to get a slice between two dates: '2006-01-01' and '2007-01-01'

Here is what I do and what I get:

dti = result.index.get_level_values(0)  
query =  ( dti> '2006-01-01') & (dti< '2007-01-01')  
res = result.loc[query]  
print(res.index.levels[0])  

No slicing is done

DatetimeIndex(['2006-01-03', '2006-01-04', '2006-01-05', '2006-01-06',
'2006-01-09', '2006-01-10', '2006-01-11', '2006-01-12',
'2006-01-13', '2006-01-17',
...
'2008-07-21', '2008-07-22', '2008-07-23', '2008-07-24',
'2008-07-25', '2008-07-28', '2008-07-29', '2008-07-30',
'2008-07-31', '2008-08-01'],
dtype='datetime64[ns, UTC]', name='date', length=650, freq='C')

2 responses

Your approach is a good one and DOES work. You simply printed the wrong thing. Don't print the index, instead print the resulting sliced dataframe. Try this

# Run the pipeline over desired days  
start_date = '2006-01-01'  
end_date = '2008-08-1'  
result = run_pipeline(make_pipeline(), start_date, end_date)

# Get the dates from the index and create a series to use as a 'mask'  
dti = result.index.get_level_values(0)  
query =  ( dti > '2006-01-01') & (dti < '2007-01-01')  
result_slice_2006 = result.loc[query]  
print(result_slice_2006)

The output will be a subset of 'result' between the dates 2006-01-03 and 2006-12-29 .

In general, don't work directly with a dataframe index, even printing it. It can be misleading (as you found out). Use the method get_level_values if you need to check the values. So, this will display just the sliced index values

display(result_slice_2006.index.get_level_values(level=0))

There is a little more detail on the problems with using index values directly in this post

Good luck.

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.

Thank, Dan!

Your are absolutely right about this. I, too, figured it out that the mask worked except that the index gave me wrong impression first, as you pointed out. I appreciat your support!