import pandas as pd
import numpy as np
import timeit

"""
Explanation: using Timeit
How to compare runs to determine which is a faster implementation.
End of explanation
"""


help('numpy.random.randint')

"""
Explanation: Create a panda data frame with 1000 values randomly 1 <= x < 10.  Uniform random?
End of explanation
"""


data = pd.DataFrame(data=np.random.randint(1,10,1000),columns=['value'])

data.describe()

np.median(a=data['value'])

"""
Explanation: Could also use np.random.normal for some statistical fun
End of explanation
"""


setup = '''
import pandas as pd
import numpy as np
data = pd.DataFrame(data=np.random.randint(1,10,1000),columns=['value'])'''

"""
Explanation: define setup string which will be run in test case, but before timing begins.
End of explanation
"""


median_statement ='''np.median(a=data['value'])'''

"""
Explanation: calculate the median of the column 'value' time how long it takes.
End of explanation
"""


help('timeit')

timeit.Timer(setup=setup, stmt=median_statement).timeit(number=100000)

"""
Explanation: run timeit with a timer containing the setup and the median statement.  The median statement will be called 100000 times.
End of explanation
"""


setup = '''
import numpy as np
data = np.random.randint(1,10,1000)
'''

median_statement = '''np.median(a=data)'''
mean_statement   = '''np.mean  (a=data)'''

"""
Explanation: Maybe a better test with less overhead.  
Interested in answering the question about the effort needed to do median so removing the panda dataframe and just working with a list. Just looking at difference in time to do median and mean.
End of explanation
"""


timeTestSetup = {'median': timeit.Timer(setup=setup,stmt=median_statement),
'mean': timeit.Timer(setup=setup, stmt=mean_statement)}

"""
Explanation: Build a little dictionary with the median and mean setup and statement.
End of explanation
"""


for (k,v) in timeTestSetup.items():
    print("Method:\t{}: Time:\t{}".format(k,v.timeit()))

"""
Explanation: Run them both and report the results.
End of explanation
"""


