-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
API: make hide_columns
and hide_index
have a consistent signature and function in Styler
#41266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jreback
merged 33 commits into
pandas-dev:master
from
attack68:hiding_data_columns_and_index
Jun 16, 2021
Merged
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
b0bfab7
ENH: make hide_index and hide_columns consistent in functionality
attack68 c615b08
DOC: improve specificity
attack68 38b7407
TST: add tests for hide_index(subset=)
attack68 40504db
TST: add tests for hide_columns(subset=None)
attack68 5199248
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 fc423dc
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 67598a4
remove show option
attack68 f05757a
remove show option
attack68 d870dc6
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 1dfad74
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 57aa0fc
type subset
attack68 0f05bc1
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 515707d
docstring for subset
attack68 fa8ecee
simpler examples
attack68 0704210
nomenclature: hide_columns_
attack68 cb58422
nomenclature: hide_index_
attack68 4d9626d
nomenclature: comments
attack68 461ad30
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 54bb183
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 c6c1086
merge upstream master
attack68 16a4da3
fix spelling
attack68 f890888
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 ec5817c
adjust to_lext components after recent merge
attack68 6da4e1f
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 35c6b65
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 7d55fc4
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 d51a3d7
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 8095415
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 0921804
merge upstream master
attack68 5e0cc0f
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 a6f811b
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 dca039d
Merge remote-tracking branch 'upstream/master' into hiding_data_colum…
attack68 b309358
Merge branch 'rls1.3.0' into hiding_data_columns_and_index
attack68 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,10 @@ | |
from pandas.util._decorators import doc | ||
|
||
import pandas as pd | ||
from pandas import RangeIndex | ||
from pandas import ( | ||
IndexSlice, | ||
RangeIndex, | ||
) | ||
from pandas.api.types import is_list_like | ||
from pandas.core import generic | ||
import pandas.core.common as com | ||
|
@@ -682,7 +685,7 @@ def to_latex( | |
self.data.columns = RangeIndex(stop=len(self.data.columns)) | ||
numeric_cols = self.data._get_numeric_data().columns.to_list() | ||
self.data.columns = _original_columns | ||
column_format = "" if self.hidden_index else "l" * self.data.index.nlevels | ||
column_format = "" if self.hide_index_ else "l" * self.data.index.nlevels | ||
for ci, _ in enumerate(self.data.columns): | ||
if ci not in self.hidden_columns: | ||
column_format += ( | ||
|
@@ -926,7 +929,7 @@ def _copy(self, deepcopy: bool = False) -> Styler: | |
) | ||
|
||
styler.uuid = self.uuid | ||
styler.hidden_index = self.hidden_index | ||
styler.hide_index_ = self.hide_index_ | ||
|
||
if deepcopy: | ||
styler.ctx = copy.deepcopy(self.ctx) | ||
|
@@ -965,7 +968,7 @@ def clear(self) -> None: | |
self.cell_context.clear() | ||
self._todo.clear() | ||
|
||
self.hidden_index = False | ||
self.hide_index_ = False | ||
self.hidden_columns = [] | ||
# self.format and self.table_styles may be dependent on user | ||
# input in self.__init__() | ||
|
@@ -1096,7 +1099,7 @@ def _applymap( | |
) -> Styler: | ||
func = partial(func, **kwargs) # applymap doesn't take kwargs? | ||
if subset is None: | ||
subset = pd.IndexSlice[:] | ||
subset = IndexSlice[:] | ||
subset = non_reducing_slice(subset) | ||
result = self.data.loc[subset].applymap(func) | ||
self._update_ctx(result) | ||
|
@@ -1509,37 +1512,169 @@ def set_na_rep(self, na_rep: str) -> StylerRenderer: | |
self.na_rep = na_rep | ||
return self.format(na_rep=na_rep, precision=self.precision) | ||
|
||
def hide_index(self) -> Styler: | ||
def hide_index(self, subset: Subset | None = None) -> Styler: | ||
""" | ||
Hide any indices from rendering. | ||
Hide the entire index, or specific keys in the index from rendering. | ||
|
||
This method has dual functionality: | ||
|
||
- if ``subset`` is ``None`` then the entire index will be hidden whilst | ||
displaying all data-rows. | ||
- if a ``subset`` is given then those specific rows will be hidden whilst the | ||
index itself remains visible. | ||
|
||
.. versionchanged:: 1.3.0 | ||
|
||
Parameters | ||
---------- | ||
subset : label, array-like, IndexSlice, optional | ||
A valid 1d input or single key along the index axis within | ||
`DataFrame.loc[<subset>, :]`, to limit ``data`` to *before* applying | ||
the function. | ||
|
||
Returns | ||
------- | ||
self : Styler | ||
|
||
See Also | ||
-------- | ||
Styler.hide_columns: Hide the entire column headers row, or specific columns. | ||
|
||
Examples | ||
-------- | ||
Simple application hiding specific rows: | ||
|
||
>>> df = pd.DataFrame([[1,2], [3,4], [5,6]], index=["a", "b", "c"]) | ||
>>> df.style.hide_index(["a", "b"]) | ||
0 1 | ||
c 5 6 | ||
|
||
Hide the index and retain the data values: | ||
|
||
>>> midx = pd.MultiIndex.from_product([["x", "y"], ["a", "b", "c"]]) | ||
>>> df = pd.DataFrame(np.random.randn(6,6), index=midx, columns=midx) | ||
>>> df.style.format("{:.1f}").hide_index() | ||
x y | ||
a b c a b c | ||
0.1 0.0 0.4 1.3 0.6 -1.4 | ||
0.7 1.0 1.3 1.5 -0.0 -0.2 | ||
1.4 -0.8 1.6 -0.2 -0.4 -0.3 | ||
0.4 1.0 -0.2 -0.8 -1.2 1.1 | ||
-0.6 1.2 1.8 1.9 0.3 0.3 | ||
0.8 0.5 -0.3 1.2 2.2 -0.8 | ||
|
||
Hide specific rows but retain the index: | ||
|
||
>>> df.style.format("{:.1f}").hide_index(subset=(slice(None), ["a", "c"])) | ||
x y | ||
a b c a b c | ||
x b 0.7 1.0 1.3 1.5 -0.0 -0.2 | ||
y b -0.6 1.2 1.8 1.9 0.3 0.3 | ||
|
||
Hide specific rows and the index: | ||
|
||
>>> df.style.format("{:.1f}").hide_index(subset=(slice(None), ["a", "c"])) | ||
... .hide_index() | ||
x y | ||
a b c a b c | ||
0.7 1.0 1.3 1.5 -0.0 -0.2 | ||
-0.6 1.2 1.8 1.9 0.3 0.3 | ||
""" | ||
self.hidden_index = True | ||
if subset is None: | ||
self.hide_index_ = True | ||
else: | ||
subset_ = IndexSlice[subset, :] # new var so mypy reads not Optional | ||
subset = non_reducing_slice(subset_) | ||
hide = self.data.loc[subset] | ||
hrows = self.index.get_indexer_for(hide.index) | ||
# error: Incompatible types in assignment (expression has type | ||
# "ndarray", variable has type "Sequence[int]") | ||
self.hidden_rows = hrows # type: ignore[assignment] | ||
return self | ||
|
||
def hide_columns(self, subset: Subset) -> Styler: | ||
def hide_columns(self, subset: Subset | None = None) -> Styler: | ||
""" | ||
Hide columns from rendering. | ||
Hide the column headers or specific keys in the columns from rendering. | ||
|
||
This method has dual functionality: | ||
|
||
- if ``subset`` is ``None`` then the entire column headers row will be hidden | ||
whilst the data-values remain visible. | ||
- if a ``subset`` is given then those specific columns, including the | ||
data-values will be hidden, whilst the column headers row remains visible. | ||
|
||
.. versionchanged:: 1.3.0 | ||
|
||
Parameters | ||
---------- | ||
subset : label, array-like, IndexSlice | ||
A valid 1d input or single key along the appropriate axis within | ||
`DataFrame.loc[]`, to limit ``data`` to *before* applying the function. | ||
subset : label, array-like, IndexSlice, optional | ||
A valid 1d input or single key along the columns axis within | ||
`DataFrame.loc[:, <subset>]`, to limit ``data`` to *before* applying | ||
the function. | ||
|
||
Returns | ||
------- | ||
self : Styler | ||
|
||
See Also | ||
-------- | ||
Styler.hide_index: Hide the entire index, or specific keys in the index. | ||
|
||
Examples | ||
-------- | ||
Simple application hiding specific columns: | ||
|
||
>>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["a", "b", "c"]) | ||
>>> df.style.hide_columns(["a", "b"]) | ||
c | ||
0 3 | ||
1 6 | ||
|
||
Hide column headers and retain the data values: | ||
|
||
>>> midx = pd.MultiIndex.from_product([["x", "y"], ["a", "b", "c"]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you show an example first that has a single level index. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
>>> df = pd.DataFrame(np.random.randn(6,6), index=midx, columns=midx) | ||
>>> df.style.format("{:.1f}").hide_columns() | ||
x d 0.1 0.0 0.4 1.3 0.6 -1.4 | ||
e 0.7 1.0 1.3 1.5 -0.0 -0.2 | ||
f 1.4 -0.8 1.6 -0.2 -0.4 -0.3 | ||
y d 0.4 1.0 -0.2 -0.8 -1.2 1.1 | ||
e -0.6 1.2 1.8 1.9 0.3 0.3 | ||
f 0.8 0.5 -0.3 1.2 2.2 -0.8 | ||
|
||
Hide specific columns but retain the column headers: | ||
|
||
>>> df.style.format("{:.1f}").hide_columns(subset=(slice(None), ["a", "c"])) | ||
x y | ||
b b | ||
x a 0.0 0.6 | ||
b 1.0 -0.0 | ||
c -0.8 -0.4 | ||
y a 1.0 -1.2 | ||
b 1.2 0.3 | ||
c 0.5 2.2 | ||
|
||
Hide specific columns and the column headers: | ||
|
||
>>> df.style.format("{:.1f}").hide_columns(subset=(slice(None), ["a", "c"])) | ||
... .hide_columns() | ||
x a 0.0 0.6 | ||
b 1.0 -0.0 | ||
c -0.8 -0.4 | ||
y a 1.0 -1.2 | ||
b 1.2 0.3 | ||
c 0.5 2.2 | ||
""" | ||
subset = non_reducing_slice(subset) | ||
hidden_df = self.data.loc[subset] | ||
hcols = self.columns.get_indexer_for(hidden_df.columns) | ||
# error: Incompatible types in assignment (expression has type | ||
# "ndarray", variable has type "Sequence[int]") | ||
self.hidden_columns = hcols # type: ignore[assignment] | ||
if subset is None: | ||
self.hide_columns_ = True | ||
else: | ||
subset_ = IndexSlice[:, subset] # new var so mypy reads not Optional | ||
subset = non_reducing_slice(subset_) | ||
hide = self.data.loc[subset] | ||
hcols = self.columns.get_indexer_for(hide.columns) | ||
# error: Incompatible types in assignment (expression has type | ||
# "ndarray", variable has type "Sequence[int]") | ||
self.hidden_columns = hcols # type: ignore[assignment] | ||
return self | ||
|
||
# ----------------------------------------------------------------------- | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.