|
50 | 50 | # _QuantileMethods is a dictionary listing all the supported methods to
|
51 | 51 | # compute quantile/percentile.
|
52 | 52 | #
|
53 |
| -# Below virtual_index refer to the index of the element where the percentile |
| 53 | +# Below virtual_index refers to the index of the element where the percentile |
54 | 54 | # would be found in the sorted sample.
|
55 | 55 | # When the sample contains exactly the percentile wanted, the virtual_index is
|
56 | 56 | # an integer to the index of this element.
|
|
68 | 68 | # Discrete methods
|
69 | 69 | inverted_cdf=dict(
|
70 | 70 | get_virtual_index=lambda n, quantiles: _inverted_cdf(n, quantiles),
|
71 |
| - fix_gamma=lambda gamma, _: gamma, # should never be called |
| 71 | + fix_gamma=None, # should never be called |
72 | 72 | ),
|
73 | 73 | averaged_inverted_cdf=dict(
|
74 | 74 | get_virtual_index=lambda n, quantiles: (n * quantiles) - 1,
|
|
81 | 81 | closest_observation=dict(
|
82 | 82 | get_virtual_index=lambda n, quantiles: _closest_observation(n,
|
83 | 83 | quantiles),
|
84 |
| - fix_gamma=lambda gamma, _: gamma, # should never be called |
| 84 | + fix_gamma=None, # should never be called |
85 | 85 | ),
|
86 | 86 | # Continuous methods
|
87 | 87 | interpolated_inverted_cdf=dict(
|
|
121 | 121 | lower=dict(
|
122 | 122 | get_virtual_index=lambda n, quantiles: np.floor(
|
123 | 123 | (n - 1) * quantiles).astype(np.intp),
|
124 |
| - fix_gamma=lambda gamma, _: gamma, |
125 |
| - # should never be called, index dtype is int |
| 124 | + fix_gamma=None, # should never be called, index dtype is int |
126 | 125 | ),
|
127 | 126 | higher=dict(
|
128 | 127 | get_virtual_index=lambda n, quantiles: np.ceil(
|
129 | 128 | (n - 1) * quantiles).astype(np.intp),
|
130 |
| - fix_gamma=lambda gamma, _: gamma, |
131 |
| - # should never be called, index dtype is int |
| 129 | + fix_gamma=None, # should never be called, index dtype is int |
132 | 130 | ),
|
133 | 131 | midpoint=dict(
|
134 | 132 | get_virtual_index=lambda n, quantiles: 0.5 * (
|
|
143 | 141 | nearest=dict(
|
144 | 142 | get_virtual_index=lambda n, quantiles: np.around(
|
145 | 143 | (n - 1) * quantiles).astype(np.intp),
|
146 |
| - fix_gamma=lambda gamma, _: gamma, |
| 144 | + fix_gamma=None, |
147 | 145 | # should never be called, index dtype is int
|
148 | 146 | ))
|
149 | 147 |
|
@@ -4866,15 +4864,23 @@ def _quantile(
|
4866 | 4864 | # Virtual because it is a floating point value, not an valid index.
|
4867 | 4865 | # The nearest neighbours are used for interpolation
|
4868 | 4866 | try:
|
4869 |
| - method = _QuantileMethods[method] |
| 4867 | + method_props = _QuantileMethods[method] |
4870 | 4868 | except KeyError:
|
4871 | 4869 | raise ValueError(
|
4872 | 4870 | f"{method!r} is not a valid method. Use one of: "
|
4873 | 4871 | f"{_QuantileMethods.keys()}") from None
|
4874 |
| - virtual_indexes = method["get_virtual_index"](values_count, quantiles) |
| 4872 | + virtual_indexes = method_props["get_virtual_index"](values_count, |
| 4873 | + quantiles) |
4875 | 4874 | virtual_indexes = np.asanyarray(virtual_indexes)
|
4876 | 4875 |
|
4877 |
| - if np.issubdtype(virtual_indexes.dtype, np.integer): |
| 4876 | + if method_props["fix_gamma"] is None: |
| 4877 | + supports_integers = True |
| 4878 | + else: |
| 4879 | + int_virtual_indices = np.issubdtype(virtual_indexes.dtype, |
| 4880 | + np.integer) |
| 4881 | + supports_integers = method == 'linear' and int_virtual_indices |
| 4882 | + |
| 4883 | + if supports_integers: |
4878 | 4884 | # No interp
E864
olation needed, take the points along axis
|
4879 | 4885 | if supports_nans:
|
4880 | 4886 | # may contain nan, which would sort to the end
|
@@ -4906,7 +4912,7 @@ def _quantile(
|
4906 | 4912 | previous = arr[previous_indexes]
|
4907 | 4913 | next = arr[next_indexes]
|
4908 | 4914 | # --- Linear interpolation
|
4909 |
| - gamma = _get_gamma(virtual_indexes, previous_indexes, method) |
| 4915 | + gamma = _get_gamma(virtual_indexes, previous_indexes, method_props) |
4910 | 4916 | result_shape = virtual_indexes.shape + (1,) * (arr.ndim - 1)
|
4911 | 4917 | gamma = gamma.reshape(result_shape)
|
4912 | 4918 | result = _lerp(previous,
|
|
0 commit comments