from statistics import mean, stdev
import pandas as pd
from scipy.stats import t 

def ts_student_t_os(data, mu="none"):
    '''
    One-sample Student t-test
    
    Parameters
    ----------
    data : pandas series with the numeric scores
    mu : optional parameter to set the hypothesized mean. If not used the midrange is used
        
    Returns
    -------
    testResults : Pandas dataframe with the hypothesized mean, sample mean, test statistic, degrees of freedom, p-value (sig.) and name of test used.
    
    Author
    ------
    Made by P. Stikker
    
    Please visit: https://PeterStatistics.com
    
    YouTube channel: https://www.youtube.com/stikpet
    
    '''
    if (mu=="none"):
        mu = (min(data) + max(data)) / 2
    
    #the sample size
    n = len(data)
    
    #the standard error
    SE = stdev(data) / n**0.5
    
    #the sample mean
    sMean = mean(data)
    
    #the t-value
    tVal = (sMean - mu)/SE
    
    #degrees of freedom
    df = n - 1
    
    #significance (p-value)
    pVal = (1 - t.cdf(abs(tVal), df))*2
    
    #prepare results
    testUsed = "one-sample Student t"
    testResults = pd.DataFrame([[mu, sMean, tVal, df, pVal, testUsed]], columns=["mu", "sample mean", "statistic", "df", "p-value", "test used"])        
    pd.set_option('display.max_colwidth', None)
    
    return testResults
