|
31 | 31 | from pandas.util._decorators import doc
|
32 | 32 |
|
33 | 33 | import pandas as pd
|
34 |
| -from pandas import RangeIndex |
| 34 | +from pandas import ( |
| 35 | + IndexSlice, |
| 36 | + RangeIndex, |
| 37 | +) |
35 | 38 | from pandas.api.types import is_list_like
|
36 | 39 | from pandas.core import generic
|
37 | 40 | import pandas.core.common as com
|
@@ -726,7 +729,7 @@ def to_latex(
|
726 | 729 | self.data.columns = RangeIndex(stop=len(self.data.columns))
|
727 | 730 | numeric_cols = self.data._get_numeric_data().columns.to_list()
|
728 | 731 | self.data.columns = _original_columns
|
729 |
| - column_format = "" if self.hidden_index else "l" * self.data.index.nlevels |
| 732 | + column_format = "" if self.hide_index_ else "l" * self.data.index.nlevels |
730 | 733 | for ci, _ in enumerate(self.data.columns):
|
731 | 734 | if ci not in self.hidden_columns:
|
732 | 735 | column_format += (
|
@@ -971,7 +974,7 @@ def _copy(self, deepcopy: bool = False) -> Styler:
|
971 | 974 | )
|
972 | 975 |
|
973 | 976 | styler.uuid = self.uuid
|
974 |
| - styler.hidden_index = self.hidden_index |
| 977 | + styler.hide_index_ = self.hide_index_ |
975 | 978 |
|
976 | 979 | if deepcopy:
|
977 | 980 | styler.ctx = copy.deepcopy(self.ctx)
|
@@ -1010,7 +1013,7 @@ def clear(self) -> None:
|
1010 | 1013 | self.cell_context.clear()
|
1011 | 1014 | self._todo.clear()
|
1012 | 1015 |
|
1013 |
| - self.hidden_index = False |
| 1016 | + self.hide_index_ = False |
1014 | 1017 | self.hidden_columns = []
|
1015 | 1018 | # self.format and self.table_styles may be dependent on user
|
1016 | 1019 | # input in self.__init__()
|
@@ -1141,7 +1144,7 @@ def _applymap(
|
1141 | 1144 | ) -> Styler:
|
1142 | 1145 | func = partial(func, **kwargs) # applymap doesn't take kwargs?
|
1143 | 1146 | if subset is None:
|
1144 |
| - subset = pd.IndexSlice[:] |
| 1147 | + subset = IndexSlice[:] |
1145 | 1148 | subset = non_reducing_slice(subset)
|
1146 | 1149 | result = self.data.loc[subset].applymap(func)
|
1147 | 1150 | self._update_ctx(result)
|
@@ -1556,37 +1559,169 @@ def set_na_rep(self, na_rep: str) -> StylerRenderer:
|
1556 | 1559 | self.na_rep = na_rep
|
1557 | 1560 | return self.format(na_rep=na_rep, precision=self.precision)
|
1558 | 1561 |
|
1559 |
| - def hide_index(self) -> Styler: |
| 1562 | + def hide_index(self, subset: Subset | None = None) -> Styler: |
1560 | 1563 | """
|
1561 |
| - Hide any indices from rendering. |
| 1564 | + Hide the entire index, or specific keys in the index from rendering. |
| 1565 | +
|
| 1566 | + This method has dual functionality: |
| 1567 | +
|
| 1568 | + - if ``subset`` is ``None`` then the entire index will be hidden whilst |
| 1569 | + displaying all data-rows. |
| 1570 | + - if a ``subset`` is given then those specific rows will be hidden whilst the |
| 1571 | + index itself remains visible. |
| 1572 | +
|
| 1573 | + .. versionchanged:: 1.3.0 |
| 1574 | +
|
| 1575 | + Parameters |
| 1576 | + ---------- |
| 1577 | + subset : label, array-like, IndexSlice, optional |
| 1578 | + A valid 1d input or single key along the index axis within |
| 1579 | + `DataFrame.loc[<subset>, :]`, to limit ``data`` to *before* applying |
| 1580 | + the function. |
1562 | 1581 |
|
1563 | 1582 | Returns
|
1564 | 1583 | -------
|
1565 | 1584 | self : Styler
|
| 1585 | +
|
| 1586 | + See Also |
| 1587 | + -------- |
| 1588 | + Styler.hide_columns: Hide the entire column headers row, or specific columns. |
| 1589 | +
|
| 1590 | + Examples |
| 1591 | + -------- |
| 1592 | + Simple application hiding specific rows: |
| 1593 | +
|
| 1594 | + >>> df = pd.DataFrame([[1,2], [3,4], [5,6]], index=["a", "b", "c"]) |
| 1595 | + >>> df.style.hide_index(["a", "b"]) |
| 1596 | + 0 1 |
| 1597 | + c 5 6 |
| 1598 | +
|
| 1599 | + Hide the index and retain the data values: |
| 1600 | +
|
| 1601 | + >>> midx = pd.MultiIndex.from_product([["x", "y"], ["a", "b", "c"]]) |
| 1602 | + >>> df = pd.DataFrame(np.random.randn(6,6), index=midx, columns=midx) |
| 1603 | + >>> df.style.format("{:.1f}").hide_index() |
| 1604 | + x y |
| 1605 | + a b c a b c |
| 1606 | + 0.1 0.0 0.4 1.3 0.6 -1.4 |
| 1607 | + 0.7 1.0 1.3 1.5 -0.0 -0.2 |
| 1608 | + 1.4 -0.8 1.6 -0.2 -0.4 -0.3 |
| 1609 | + 0.4 1.0 -0.2 -0.8 -1.2 1.1 |
| 1610 | + -0.6 1.2 1.8 1.9 0.3 0.3 |
| 1611 | + 0.8 0.5 -0.3 1.2 2.2 -0.8 |
| 1612 | +
|
| 1613 | + Hide specific rows but retain the index: |
| 1614 | +
|
| 1615 | + >>> df.style.format("{:.1f}").hide_index(subset=(slice(None), ["a", "c"])) |
| 1616 | + x y |
| 1617 | + a b c a b c |
| 1618 | + x b 0.7 1.0 1.3 1.5 -0.0 -0.2 |
| 1619 | + y b -0.6 1.2 1.8 1.9 0.3 0.3 |
| 1620 | +
|
| 1621 | + Hide specific rows and the index: |
| 1622 | +
|
| 1623 | + >>> df.style.format("{:.1f}").hide_index(subset=(slice(None), ["a", "c"])) |
| 1624 | + ... .hide_index() |
| 1625 | + x y |
| 1626 | + a b c a b c |
| 1627 | + 0.7 1.0 1.3 1.5 -0.0 -0.2 |
| 1628 | + -0.6 1.2 1.8 1.9 0.3 0.3 |
1566 | 1629 | """
|
1567 |
| - self.hidden_index = True |
| 1630 | + if subset is None: |
| 1631 | + self.hide_index_ = True |
| 1632 | + else: |
| 1633 | + subset_ = IndexSlice[subset, :] # new var so mypy reads not Optional |
| 1634 | + subset = non_reducing_slice(subset_) |
| 1635 | + hide = self.data.loc[subset] |
| 1636 | + hrows = self.index.get_indexer_for(hide.index) |
| 1637 | + # error: Incompatible types in assignment (expression has type |
| 1638 | + # "ndarray", variable has type "Sequence[int]") |
| 1639 | + self.hidden_rows = hrows # type: ignore[assignment] |
1568 | 1640 | return self
|
1569 | 1641 |
|
1570 |
| - def hide_columns(self, subset: Subset) -> Styler: |
| 1642 | + def hide_columns(self, subset: Subset | None = None) -> Styler: |
1571 | 1643 | """
|
1572 |
| - Hide columns from rendering. |
| 1644 | + Hide the column headers or specific keys in the columns from rendering. |
| 1645 | +
|
| 1646 | + This method has dual functionality: |
| 1647 | +
|
| 1648 | + - if ``subset`` is ``None`` then the entire column headers row will be hidden |
| 1649 | + whilst the data-values remain visible. |
| 1650 | + - if a ``subset`` is given then those specific columns, including the |
| 1651 | + data-values will be hidden, whilst the column headers row remains visible. |
| 1652 | +
|
| 1653 | + .. versionchanged:: 1.3.0 |
1573 | 1654 |
|
1574 | 1655 | Parameters
|
1575 | 1656 | ----------
|
1576 |
| - subset : label, array-like, IndexSlice |
1577 |
| - A valid 1d input or single key along the appropriate axis within |
1578 |
| - `DataFrame.loc[]`, to limit ``data`` to *before* applying the function. |
| 1657 | + subset : label, array-like, IndexSlice, optional |
| 1658 | + A valid 1d input or single key along the columns axis within |
| 1659 | + `DataFrame.loc[:, <subset>]`, to limit ``data`` to *before* applying |
| 1660 | + the function. |
1579 | 1661 |
|
1580 | 1662 | Returns
|
1581 | 1663 | -------
|
1582 | 1664 | self : Styler
|
| 1665 | +
|
| 1666 | + See Also |
| 1667 | + -------- |
| 1668 | + Styler.hide_index: Hide the entire index, or specific keys in the index. |
| 1669 | +
|
| 1670 | + Examples |
| 1671 | + -------- |
| 1672 | + Simple application hiding specific columns: |
| 1673 | +
|
| 1674 | + >>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"]) |
| 1675 | + >>> df.style.hide_columns(["a", "b"]) |
| 1676 | + c |
| 1677 | + 0 3 |
| 1678 | + 1 6 |
| 1679 | +
|
| 1680 | + Hide column headers and retain the data values: |
| 1681 | +
|
| 1682 | + >>> midx = pd.MultiIndex.from_product([["x", "y"], ["a", "b", "c"]]) |
| 1683 | + >>> df = pd.DataFrame(np.random.randn(6,6), index=midx, columns=midx) |
| 1684 | + >>> df.style.format("{:.1f}").hide_columns() |
| 1685 | + x d 0.1 0.0 0.4 1.3 0.6 -1.4 |
| 1686 | + e 0.7 1.0 1.3 1.5 -0.0 -0.2 |
| 1687 | + f 1.4 -0.8 1.6 -0.2 -0.4 -0.3 |
| 1688 | + y d 0.4 1.0 -0.2 -0.8 -1.2 1.1 |
| 1689 | + e -0.6 1.2 1.8 1.9 0.3 0.3 |
| 1690 | + f 0.8 0.5 -0.3 1.2 2.2 -0.8 |
| 1691 | +
|
| 1692 | + Hide specific columns but retain the column headers: |
| 1693 | +
|
| 1694 | + >>> df.style.format("{:.1f}").hide_columns(subset=(slice(None), ["a", "c"])) |
| 1695 | + x y |
| 1696 | + b b |
| 1697 | + x a 0.0 0.6 |
| 1698 | + b 1.0 -0.0 |
| 1699 | + c -0.8 -0.4 |
| 1700 | + y a 1.0 -1.2 |
| 1701 | + b 1.2 0.3 |
| 1702 | + c 0.5 2.2 |
| 1703 | +
|
| 1704 | + Hide specific columns and the column headers: |
| 1705 | +
|
| 1706 | + >>> df.style.format("{:.1f}").hide_columns(subset=(slice(None), ["a", "c"])) |
| 1707 | + ... .hide_columns() |
| 1708 | + x a 0.0 0.6 |
| 1709 | + b 1.0 -0.0 |
| 1710 | + c -0.8 -0.4 |
| 1711 | + y a 1.0 -1.2 |
| 1712 | + b 1.2 0.3 |
| 1713 | + c 0.5 2.2 |
1583 | 1714 | """
|
1584 |
| - subset = non_reducing_slice(subset) |
1585 |
| - hidden_df = self.data.loc[subset] |
1586 |
| - hcols = self.columns.get_indexer_for(hidden_df.columns) |
1587 |
| - # error: Incompatible types in assignment (expression has type |
1588 |
| - # "ndarray", variable has type "Sequence[int]") |
1589 |
| - self.hidden_columns = hcols # type: ignore[assignment] |
| 1715 | + if subset is None: |
| 1716 | + self.hide_columns_ = True |
| 1717 | + else: |
| 1718 | + subset_ = IndexSlice[:, subset] # new var so mypy reads not Optional |
| 1719 | + subset = non_reducing_slice(subset_) |
| 1720 | + hide = self.data.loc[subset] |
| 1721 | + hcols = self.columns.get_indexer_for(hide.columns) |
| 1722 | + # error: Incompatible types in assignment (expression has type |
| 1723 | + # "ndarray", variable has type "Sequence[int]") |
| 1724 | + self.hidden_columns = hcols # type: ignore[assignment] |
1590 | 1725 | return self
|
1591 | 1726 |
|
1592 | 1727 | # -----------------------------------------------------------------------
|
|
0 commit comments