Use scipy.stats.percentileofscore:

# libs required
from scipy import stats
import pandas as pd
import numpy as np

# generate ramdom data with same seed (to be reproducible)
np.random.seed(seed=1)
df = pd.DataFrame(np.random.uniform(0, 1, (10)), columns=['a'])

# quantile function
x = df.quantile(0.5)[0]

# inverse of quantile
stats.percentileofscore(df['a'], x)
Answer from fernandosjp on Stack Overflow
🌐
NumPy
numpy.org › devdocs › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.5.dev0 Manual
Given a sample a from an underlying distribution, quantile provides a nonparametric estimate of the inverse cumulative distribution function.
Discussions

Inverse function of `quantile()`
The function `quantile()` returns the value in a vector that is on the designated percentile of the underlying estimate distribution of the values in… More on reddit.com
🌐 r/rstats
5
7
May 6, 2024
python - Numpy implementation of the Quantile function - Cross Validated
I am trying to wrap my head around the implementation of the quantile function for finite samples and, specifically, in numpy (main reason to do this: I am working on conformal predictions). It is ... More on stats.stackexchange.com
🌐 stats.stackexchange.com
May 20, 2024
python - Numpy function to get the quantile that corresponds to a given value - Stack Overflow
Note that it's not an exact inverse because the quantile/percentile functions are not exact. Given a finite array of observations, the percentiles will have discrete values; in other words, you may be specifying a q that falls between those values and the functions find the closest one. from scipy import stats import numpy ... More on stackoverflow.com
🌐 stackoverflow.com
BUG: Quantile function on complex numbers doesn't error
It must be, for its inverse to exist. Consequentially, it makes no mathematical sense to apply quantiles to an unordered set, like a complex input array. Some order can be introduced (e.g., lexicographic) but it is artificial, without a sound mathematical support. However, both sorting and quantiles functions work in numpy ... More on github.com
🌐 github.com
4
November 22, 2022
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.1 Manual
Given a sample a from an underlying distribution, quantile provides a nonparametric estimate of the inverse cumulative distribution function.
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.stats.quantile.html
quantile — SciPy v1.17.0 Manual
numpy.quantile · Trimming and winsorization transition guide · Notes · Given a sample x from an underlying distribution, quantile provides a nonparametric estimate of the inverse cumulative distribution function.
🌐
NumPy
numpy.org › doc › 2.2 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.2 Manual
Given a sample a from an underlying distribution, quantile provides a nonparametric estimate of the inverse cumulative distribution function.
🌐
NumPy
numpy.org › doc › 2.3 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.3 Manual
Given a sample a from an underlying distribution, quantile provides a nonparametric estimate of the inverse cumulative distribution function.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.4 Manual
June 22, 2021 - Given a sample a from an underlying distribution, quantile provides a nonparametric estimate of the inverse cumulative distribution function.
🌐
Reddit
reddit.com › r/rstats › inverse function of `quantile()`
r/rstats on Reddit: Inverse function of `quantile()`
May 6, 2024 - The function `quantile()` returns the value in a vector that is on the designated percentile of the underlying estimate distribution of the values in…
Find elsewhere
Top answer
1 of 1
5

The value returned by the np.quantile function will depend on the interpolation behavior; there are a number of different ways to define "quantile", and numpy implements several of them. For more detail, see R. J. Hyndman and Y. Fan, “Sample quantiles in statistical packages,” The American Statistician, 50(4), pp. 361-365, 1996.

This is not much different from the situation as arises when estimating the median for a sample with an even sample size. See also: Definition of quantile

How np.quantile does interpolation is explained in the numpy documentation.

Given a vector V of length n, the q-th quantile of V is the value q of the way from the minimum to the maximum in a sorted copy of V. The values and distances of the two nearest neighbors as well as the method parameter will determine the quantile if the normalized ranking does not match the location of q exactly. This function is the same as the median if q=0.5, the same as the minimum if q=0.0 and the same as the maximum if q=1.0.

The optional method parameter specifies the method to use when the desired quantile lies between two indexes i and j = i + 1. In that case, we first determine i + g, a virtual index that lies between i and j, where i is the floor and g is the fractional part of the index. The final result is, then, an interpolation of a[i] and a[j] based on g. During the computation of g, i and j are modified using correction constants alpha and beta whose choices depend on the method used. Finally, note that since Python uses 0-based indexing, the code subtracts another 1 from the index internally.

The following formula determines the virtual index i + g, the location of the quantile in the sorted sample: $$ i + g = q \times (n - \alpha - \beta +1 ) + \alpha $$

This is straightforward to implement; this implementation matches numpy for method="linear", which is the default.

import numpy as np


def my_quantile(a, q):
    n = a.size
    alpha = 1.0
    beta = 1.0
    i = int(q * n)
    g = q * (n - alpha - beta + 1.0) + alpha - i
    a = np.sort(a)
    upper = a[i]
    lower = a[i - 1]
    return (upper - lower) * g + lower


if __name__ == "__main__":
    v = np.array([2, 3, 4, 10])
    print(v)
    q_val = np.quantile(v, 1.0 / 3.0, method="linear")
    me = my_quantile(a=v, q=1.0 / 3.0)
    print(f"numpy:\t{q_val}") # 3.0
    print(f"mine:\t{me}") # 3.0

    q_val = np.quantile(v, 0.25, method="linear")
    me = my_quantile(a=v, q=0.25)
    print(20 * "-")
    print(f"numpy:\t{q_val}") # 2.75
    print(f"mine:\t{me}") # 2.75

    prng = np.random.default_rng(42)
    norm_v = prng.normal(size=100)
    me = my_quantile(a=norm_v, q=0.25)
    print(20 * "-")
    print(f"numpy:\t{q_val}") # -0.5439015088644885
    print(f"mine:\t{me}") # -0.5439015088644885

Uses numpy version 1.24.3.

In comments, OP writes that they expect np.quantile(v, 0.25) to return 2. This is achieved by a different method than the default ”linear” method. If OP desires this behavior, then they should use np.quantile(v, 0.25, method='lower'), which returns 2.

🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.Series.quantile.html
pandas.Series.quantile — pandas 3.0.2 documentation
Calculate the rolling quantile. numpy.percentile · Returns the q-th percentile(s) of the array elements. Examples · >>> s = pd.Series([1, 2, 3, 4]) >>> s.quantile(0.5) 2.5 >>> s.quantile([0.25, 0.5, 0.75]) 0.25 1.75 0.50 2.50 0.75 3.25 dtype: float64 ·
🌐
GitHub
github.com › numpy › numpy › issues › 22652
BUG: Quantile function on complex numbers doesn't error · Issue #22652 · numpy/numpy
November 22, 2022 - Complex numbers are unordered. The complex set, ℂ, is isomorphic with ℝ², which is an unordered vector space. On the other hand, quantiles are the inverse of the cumulative distribution function, CDF.
Author   aschaffer
🌐
Codecademy
codecademy.com › docs › python:numpy › built-in functions › .quantile()
Python:NumPy | Built-in Functions | .quantile() | Codecademy
April 19, 2025 - Valid options include: 'inverted_cdf', 'averaged_inverted_cdf', 'closest_observation', 'interpolated_inverted_cdf', 'hazen', 'weibull', 'median_unbiased', and 'normal_unbiased'. keepdims (Optional): If True, the reduced axes are retained with size one, maintaining the number of dimensions in the output. weights (Optional): An array of weights corresponding to values in a, used to influence the quantile calculation.
🌐
TutorialsPoint
tutorialspoint.com › numpy › numpy_quantile_function.htm
NumPy quantile() Function
This function returns the q-th quantile(s) of the input array along the specified axis. The result is a scalar or array depending on the input and the value of the q parameter. Following is a basic example to compute the 50th percentile (median) ...
🌐
NumPy
numpy.org › doc › 1.25 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v1.25 Manual
If q is a single probability and axis=None, then the result is a scalar. If multiple probabilies levels are given, first axis of the result corresponds to the quantiles. The other axes are the axes that remain after the reduction of a. If the input contains integers or floats smaller than float64, ...
🌐
GitHub
github.com › numpy › numpy › issues › 10736
Restructure percentile methods · Issue #10736 · numpy/numpy
March 12, 2018 - As exemplified in the Wikipedia page: https://en.wikipedia.org/wiki/Percentile#The_nearest-rank_method
Author   ricardoV94
🌐
NumPy
numpy.org › doc › 2.1 › reference › generated › numpy.percentile.html
numpy.percentile — NumPy v2.1 Manual
The behavior of numpy.percentile with percentage q is that of numpy.quantile with argument q/100.
🌐
SciPy
docs.scipy.org › doc › scipy-1.16.1 › reference › generated › scipy.stats.quantile.html
quantile — SciPy v1.16.1 Manual
Given a sample x from an underlying distribution, quantile provides a nonparametric estimate of the inverse cumulative distribution function.
🌐
SciPy
docs.scipy.org › doc › scipy › reference › generated › scipy.special.stdtrit.html
scipy.special.stdtrit — SciPy v1.17.0 Manual
>>> x = np.linspace(0, 1, 1000) >>> parameters = [(1, "solid"), (2, "dashed"), (5, "dotted")] >>> fig, ax = plt.subplots() >>> for (df, linestyle) in parameters: ... ax.plot(x, stdtrit(df, x), ls=linestyle, label=f"$df={df}$") >>> ax.legend() >>> ax.set_ylim(-10, 10) >>> ax.set_title("Student t distribution quantile function") >>> plt.show() The function can be computed for several degrees of freedom at the same time by providing a NumPy array or list for df:
🌐
NumPy
numpy.org › doc › 2.0 › reference › generated › numpy.quantile.html
numpy.quantile — NumPy v2.0 Manual
If q is a single probability and axis=None, then the result is a scalar. If multiple probability levels are given, first axis of the result corresponds to the quantiles. The other axes are the axes that remain after the reduction of a. If the input contains integers or floats smaller than float64, ...