ENH: Add Styler.where by Licht-T · Pull Request #17474 · pandas-dev/pandas (original) (raw)
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 andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Conversation21 Commits5 Checks0 Files changed
Conversation
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
[ Show hidden characters]({{ revealButtonHref }})
- closes API: Add Styler.where #16255
- tests added / passed
Addedtest_where_subset
inpandas/tests/io/formats/test_style.py
. - passes
git diff upstream/master -u -- "*.py" | flake8 --diff
- whatsnew entry
It makes the redundant style condition simple.
df.style.applymap(lambda val: 'color: %s' % 'red' if val < 0 else 'black') df.style.where(lambda val: val < 0, 'color: red', 'color: black')
Hello @Licht-T! Thanks for updating the PR.
Cheers ! There are no PEP8 issues in this Pull Request. 🍻
Comment last updated on September 10, 2017 at 14:48 Hours UTC
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a whatsnew note on Other Enahancements as well as use this in the notebook where appropriate
""" |
---|
Apply a function elementwise, updating the HTML |
representation with a style which is selected in |
accordance with the return value of a function. |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a versionadded tag
@@ -623,6 +623,32 @@ def applymap(self, func, subset=None, **kwargs): |
---|
(func, subset), kwargs)) |
return self |
def where(self, func, style1, style2='', subset=None, **kwargs): |
""" |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defaults should be None
not ''
Parameters |
---------- |
func : function |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
callable
---------- |
---|
func : function |
``func`` should take a scalar and return a boolean |
style1 : a style applied when ``func`` returns true |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style1: str
when applied......
@@ -265,6 +265,26 @@ def f(x): |
---|
col in self.df.loc[slice_].columns) |
assert result == expected |
def test_where_subset(self): |
def f(x): |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a comment with the issue number (this PR number)
for slice_ in slices: |
---|
result = self.df.style.where(f, style1, style2, |
subset=slice_)._compute().ctx |
expected = self.df.style.applymap(g, subset=slice_)._compute().ctx |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests are fine, but could you add one test that checks the generated values directly against the dictionary?
Travis CI seems not work well due to its own error.
Could someone restart the Travis CI build?
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this seems ok, @TomAugspurger . do we have a section in api.rst for things like this?
@@ -112,6 +112,7 @@ Other Enhancements |
---|
- `read_*` methods can now infer compression from non-string paths, such as ``pathlib.Path`` objects (:issue:`17206`). |
- :func:`pd.read_sas()` now recognizes much more of the most frequently used date (datetime) formats in SAS7BDAT files (:issue:`15871`). |
- :func:`DataFrame.items` and :func:`Series.items` is now present in both Python 2 and 3 and is lazy in all cases (:issue:`13918`, :issue:`17213`) |
- :func:`Styler.where` has been implemented. It enables simple DataFrame styling on the Jupyter notebook (:issue:`17474`). |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pls label this as a convenience for Styler.applymap
Parameters |
---------- |
func : callable |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should mimic the signature of DataFrame.where
as much as possible
func -> cond
style1 -> value
style2 -> other
@jreback Thanks for your suggestion. Fixed!
@@ -112,6 +112,7 @@ Other Enhancements |
---|
- `read_*` methods can now infer compression from non-string paths, such as ``pathlib.Path`` objects (:issue:`17206`). |
- :func:`pd.read_sas()` now recognizes much more of the most frequently used date (datetime) formats in SAS7BDAT files (:issue:`15871`). |
- :func:`DataFrame.items` and :func:`Series.items` is now present in both Python 2 and 3 and is lazy in all cases (:issue:`13918`, :issue:`17213`) |
- :func:`Styler.where` has been implemented. It is as a convenience for Styler.applymap and enables simple DataFrame styling on the Jupyter notebook (:issue:`17474`). |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use :func:`Styler.applymap`
as well
Returns |
------- |
self : Styler |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a See Also
here (pointing to .applymap) and the reciprical one there
@@ -112,6 +112,7 @@ Other Enhancements |
---|
- `read_*` methods can now infer compression from non-string paths, such as ``pathlib.Path`` objects (:issue:`17206`). |
- :func:`pd.read_sas()` now recognizes much more of the most frequently used date (datetime) formats in SAS7BDAT files (:issue:`15871`). |
- :func:`DataFrame.items` and :func:`Series.items` is now present in both Python 2 and 3 and is lazy in all cases (:issue:`13918`, :issue:`17213`) |
- :func:`Styler.where` has been implemented. It is as a convenience for Styler.applymap and enables simple DataFrame styling on the Jupyter notebook (:issue:`17474`). |
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.where
needs to be added to api.rst
thanks @Licht-T
If you'd have a pass thru the actual documentation notebook and make a PR for anything where we should logically use .where
to make the syntax more clear would be great (or indicate that it looks good).
alanbato pushed a commit to alanbato/pandas that referenced this pull request
No-Stream pushed a commit to No-Stream/pandas that referenced this pull request