4242from pandas .core .indexing import check_setitem_lengths
4343from pandas .core .internals .arrays import extract_array
4444import pandas .core .missing as missing
45+ from pandas .core .nanops import nanpercentile
4546
4647from pandas .io .formats .printing import pprint_thing
4748
@@ -1438,7 +1439,7 @@ def _unstack(self, unstacker_func, new_columns, n_rows, fill_value):
14381439 blocks = [make_block (new_values , placement = new_placement )]
14391440 return blocks , mask
14401441
1441- def quantile (self , qs , interpolation = 'linear' , axis = 0 , axes = None ):
1442+ def quantile (self , qs , interpolation = 'linear' , axis = 0 ):
14421443 """
14431444 compute the quantiles of the
14441445
@@ -1447,94 +1448,53 @@ def quantile(self, qs, interpolation='linear', axis=0, axes=None):
14471448 qs: a scalar or list of the quantiles to be computed
14481449 interpolation: type of interpolation, default 'linear'
14491450 axis: axis to compute, default 0
1450- axes : BlockManager.axes
14511451
14521452 Returns
14531453 -------
1454- tuple of (axis, block)
1455-
1454+ Block
14561455 """
1457- kw = {'interpolation' : interpolation }
14581456 values = self .get_values ()
14591457 values , _ = self ._try_coerce_args (values , values )
14601458
1461- def _nanpercentile1D (values , mask , q , ** kw ):
1462- # mask is Union[ExtensionArray, ndarray]
1463- values = values [~ mask ]
1464-
1465- if len (values ) == 0 :
1466- if lib .is_scalar (q ):
1467- return self ._na_value
1468- else :
1469- return np .array ([self ._na_value ] * len (q ),
1470- dtype = values .dtype )
1471-
1472- return np .percentile (values , q , ** kw )
1473-
1474- def _nanpercentile (values , q , axis , ** kw ):
1475-
1476- mask = isna (self .values )
1477- if not lib .is_scalar (mask ) and mask .any ():
1478- if self .ndim == 1 :
1479- return _nanpercentile1D (values , mask , q , ** kw )
1480- else :
1481- # for nonconsolidatable blocks mask is 1D, but values 2D
1482- if mask .ndim < values .ndim :
1483- mask = mask .reshape (values .shape )
1484- if axis == 0 :
1485- values = values .T
1486- mask = mask .T
1487- result = [_nanpercentile1D (val , m , q , ** kw ) for (val , m )
1488- in zip (list (values ), list (mask ))]
1489- result = np .array (result , dtype = values .dtype , copy = False ).T
1490- return result
1491- else :
1492- return np .percentile (values , q , axis = axis , ** kw )
1493-
1494- from pandas import Float64Index
14951459 is_empty = values .shape [axis ] == 0
1496- if is_list_like (qs ):
1497- ax = Float64Index (qs )
1460+ orig_scalar = not is_list_like (qs )
1461+ if orig_scalar :
1462+ # make list-like, unpack later
1463+ qs = [qs ]
14981464
1499- if is_empty :
1500- if self .ndim == 1 :
1501- result = self ._na_value
1502- else :
1503- # create the array of na_values
1504- # 2d len(values) * len(qs)
1505- result = np .repeat (np .array ([self ._na_value ] * len (qs )),
1506- len (values )).reshape (len (values ),
1507- len (qs ))
1465+ if is_empty :
1466+ if self .ndim == 1 :
1467+ result = self ._na_value
15081468 else :
1509- result = _nanpercentile (values , np .array (qs ) * 100 ,
1510- axis = axis , ** kw )
1511-
1512- result = np .array (result , copy = False )
1513- if self .ndim > 1 :
1514- result = result .T
1515-
1469+ # create the array of na_values
1470+ # 2d len(values) * len(qs)
1471+ result = np .repeat (np .array ([self ._na_value ] * len (qs )),
1472+ len (values )).reshape (len (values ),
1473+ len (qs ))
15161474 else :
1475+ mask = isna (self .values )
1476+ result = nanpercentile (values , np .array (qs ) * 100 ,
1477+ axis = axis , na_value = self ._na_value ,
1478+ mask = mask , ndim = self .ndim ,
1479+ interpolation = interpolation )
15171480
1518- if self .ndim == 1 :
1519- ax = Float64Index ([qs ])
1520- else :
1521- ax = axes [0 ]
1481+ result = np .array (result , copy = False )
1482+ if self .ndim > 1 :
1483+ result = result .T
15221484
1523- if is_empty :
1524- if self .ndim == 1 :
1525- result = self ._na_value
1526- else :
1527- result = np .array ([self ._na_value ] * len (self ))
1528- else :
1529- result = _nanpercentile (values , qs * 100 , axis = axis , ** kw )
1485+ if orig_scalar and not lib .is_scalar (result ):
1486+ # result could be scalar in case with is_empty and self.ndim == 1
1487+ assert result .shape [- 1 ] == 1 , result .shape
1488+ result = result [..., 0 ]
1489+ result = lib .item_from_zerodim (result )
15301490
15311491 ndim = getattr (result , 'ndim' , None ) or 0
15321492 result = self ._try_coerce_result (result )
15331493 if lib .is_scalar (result ):
1534- return ax , self .make_block_scalar (result )
1535- return ax , make_block (result ,
1536- placement = np .arange (len (result )),
1537- ndim = ndim )
1494+ return self .make_block_scalar (result )
1495+ return make_block (result ,
1496+ placement = np .arange (len (result )),
1497+ ndim = ndim )
15381498
15391499 def _replace_coerce (self , to_replace , value , inplace = True , regex = False ,
15401500 convert = False , mask = None ):
0 commit comments