-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
API: Styler.hide_columns
replaced by .hide_values
which also operates row-wise
#41158
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ac19894
initial hide_values method
attack68 7af2849
initial hide_values method
attack68 3ec679f
add tests
attack68 8bdb867
add tests
attack68 e9b011c
add doc examples
attack68 654015e
mypy error: Slice index must be an integer or None
attack68 3b55523
Merge remote-tracking branch 'upstream/master' into depr_hide_columns
attack68 0fad4e2
Merge remote-tracking branch 'upstream/master' into depr_hide_columns
attack68 513fc25
Merge remote-tracking branch 'upstream/master' into depr_hide_columns
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 |
---|---|---|
|
@@ -28,6 +28,7 @@ | |
from pandas.util._decorators import doc | ||
|
||
import pandas as pd | ||
from pandas import IndexSlice | ||
from pandas.api.types import is_list_like | ||
from pandas.core import generic | ||
import pandas.core.common as com | ||
|
@@ -640,7 +641,7 @@ def apply( | |
def _applymap(self, func: Callable, subset=None, **kwargs) -> 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) | ||
|
@@ -1060,12 +1061,76 @@ def hide_columns(self, subset) -> Styler: | |
------- | ||
self : Styler | ||
""" | ||
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] | ||
return self.hide_values(subset) | ||
|
||
def hide_values(self, subset, axis: Axis = "columns", show: bool = False) -> Styler: | ||
""" | ||
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. i wouldn't make this user visible instead prefer hide_index, hide_columns (this can be the impl) |
||
Hide (or exclusively show) columns or rows upon rendering. | ||
|
||
Parameters | ||
---------- | ||
subset : IndexSlice | ||
An valid input to a specific ``axis`` in ``DataFrame.loc`` that identifies | ||
which columns or rows are hidden/shown. | ||
axis : {0 or 'index', 1 or 'columns'} | ||
Axis along which the ``subset`` is applied. | ||
show : bool | ||
Indicates whether the supplied subset should be hidden, or exclusively | ||
shown. | ||
|
||
Returns | ||
------- | ||
self : Styler | ||
|
||
Examples | ||
-------- | ||
>>> df = DataFrame([[1, 2], [3, 4]], columns=["c1", "c2"], index=["i1", "i2"]) | ||
>>> df.style.hide_values("c1") | ||
c2 | ||
i1 2 | ||
i2 4 | ||
|
||
>>> df.style.hide_values("i1", axis="index") | ||
c1 c2 | ||
i2 3 4 | ||
|
||
>>> df.style.hide_values("i1", axis="index", show=True) | ||
c1 c2 | ||
i1 1 2 | ||
|
||
>>> mcols = MultiIndex.from_product([["c1", "c2"], ["d1", "d2", "d3"]]) | ||
>>> data = np.arange(12).reshape((2,6)) | ||
>>> df = DataFrame(data, columns=mcols, index=["i1", "i2"]) | ||
>>> df.style.hide_values(subset=(slice(None), "d2":"d3")) | ||
c1 c2 | ||
d1 d1 | ||
i1 0 6 | ||
i2 3 9 | ||
""" | ||
if axis in [0, "index"]: | ||
subset = IndexSlice[subset, :] | ||
subset = non_reducing_slice(subset) | ||
hide = self.data.loc[subset] | ||
if show: # invert the display | ||
hide = self.data.loc[~self.data.index.isin(hide.index.to_list()), :] | ||
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] | ||
elif axis in [1, "columns"]: | ||
subset = IndexSlice[:, subset] | ||
subset = non_reducing_slice(subset) | ||
hide = self.data.loc[subset] | ||
if show: # invert the display | ||
hide = self.data.loc[:, ~self.data.columns.isin(hide.columns.to_list())] | ||
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] | ||
else: | ||
raise ValueError( | ||
f"`axis` must be one of [0, 1] or 'index' or 'columns', got: {axis}" | ||
) | ||
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i find this a very confusing name, hide_axis is more appropriate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trick is to distinguish between the cases of:
a) hiding select rows or columns of data (whilst other index keys or column headers are visible),
b) or displaying the data values but just hiding the index or column headers row in their entirety.
Currently
hide_columns
does a) whilsthide_index
does b). The complete idea was that:i)
hide_values
would do a) for either the index or columns axes.ii)
hide_headers
(a new method) would do b) for either the index or columns axes.?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback i created an alternative in #41266 which seeks to achieve the same functionality in a different way re-using exiting methods and kwargs. Close this PR if you prefer the alternative,