1

I am completely new to programming languages and I have picked up Python for backtesting a trading strategy (because I heard it is relatively easy). I have made some progress in learning the basics, however I am currently stuck at performing an ADFuller tests on a timeseries dataframe.

This is how my Dataframe looks

Now I need to run ADF test on the columns - "A-Btd", "A- Ctd" and so on (I have 66 columns like these).I would like to get the test statistic/output for each of them.

I tired using lines such as cadfs = [ts.adfuller(df1)]. Since, I lack the expertise I am not able to adjust the code as per my dataframe.

I apologize in advance if I have missed out some important information I have to give out. Please leave a comment and I will provide it asap.

Thanks a lot in advance!

1 Answer 1

4

If you have to do it for so many, I would try putting the results in a dict, like this:

import statsmodels.tsa.stattools as tsa

df = ... #load your dataframe
adf_results = {}

for col in df.columns.values:  #or edit this for a subset of columns first
    adf_results[col] = tsa.adfuller(df[col])

Obviously specifying other settings as desired, e.g. tsa.adfuller(df[col], autolag='BIC'). Or if you don't want all the output and would rather just parse each column to find out if it's stationary or not, the test statistic is the first entry in the tuple returned by adfuller(), so you could just use tsa.adfuller(df[col])[0] and test it against your threshold to get a boolean result, then make that the value in your dict.

2
  • Thanks, worked like a charm! However I require my output in this format - Link. For reference - this is my current code - Link and my current ouput looks like this on the console -Link. Would be great if you could let me know how to append these results to the datafame as shown in the first link.
    – Sam18J
    Jun 30, 2016 at 18:03
  • 1
    If you just add the [0] index after the test, it'll just show the test statistic. I mentioned it in the post, but didn't do it that way in the main code block.
    – Jeff
    Jun 30, 2016 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.