pythonpython-3.xscipyt-test

stats.ttest_ind: extract df value


I am using stats.ttest_ind:

a = list(range(0, 10))
b = list(range(20, 40, 2))
out = stats.ttest_ind(a, b)
print(out)

#Output
TtestResult(statistic=-11.443934479174386, pvalue=1.0790209865144616e-09, df=18.0)

I can extract statistic and pvalue using out[0] and out[1], but when I try to extract df with out[2], I get the following error:

IndexError: tuple index out of range.

out[-1] also does not work.

I have also tried the following code:

t_stat, pvalue, df = stats.ttest_ind(a,b)

but this resulted in the following error: ValueError: not enough values to unpack (expected 3, got 2)

It seems as if the df value is not in out as soon as I try to extract it. out has length 2 and I can only get the first 2 elements. Does anyone know what happens here or how I can solve this?


Solution

  • out is a scipy.stats._stats_py.TtestResult object.

    You can fetch degrees of freedom by:

    out.df
    #18.0
    

    If you do:

    list(out)
    

    you get:

    [-11.443934479174386, 1.0790209865144618e-09]
    

    So, out[2] and out[-1] will not give df.