import pandas as pd
from scipy.stats import binom

def ts_sign_os(data, hypMed = "none"):
    '''
    one-sample sign test
     
    This function will perform one-sample sign test
    
    Parameters
    ----------
    data : list or Pandas data series with the data as numbers
    hypMed : optional hypothesized median, otherwise the midrange will be used
        
    Returns
    -------
    testResults : Pandas dataframe with the significance (p-value) and test used
   
    Notes
    -----
    this uses the binom function from scipy.stats for the binomial distribution cdf.
    
    Author
    ------
    Made by P. Stikker
    
    Please visit: https://PeterStatistics.com
    
    YouTube channel: https://www.youtube.com/stikpet
    
    '''
    #set hypothesized median to mid range if not provided
    if (hypMed=="none"):
        hypMed = (min(data) + max(data)) / 2
        
    #Determine count of cases below hypothesized median
    group1 = data[data<hypMed]
    group2 = data[data>hypMed]
    n1 = len(group1)
    n2 = len(group2)
    
    #Select the lowest of the two
    myMin = min(n1,n2)
    
    #Determine total number of cases (unequal to hyp. median)
    n = n1+n2
    
    #Determine the significance using binomial test
    pVal = 2*binom.cdf(myMin, n,0.5)
    testUsed = "one-sample sign test"
    testResults = pd.DataFrame([[pVal, testUsed]], columns=["p-value", "test"])
    pd.set_option('display.max_colwidth', None)
    
    return(testResults)

#Example
dataList = [1, 2, 5, 1, 1, 5, 3, 1, 5, 1, 1, 5, 1, 1, 3, 3, 3, 4, 2, 4]
data = pd.Series(dataList)

ts_sign_os(data)
ts_sign_os(data, hypMed=2)