MCPcopy
hub / github.com/pandas-dev/pandas / format_percentiles

Function format_percentiles

pandas/io/formats/format.py:1554–1617  ·  view source on GitHub ↗

Outputs rounded and formatted percentiles. Parameters ---------- percentiles : list-like, containing floats from interval [0,1] Returns ------- formatted : list of strings Notes ----- Rounding precision is chosen so that: (1) if any two elements of ``p

(
    percentiles: np.ndarray | Sequence[float],
)

Source from the content-addressed store, hash-verified

1552
1553
1554def format_percentiles(
1555 percentiles: np.ndarray | Sequence[float],
1556) -> list[str]:
1557 """
1558 Outputs rounded and formatted percentiles.
1559
1560 Parameters
1561 ----------
1562 percentiles : list-like, containing floats from interval [0,1]
1563
1564 Returns
1565 -------
1566 formatted : list of strings
1567
1568 Notes
1569 -----
1570 Rounding precision is chosen so that: (1) if any two elements of
1571 ``percentiles`` differ, they remain different after rounding
1572 (2) no entry is *rounded* to 0% or 100%.
1573 Any non-integer is always rounded to at least 1 decimal place.
1574
1575 Examples
1576 --------
1577 Keeps all entries different after rounding:
1578
1579 >>> format_percentiles([0.01999, 0.02001, 0.5, 0.666666, 0.9999])
1580 ['1.999%', '2.001%', '50%', '66.667%', '99.99%']
1581
1582 No element is rounded to 0% or 100% (unless already equal to it).
1583 Duplicates are allowed:
1584
1585 >>> format_percentiles([0, 0.5, 0.02001, 0.5, 0.666666, 0.9999])
1586 ['0%', '50%', '2.0%', '50%', '66.67%', '99.99%']
1587 """
1588 if len(percentiles) == 0:
1589 return []
1590
1591 percentiles = np.asarray(percentiles)
1592
1593 # It checks for np.nan as well
1594 if (
1595 not is_numeric_dtype(percentiles)
1596 or not np.all(percentiles >= 0)
1597 or not np.all(percentiles <= 1)
1598 ):
1599 raise ValueError("percentiles should all be in the interval [0,1]")
1600
1601 percentiles = 100 * percentiles
1602 prec = get_precision(percentiles)
1603 percentiles_round_type = percentiles.round(prec).astype(int)
1604
1605 int_idx = np.isclose(percentiles_round_type, percentiles)
1606
1607 if np.all(int_idx):
1608 out = percentiles_round_type.astype(str)
1609 return [i + "%" for i in out]
1610
1611 unique_pcts = np.unique(percentiles)

Callers 2

describe_numeric_1dFunction · 0.90
describe_timestamp_1dFunction · 0.90

Calls 6

is_numeric_dtypeFunction · 0.90
get_precisionFunction · 0.85
allMethod · 0.45
astypeMethod · 0.45
roundMethod · 0.45
uniqueMethod · 0.45

Tested by

no test coverage detected