11from __future__ import annotations
22
3- from typing import TYPE_CHECKING
4-
53import numpy as np
64
75from pandas ._typing import (
86 ArrayLike ,
97 npt ,
108)
119
12- from pandas .core .dtypes .common import is_sparse
1310from pandas .core .dtypes .missing import (
1411 isna ,
1512 na_value_for_dtype ,
1613)
1714
1815from pandas .core .nanops import nanpercentile
1916
20- if TYPE_CHECKING :
21- from pandas .core .arrays import ExtensionArray
22-
2317
2418def quantile_compat (
2519 values : ArrayLike , qs : npt .NDArray [np .float64 ], interpolation : str
@@ -40,23 +34,12 @@ def quantile_compat(
4034 if isinstance (values , np .ndarray ):
4135 fill_value = na_value_for_dtype (values .dtype , compat = False )
4236 mask = isna (values )
43- return _quantile_with_mask (values , mask , fill_value , qs , interpolation )
37+ return quantile_with_mask (values , mask , fill_value , qs , interpolation )
4438 else :
45- # In general we don't want to import from arrays here;
46- # this is temporary pending discussion in GH#41428
47- from pandas .core .arrays import BaseMaskedArray
48-
49- if isinstance (values , BaseMaskedArray ):
50- # e.g. IntegerArray, does not implement _from_factorized
51- out = _quantile_ea_fallback (values , qs , interpolation )
52-
53- else :
54- out = _quantile_ea_compat (values , qs , interpolation )
39+ return values ._quantile (qs , interpolation )
5540
56- return out
5741
58-
59- def _quantile_with_mask (
42+ def quantile_with_mask (
6043 values : np .ndarray ,
6144 mask : np .ndarray ,
6245 fill_value ,
@@ -114,82 +97,3 @@ def _quantile_with_mask(
11497 result = result .T
11598
11699 return result
117-
118-
119- def _quantile_ea_compat (
120- values : ExtensionArray , qs : npt .NDArray [np .float64 ], interpolation : str
121- ) -> ExtensionArray :
122- """
123- ExtensionArray compatibility layer for _quantile_with_mask.
124-
125- We pretend that an ExtensionArray with shape (N,) is actually (1, N,)
126- for compatibility with non-EA code.
127-
128- Parameters
129- ----------
130- values : ExtensionArray
131- qs : np.ndarray[float64]
132- interpolation: str
133-
134- Returns
135- -------
136- ExtensionArray
137- """
138- # TODO(EA2D): make-believe not needed with 2D EAs
139- orig = values
140-
141- # asarray needed for Sparse, see GH#24600
142- mask = np .asarray (values .isna ())
143- mask = np .atleast_2d (mask )
144-
145- arr , fill_value = values ._values_for_factorize ()
146- arr = np .atleast_2d (arr )
147-
148- result = _quantile_with_mask (arr , mask , fill_value , qs , interpolation )
149-
150- if not is_sparse (orig .dtype ):
151- # shape[0] should be 1 as long as EAs are 1D
152-
153- if orig .ndim == 2 :
154- # i.e. DatetimeArray
155- result = type (orig )._from_factorized (result , orig )
156-
157- else :
158- assert result .shape == (1 , len (qs )), result .shape
159- result = type (orig )._from_factorized (result [0 ], orig )
160-
161- # error: Incompatible return value type (got "ndarray", expected "ExtensionArray")
162- return result # type: ignore[return-value]
163-
164-
165- def _quantile_ea_fallback (
166- values : ExtensionArray , qs : npt .NDArray [np .float64 ], interpolation : str
167- ) -> ExtensionArray :
168- """
169- quantile compatibility for ExtensionArray subclasses that do not
170- implement `_from_factorized`, e.g. IntegerArray.
171-
172- Notes
173- -----
174- We assume that all impacted cases are 1D-only.
175- """
176- mask = np .atleast_2d (np .asarray (values .isna ()))
177- npvalues = np .atleast_2d (np .asarray (values ))
178-
179- res = _quantile_with_mask (
180- npvalues ,
181- mask = mask ,
182- fill_value = values .dtype .na_value ,
183- qs = qs ,
184- interpolation = interpolation ,
185- )
186- assert res .ndim == 2
187- assert res .shape [0 ] == 1
188- res = res [0 ]
189- try :
190- out = type (values )._from_sequence (res , dtype = values .dtype )
191- except TypeError :
192- # GH#42626: not able to safely cast Int64
193- # for floating point output
194- out = np .atleast_2d (np .asarray (res , dtype = np .float64 ))
195- return out
0 commit comments