4
4
5
5
import numpy as np
6
6
7
- from pandas ._libs import lib
8
7
from pandas ._typing import ArrayLike
9
8
10
- from pandas .core .dtypes .common import (
11
- is_list_like ,
12
- is_sparse ,
13
- )
9
+ from pandas .core .dtypes .common import is_sparse
14
10
from pandas .core .dtypes .missing import (
15
11
isna ,
16
12
na_value_for_dtype ,
22
18
from pandas .core .arrays import ExtensionArray
23
19
24
20
25
- def quantile_compat (values : ArrayLike , qs , interpolation : str , axis : int ) -> ArrayLike :
21
+ def quantile_compat (values : ArrayLike , qs : np . ndarray , interpolation : str ) -> ArrayLike :
26
22
"""
27
23
Compute the quantiles of the given values for each quantile in `qs`.
28
24
29
25
Parameters
30
26
----------
31
27
values : np.ndarray or ExtensionArray
32
- qs : a scalar or list of the quantiles to be computed
28
+ qs : np.ndarray[float64]
33
29
interpolation : str
34
- axis : int
35
30
36
31
Returns
37
32
-------
@@ -40,18 +35,17 @@ def quantile_compat(values: ArrayLike, qs, interpolation: str, axis: int) -> Arr
40
35
if isinstance (values , np .ndarray ):
41
36
fill_value = na_value_for_dtype (values .dtype , compat = False )
42
37
mask = isna (values )
43
- return quantile_with_mask (values , mask , fill_value , qs , interpolation , axis )
38
+ return _quantile_with_mask (values , mask , fill_value , qs , interpolation )
44
39
else :
45
- return quantile_ea_compat (values , qs , interpolation , axis )
40
+ return _quantile_ea_compat (values , qs , interpolation )
46
41
47
42
48
- def quantile_with_mask (
43
+ def _quantile_with_mask (
49
44
values : np .ndarray ,
50
45
mask : np .ndarray ,
51
46
fill_value ,
52
- qs ,
47
+ qs : np . ndarray ,
53
48
interpolation : str ,
54
- axis : int ,
55
49
) -> np .ndarray :
56
50
"""
57
51
Compute the quantiles of the given values for each quantile in `qs`.
@@ -66,11 +60,9 @@ def quantile_with_mask(
66
60
fill_value : Scalar
67
61
The value to interpret fill NA entries with
68
62
For ExtensionArray, this is _values_for_factorize()[1]
69
- qs : a scalar or list of the quantiles to be computed
63
+ qs : np.ndarray[float64]
70
64
interpolation : str
71
65
Type of interpolation
72
- axis : int
73
- Axis along which to compute quantiles.
74
66
75
67
Returns
76
68
-------
@@ -80,12 +72,12 @@ def quantile_with_mask(
80
72
-----
81
73
Assumes values is already 2D. For ExtensionArray this means np.atleast_2d
82
74
has been called on _values_for_factorize()[0]
75
+
76
+ Quantile is computed along axis=1.
83
77
"""
84
- is_empty = values .shape [axis ] == 0
85
- orig_scalar = not is_list_like (qs )
86
- if orig_scalar :
87
- # make list-like, unpack later
88
- qs = [qs ]
78
+ assert values .ndim == 2
79
+
80
+ is_empty = values .shape [1 ] == 0
89
81
90
82
if is_empty :
91
83
# create the array of na_values
@@ -97,39 +89,31 @@ def quantile_with_mask(
97
89
result = nanpercentile (
98
90
values ,
99
91
np .array (qs ) * 100 ,
100
- axis = axis ,
101
92
na_value = fill_value ,
102
93
mask = mask ,
103
- ndim = values .ndim ,
104
94
interpolation = interpolation ,
105
95
)
106
96
107
97
result = np .array (result , copy = False )
108
98
result = result .T
109
99
110
- if orig_scalar :
111
- assert result .shape [- 1 ] == 1 , result .shape
112
- result = result [..., 0 ]
113
- result = lib .item_from_zerodim (result )
114
-
115
100
return result
116
101
117
102
118
- def quantile_ea_compat (
119
- values : ExtensionArray , qs , interpolation : str , axis : int
103
+ def _quantile_ea_compat (
104
+ values : ExtensionArray , qs : np . ndarray , interpolation : str
120
105
) -> ExtensionArray :
121
106
"""
122
- ExtensionArray compatibility layer for quantile_with_mask .
107
+ ExtensionArray compatibility layer for _quantile_with_mask .
123
108
124
109
We pretend that an ExtensionArray with shape (N,) is actually (1, N,)
125
110
for compatibility with non-EA code.
126
111
127
112
Parameters
128
113
----------
129
114
values : ExtensionArray
130
- qs : a scalar or list of the quantiles to be computed
115
+ qs : np.ndarray[float64]
131
116
interpolation: str
132
- axis : int
133
117
134
118
Returns
135
119
-------
@@ -145,19 +129,12 @@ def quantile_ea_compat(
145
129
arr , fill_value = values ._values_for_factorize ()
146
130
arr = np .atleast_2d (arr )
147
131
148
- result = quantile_with_mask (arr , mask , fill_value , qs , interpolation , axis )
132
+ result = _quantile_with_mask (arr , mask , fill_value , qs , interpolation )
149
133
150
134
if not is_sparse (orig .dtype ):
151
135
# shape[0] should be 1 as long as EAs are 1D
152
-
153
- if result .ndim == 1 :
154
- # i.e. qs was originally a scalar
155
- assert result .shape == (1 ,), result .shape
156
- result = type (orig )._from_factorized (result , orig )
157
-
158
- else :
159
- assert result .shape == (1 , len (qs )), result .shape
160
- result = type (orig )._from_factorized (result [0 ], orig )
136
+ assert result .shape == (1 , len (qs )), result .shape
137
+ result = type (orig )._from_factorized (result [0 ], orig )
161
138
162
139
# error: Incompatible return value type (got "ndarray", expected "ExtensionArray")
163
140
return result # type: ignore[return-value]
0 commit comments