I've come up with a convoluted, but effective, way to accomplish the desired effect.
The key is to define the following function establish_percentile_method
. This function has two mandatory parameters, and it returns a value. The mandatory parameters are:
method
- a string, one of the possible legitimate arguments that can be passed to numpy.percentile
's method
parameter. For further details, see the numpy.percentile
documentation page.
og_percentile
- a function object, which should act exactly like the percentile
function as documented in the numpy
documentation.
establish_percentile_method
changes the numpy
library's percentile
variable to point to a function that acts almost exactly as documented for percentile
in the numpy
documentation, except that its method of computation is method
.
The return value is a function that acts like percentile
as described in the numpy
documentation.
def establish_percentile_method(method, og_percentile):
def new_percentile(*args, **kwargs):
kwargs['method'] = method
return og_percentile(*args, **kwargs)
np.percentile = new_percentile
return og_percentile
Now you can use the function just defined to change the default method that numpy.percentile
uses to calculate percentiles, and as a consequence affect every function that invokes numpy.percentile
, such as pandas.describe()
or seaborn.boxplot(...)
.
import numpy as np
def establish_percentile_method(method, og_percentile):
def new_percentile(*args, **kwargs):
if 'method' not in kwargs:
kwargs['method'] = method
return og_percentile(*args, **kwargs)
np.percentile = new_percentile
return og_percentile
import pandas as pd
import seaborn as sns
df = pd.DataFrame(data = [[1], [2], [3], [4]], columns = ['C'])
print(df) # Or, better, simply df, if you work in an interactive environment such as Google Colab.
df.describe()
sns.boxplot(data = df, y = 'C')
og_percentile = establish_percentile_method('inverted_cdf', np.percentile)
df.describe()
sns.boxplot(data = df, y = 'C')
establish_percentile_method('averaged_inverted_cdf', og_percentile)
df.describe()
sns.boxplot(data = df, y = 'C')
np.percentile = og_percentile
df.describe()
sns.boxplot(data = df, y = 'C')