Repository: scipy/scipy
File: scipy/stats/_stats_py.py (Python)

Code before:
        alternative = self._alternative
        p = self._p
        x = np.sort(self._x, axis=-1)
        n = x.shape[-1]
        bd = stats.binom(n, p)
        shape = p.shape

        if confidence_level <= 0 or confidence_level >= 1:
            message = "`confidence_level` must be a number between 0 and 1."
            raise ValueError(message)

        if n == 0:
            zeros = np.zeros(p.shape, dtype=x.dtype)
            low, high = zeros, zeros
        elif alternative == 'less':
            p = 1 - confidence_level
            low = np.full(shape, -np.inf)
            high_index = bd.isf(p).astype(int)
            valid_index = high_index < n
            high_index[~valid_index] = 0
            high = np.where(valid_index, np.take_along_axis(x, high_index), np.nan)
        elif alternative == 'greater':
            p = 1 - confidence_level
            low_index = (bd.ppf(p)).astype(int) - 1
            valid_index = low_index >= 0
            low_index[~valid_index] = 0
            low = np.where(valid_index, np.take_along_axis(x, low_index), np.nan)
            high = np.full(shape, np.inf)
        elif alternative == 'two-sided':
            p = (1 - confidence_level) / 2
            low_index = (bd.ppf(p)).astype(int) - 1
            valid_index = low_index >= 0
            low_index[~valid_index] = 0
            low = np.where(valid_index, np.take_along_axis(x, low_index), np.nan)
            high_index = bd.isf(p).astype(int)
            valid_index = high_index < n
            high_index[~valid_index] = 0
            high = np.where(valid_index, np.take_along_axis(x, high_index), np.nan)

        args = self._axis, self._axis_none, self._keepdims, self._ndim, self._nan_out
        low = _quantile_test_postprocess(low, *args)
        high = _quantile_test_postprocess(high, *args)
        return ConfidenceInterval(low, high)


def quantile_test_iv(x, q, p, alternative, axis, keepdims):

    x = np.atleast_1d(x)
    message = '`x` must be an array of numbers.'
    if not np.issubdtype(x.dtype, np.number):
        raise ValueError(message)

Review comment (suggestion):
`n` is the length of the data along `axis`. When it's zero, these low and high endpoints (like all return values) will get nan-ed out, so set them to anything of the correct shape and dtype for now.

Code after:
        >>> q = 0  # hypothesized value of the quantile
        >>> x = np.exp(np.arange(0, 1.01, 0.01))
        >>> res = stats.quantile_test(x, q=q, p=p, alternative='less')
        >>> lb, ub = res.confidence_interval()
        >>> lb, ub
        (-inf, 2.293318740264183)
        >>> res = stats.quantile_test(x, q=q, p=p, alternative='two-sided')
        >>> lb, ub = res.confidence_interval(0.9)
        >>> lb, ub
        (1.9542373206359396, 2.293318740264183)
        """

        alternative = self._alternative
        p = self._p
        x = np.sort(self._x, axis=-1)
        n = x.shape[-1]
        bd = stats.binom(n, p)
        shape = p.shape

        if confidence_level <= 0 or confidence_level >= 1:
            message = "`confidence_level` must be a number between 0 and 1."
            raise ValueError(message)

        if n == 0:
            zeros = np.zeros(p.shape, dtype=x.dtype)
            low, high = zeros, zeros
        elif alternative == 'less':
            p = 1 - confidence_level
            low = np.full(shape, -np.inf)
            high_index = bd.isf(p).astype(int)
            valid_index = high_index < n
            high_index[~valid_index] = 0
            x_high = np.take_along_axis(x, high_index, axis=-1)
            high = np.where(valid_index, x_high, np.nan)
        elif alternative == 'greater':
            p = 1 - confidence_level
            low_index = (bd.ppf(p)).astype(int) - 1
            valid_index = low_index >= 0
            low_index[~valid_index] = 0
            x_low = np.take_along_axis(x, low_index, axis=-1)
            low = np.where(valid_index, x_low, np.nan)
            high = np.full(shape, np.inf)
        elif alternative == 'two-sided':
            p = (1 - confidence_level) / 2
            low_index = (bd.ppf(p)).astype(int) - 1
            valid_index = low_index >= 0
            low_index[~valid_index] = 0
            x_low = np.take_along_axis(x, low_index, axis=-1)
            low = np.where(valid_index, x_low, np.nan)
            high_index = bd.isf(p).astype(int)
            valid_index = high_index < n