-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Add Styler.where #17474
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
ENH: Add Styler.where #17474
Conversation
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 |
c386656
to
9cf48fb
Compare
Codecov Report
@@ Coverage Diff @@
## master #17474 +/- ##
==========================================
- Coverage 91.16% 91.14% -0.02%
==========================================
Files 163 163
Lines 49561 49561
==========================================
- Hits 45180 45171 -9
- Misses 4381 4390 +9
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #17474 +/- ##
==========================================
- Coverage 91.15% 91.13% -0.02%
==========================================
Files 163 163
Lines 49534 49534
==========================================
- Hits 45153 45144 -9
- Misses 4381 4390 +9
Continue to review full report at Codecov.
|
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.
can you add a whatsnew note on Other Enahancements as well as use this in the notebook where appropriate
pandas/io/formats/style.py
Outdated
""" | ||
Apply a function elementwise, updating the HTML | ||
representation with a style which is selected in | ||
accordance with the return value of a function. |
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.
add a versionadded tag
pandas/io/formats/style.py
Outdated
@@ -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): | |||
""" |
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.
defaults should be None
not ''
pandas/io/formats/style.py
Outdated
|
||
Parameters | ||
---------- | ||
func : function |
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.
callable
pandas/io/formats/style.py
Outdated
---------- | ||
func : function | ||
``func`` should take a scalar and return a boolean | ||
style1 : a style applied when ``func`` returns true |
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.
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): |
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.
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 |
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.
These tests are fine, but could you add one test that checks the generated values directly against the dictionary?
9cf48fb
to
c6dc109
Compare
@jreback @TomAugspurger Thanks for your review. Fixed and added some new tests. |
Travis CI seems not work well due to its own error. |
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.
this seems ok, @TomAugspurger . do we have a section in api.rst for things like this?
doc/source/whatsnew/v0.21.0.txt
Outdated
@@ -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`). |
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.
pls label this as a convenience for Styler.applymap
pandas/io/formats/style.py
Outdated
|
||
Parameters | ||
---------- | ||
func : callable |
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.
this should mimic the signature of DataFrame.where
as much as possible
func -> cond
style1 -> value
style2 -> other
e534c8b
to
5f513db
Compare
@jreback Thanks for your suggestion. Fixed! |
doc/source/whatsnew/v0.21.0.txt
Outdated
@@ -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`). |
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.
use :func:`Styler.applymap`
as well
|
||
Returns | ||
------- | ||
self : Styler |
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.
can you add a See Also
here (pointing to .applymap) and the reciprical one there
doc/source/whatsnew/v0.21.0.txt
Outdated
@@ -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`). | |||
|
|||
|
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.
.where
needs to be added to api.rst
5f513db
to
9542138
Compare
9542138
to
82ae1dc
Compare
@jreback Thanks. All is now fixed. |
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 |
Added
test_where_subset
inpandas/tests/io/formats/test_style.py
.git diff upstream/master -u -- "*.py" | flake8 --diff
It makes the redundant style condition simple.