|
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
|
@@ -682,7 +685,7 @@ def to_latex(
|
682 | 685 | self.data.columns = RangeIndex(stop=len(self.data.columns))
|
683 | 686 | numeric_cols = self.data._get_numeric_data().columns.to_list()
|
684 | 687 | self.data.columns = _original_columns
|
685 |
| - column_format = "" if self.hidden_index else "l" * self.data.index.nlevels |
| 688 | + column_format = "" if self.hide_index_ else "l" * self.data.index.nlevels |
686 | 689 | for ci, _ in enumerate(self.data.columns):
|
687 | 690 | if ci not in self.hidden_columns:
|
688 | 691 | column_format += (
|
@@ -926,7 +929,7 @@ def _copy(self, deepcopy: bool = False) -> Styler:
|
926 | 929 | )
|
927 | 930 |
|
928 | 931 | styler.uuid = self.uuid
|
929 |
| - styler.hidden_index = self.hidden_index |
| 932 | + styler.hide_index_ = self.hide_index_ |
930 | 933 |
|
931 | 934 | if deepcopy:
|
932 | 935 | styler.ctx = copy.deepcopy(self.ctx)
|
@@ -965,7 +968,7 @@ def clear(self) -> None:
|
965 | 968 | self.cell_context.clear()
|
966 | 969 | self._todo.clear()
|
967 | 970 |
|
968 |
| - self.hidden_index = False |
| 971 | + self.hide_index_ = False |
969 | 972 | self.hidden_columns = []
|
970 | 973 | # self.format and self.table_styles may be dependent on user
|
971 | 974 | # input in self.__init__()
|
@@ -1096,7 +1099,7 @@ def _applymap(
|
1096 | 1099 | ) -> Styler:
|
1097 | 1100 | func = partial(func, **kwargs) # applymap doesn't take kwargs?
|
1098 | 1101 | if subset is None:
|
1099 |
| - subset = pd.IndexSlice[:] |
| 1102 | + subset = IndexSlice[:] |
1100 | 1103 | subset = non_reducing_slice(subset)
|
1101 | 1104 | result = self.data.loc[subset].applymap(func)
|
1102 | 1105 | self._update_ctx(result)
|
@@ -1511,37 +1514,169 @@ def set_na_rep(self, na_rep: str) -> StylerRenderer:
|
1511 | 1514 | self.na_rep = na_rep
|
1512 | 1515 | return self.format(na_rep=na_rep, precision=self.precision)
|
1513 | 1516 |
|
1514 |
| - def hide_index(self) -> Styler: |
| 1517 | + def hide_index(self, subset: Subset | None = None) -> Styler: |
1515 | 1518 | """
|
1516 |
| - Hide any indices from rendering. |
| 1519 | + Hide the entire index, or specific keys in the index from rendering. |
| 1520 | +
|
| 1521 | + This method has dual functionality: |
| 1522 | +
|
| 1523 | + - if ``subset`` is ``None`` then the entire index will be hidden whilst |
| 1524 | + displaying all data-rows. |
| 1525 | + - if a ``subset`` is given then those specific rows will be hidden whilst the |
| 1526 | + index itself remains visible. |
| 1527 | +
|
| 1528 | + .. versionchanged:: 1.3.0 |
| 1529 | +
|
| 1530 | + Parameters |
| 1531 | + ---------- |
| 1532 | + subset : label, array-like, IndexSlice, optional |
| 1533 | + A valid 1d input or single key along the index axis within |
| 1534 | + `DataFrame.loc[<subset>, :]`, to limit ``data`` to *before* applying |
| 1535 | + the function. |
1517 | 1536 |
|
1518 | 1537 | Returns
|
1519 | 1538 | -------
|
1520 | 1539 | self : Styler
|
| 1540 | +
|
| 1541 | + See Also |
| 1542 | + -------- |
| 1543 | + Styler.hide_columns: Hide the entire column headers row, or specific columns. |
| 1544 | +
|
| 1545 | + Examples |
| 1546 | + -------- |
| 1547 | + Simple application hiding specific rows: |
| 1548 | +
|
| 1549 | + >>> df = pd.DataFrame([[1,2], [3,4], [5,6]], index=["a", "b", "c"]) |
| 1550 | + >>> df.style.hide_index(["a", "b"]) |
| 1551 | + 0 1 |
| 1552 | + c 5 6 |
| 1553 | +
|
| 1554 | + Hide the index and retain the data values: |
| 1555 | +
|
| 1556 | + >>> midx = pd.MultiIndex.from_product([["x", "y"], ["a", "b", "c"]]) |
| 1557 | + >>> df = pd.DataFrame(np.random.randn(6,6), index=midx, columns=midx) |
| 1558 | + >>> df.style.format("{:.1f}").hide_index() |
| 1559 | + x y |
| 1560 | + a b c a b c |
| 1561 | + 0.1 0.0 0.4 1.3 0.6 -1.4 |
| 1562 | + 0.7 1.0 1.3 1.5 -0.0 -0.2 |
| 1563 | + 1.4 -0.8 1.6 -0.2 -0.4 -0.3 |
| 1564 | + 0.4 1.0 -0.2 -0.8 -1.2 1.1 |
| 1565 | + -0.6 1.2 1.8 1.9 0.3 0.3 |
| 1566 | + 0.8 0.5 -0.3 1.2 2.2 -0.8 |
| 1567 | +
|
| 1568 | + Hide specific rows but retain the index: |
| 1569 | +
|
| 1570 | + >>> df.style.format("{:.1f}").hide_index(subset=(slice(None), ["a", "c"])) |
| 1571 | + x y |
| 1572 | + a b c a b c |
| 1573 | + x b 0.7 1.0 1.3 1.5 -0.0 -0.2 |
| 1574 | + y b -0.6 1.2 1.8 1.9 0.3 0.3 |
| 1575 | +
|
| 1576 | + Hide specific rows and the index: |
| 1577 | +
|
| 1578 | + >>> df.style.format("{:.1f}").hide_index(subset=(slice(None), ["a", "c"])) |
| 1579 | + ... .hide_index() |
| 1580 | + x y |
| 1581 | + a b c a b c |
| 1582 | + 0.7 1.0 1.3 1.5 -0.0 -0.2 |
| 1583 | + -0.6 1.2 1.8 1.9 0.3 0.3 |
1521 | 1584 | """
|
1522 |
| - self.hidden_index = True |
| 1585 | + if subset is None: |
| 1586 | + self.hide_index_ = True |
| 1587 | + else: |
| 1588 | + subset_ = IndexSlice[subset, :] # new var so mypy reads not Optional |
| 1589 | + subset = non_reducing_slice(subset_) |
| 1590 | + hide = self.data.loc[subset] |
| 1591 | + hrows = self.index.get_indexer_for(hide.index) |
| 1592 | + # error: Incompatible types in assignment (expression has type |
| 1593 | + # "ndarray", variable has type "Sequence[int]") |
| 1594 | + self.hidden_rows = hrows # type: ignore[assignment] |
1523 | 1595 | return self
|
1524 | 1596 |
|
1525 |
| - def hide_columns(self, subset: Subset) -> Styler: |
| 1597 | + def hide_columns(self, subset: Subset | None = None) -> Styler: |
1526 | 1598 | """
|
1527 |
| - Hide columns from rendering. |
| 1599 | + Hide the column headers or specific keys in the columns from rendering. |
| 1600 | +
|
| 1601 | + This method has dual functionality: |
| 1602 | +
|
| 1603 | + - if ``subset`` is ``None`` then the entire column headers row will be hidden |
| 1604 | + whilst the data-values remain visible. |
| 1605 | + - if a ``subset`` is given then those specific columns, including the |
| 1606 | + data-values will be hidden, whilst the column headers row remains visible. |
| 1607 | +
|
| 1608 | + .. versionchanged:: 1.3.0 |
1528 | 1609 |
|
1529 | 1610 | Parameters
|
1530 | 1611 | ----------
|
1531 |
| - subset : label, array-like, IndexSlice |
1532 |
| - A valid 1d input or single key along the appropriate axis within |
1533 |
| - `DataFrame.loc[]`, to limit ``data`` to *before* applying the function. |
| 1612 | + subset : label, array-like, IndexSlice, optional |
| 1613 | + A valid 1d input or single key along the columns axis within |
| 1614 | + `DataFrame.loc[:, <subset>]`, to limit ``data`` to *before* applying |
| 1615 | + the function. |
1534 | 1616 |
|
1535 | 1617 | Returns
|
1536 | 1618 | -------
|
1537 | 1619 | self : Styler
|
| 1620 | +
|
| 1621 | + See Also |
| 1622 | + -------- |
| 1623 | + Styler.hide_index: Hide the entire index, or specific keys in the index. |
| 1624 | +
|
| 1625 | + Examples |
| 1626 | + -------- |
| 1627 | + Simple application hiding specific columns: |
| 1628 | +
|
| 1629 | + >>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"]) |
| 1630 | + >>> df.style.hide_columns(["a", "b"]) |
| 1631 | + c |
| 1632 | + 0 3 |
| 1633 | + 1 6 |
| 1634 | +
|
| 1635 | + Hide column headers and retain the data values: |
| 1636 | +
|
| 1637 | + >>> midx = pd.MultiIndex.from_product([["x", "y"], ["a", "b", "c"]]) |
| 1638 | + >>> df = pd.DataFrame(np.random.randn(6,6), index=midx, columns=midx) |
| 1639 | + >>> df.style.format("{:.1f}").hide_columns() |
| 1640 | + x d 0.1 0.0 0.4 1.3 0.6 -1.4 |
| 1641 | + e 0.7 1.0 1.3 1.5 -0.0 -0.2 |
| 1642 | + f 1.4 -0.8 1.6 -0.2 -0.4 -0.3 |
| 1643 | + y d 0.4 1.0 -0.2 -0.8 -1.2 1.1 |
| 1644 | + e -0.6 1.2 1.8 1.9 0.3 0.3 |
| 1645 | + f 0.8 0.5 -0.3 1.2 2.2 -0.8 |
| 1646 | +
|
| 1647 | + Hide specific columns but retain the column headers: |
| 1648 | +
|
| 1649 | + >>> df.style.format("{:.1f}").hide_columns(subset=(slice(None), ["a", "c"])) |
| 1650 | + x y |
| 1651 | + b b |
| 1652 | + x a 0.0 0.6 |
| 1653 | + b 1.0 -0.0 |
| 1654 | + c -0.8 -0.4 |
| 1655 | + y a 1.0 -1.2 |
| 1656 | + b 1.2 0.3 |
| 1657 | + c 0.5 2.2 |
| 1658 | +
|
| 1659 | + Hide specific columns and the column headers: |
| 1660 | +
|
| 1661 | + >>> df.style.format("{:.1f}").hide_columns(subset=(slice(None), ["a", "c"])) |
| 1662 | + ... .hide_columns() |
| 1663 | + x a 0.0 0.6 |
| 1664 | + b 1.0 -0.0 |
| 1665 | + c -0.8 -0.4 |
| 1666 | + y a 1.0 -1.2 |
| 1667 | + b 1.2 0.3 |
| 1668 | + c 0.5 2.2 |
1538 | 1669 | """
|
1539 |
| - subset = non_reducing_slice(subset) |
1540 |
| - hidden_df = self.data.loc[subset] |
1541 |
| - hcols = self.columns.get_indexer_for(hidden_df.columns) |
1542 |
| - # error: Incompatible types in assignment (expression has type |
1543 |
| - # "ndarray", variable has type "Sequence[int]") |
1544 |
| - self.hidden_columns = hcols # type: ignore[assignment] |
| 1670 | + if subset is None: |
| 1671 | + self.hide_columns_ = True |
| 1672 | + else: |
| 1673 | + subset_ = IndexSlice[:, subset] # new var so mypy reads not Optional |
| 1674 | + subset = non_reducing_slice(subset_) |
| 1675 | + hide = self.data.loc[subset] |
| 1676 | + hcols = self.columns.get_indexer_for(hide.columns) |
| 1677 | + # error: Incompatible types in assignment (expression has type |
| 1678 | + # "ndarray", variable has type "Sequence[int]") |
| 1679 | + self.hidden_columns = hcols # type: ignore[assignment] |
1545 | 1680 | return self
|
1546 | 1681 |
|
1547 | 1682 | # -----------------------------------------------------------------------
|
|
0 commit comments