Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to get datetime from pandas history dataframe?

Hi,

I'm calculating a SMA over a daily period but I now need to find the corresponding timeseries object corresponding with the SMA I'm calculating.

For instance, if I'm calculating an SMA over a 5 day period. How do I get the dates of the previous 5 days over which the SMA is calculated. To my knowledge, get_datetime() just gives the date at which the algo is running.

Any help, is appreciated.
Andrew

4 responses

I've worked out where the data needs to come from.

I'm usng the following code:

        daily_prices = history(bar_count=252, frequency='1d', field='price')  

which creates a pandas dataframe. What I now need to do is access the datetime field of the dataframe but I don't know how to.

Any help?

Just worked it out.

Need this code:

daily_prices[sid].keys()  

Sorry to bother you.

If you only want the datetimes, you can use:

daily_prices = history(bar_count=252, frequency='1d', field='price')  
datetimes = daily_prices.index  

In this example, 'datetimes' will be a 'DatetimeIndex' object from pandas.

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.

Ah! Thanks Joe. I'll try that!!