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

Method _format_multi

pandas/core/indexes/multi.py:1593–1654  ·  view source on GitHub ↗
(
        self,
        *,
        include_names: bool,
        sparsify: bool | None | lib.NoDefault,
        formatter: Callable | None = None,
    )

Source from the content-addressed store, hash-verified

1591 return mi._values
1592
1593 def _format_multi(
1594 self,
1595 *,
1596 include_names: bool,
1597 sparsify: bool | None | lib.NoDefault,
1598 formatter: Callable | None = None,
1599 ) -> list:
1600 if len(self) == 0:
1601 return []
1602
1603 stringified_levels = []
1604 for lev, level_codes in zip(self.levels, self.codes, strict=True):
1605 na = _get_na_rep(lev.dtype)
1606
1607 if len(lev) > 0:
1608 taken = formatted = lev.take(level_codes)
1609 formatted = taken._format_flat(include_name=False, formatter=formatter)
1610
1611 # we have some NA
1612 mask = level_codes == -1
1613 if mask.any():
1614 formatted = np.array(formatted, dtype=object)
1615 formatted[mask] = na
1616 formatted = formatted.tolist()
1617
1618 else:
1619 # weird all NA case
1620 formatted = [
1621 pprint_thing(na if isna(x) else x, escape_chars=("\t", "\r", "\n"))
1622 for x in algos.take_nd(lev._values, level_codes)
1623 ]
1624 stringified_levels.append(formatted)
1625
1626 result_levels = []
1627 for lev, lev_name in zip(stringified_levels, self.names, strict=True):
1628 level = []
1629
1630 if include_names:
1631 level.append(
1632 pprint_thing(lev_name, escape_chars=("\t", "\r", "\n"))
1633 if lev_name is not None
1634 else ""
1635 )
1636
1637 level.extend(np.array(lev, dtype=object))
1638 result_levels.append(level)
1639
1640 if sparsify is None:
1641 sparsify = get_option("display.multi_sparse")
1642
1643 if sparsify:
1644 sentinel: Literal[""] | bool | lib.NoDefault = ""
1645 # GH3547 use value of sparsify as sentinel if it's "Falsey"
1646 assert isinstance(sparsify, bool) or sparsify is lib.no_default
1647 if sparsify is lib.no_default:
1648 sentinel = sparsify
1649 # little bit of a kludge job for #1217
1650 result_levels = sparsify_labels(

Callers 8

_format_header_miMethod · 0.80
to_stringMethod · 0.80
_get_formatted_indexMethod · 0.80
_write_col_headerMethod · 0.80
_get_level_lengthsFunction · 0.80

Calls 12

pprint_thingFunction · 0.90
isnaFunction · 0.90
get_optionFunction · 0.90
_get_na_repFunction · 0.85
sparsify_labelsFunction · 0.85
_format_flatMethod · 0.80
take_ndMethod · 0.80
takeMethod · 0.45
anyMethod · 0.45
arrayMethod · 0.45
tolistMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected