-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Incomplete Styler copy methods fix (#39708) #39975
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
Conversation
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.
looks like a pretty good start. some comments inline.
pandas/io/formats/style.py
Outdated
na_rep=self.na_rep, | ||
uuid_len=self.uuid_len, |
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.
ignore the uuid_len
since the uuid
is explicitly provided
pandas/io/formats/style.py
Outdated
@@ -783,15 +783,29 @@ def _copy(self, deepcopy: bool = False) -> Styler: | |||
precision=self.precision, | |||
caption=self.caption, | |||
uuid=self.uuid, |
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 uuid
is suffixed with "_" so need to trim this off here self.uuid[:-1]
edit: actually it might be cleaner and always work if the uuid
is manually overwritten after class instantiation.
doc/source/whatsnew/v1.3.0.rst
Outdated
@@ -482,6 +482,7 @@ Other | |||
- Bug in :class:`Styler` where rendered HTML was missing a column class identifier for certain header cells (:issue:`39716`) | |||
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`) | |||
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`) | |||
- Bug in :class:`Styler` copy methods which were not updated for recently introduced attributes (:issue:`39708`) |
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 move this up to where other Styler
Items are listed
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 may not be necessary if this changes were in 1.3 (which they might be); you can add this issue number with another change for Styler (e.g. where the attributes were changed)
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 has to be 1.3, since tooltips is the most recent and thats in 1.3.0, referenced in this PR
pandas/io/formats/style.py
Outdated
if deepcopy: | ||
styler.ctx = copy.deepcopy(self.ctx) | ||
styler._todo = copy.deepcopy(self._todo) | ||
styler.table_styles = copy.deepcopy(self.table_styles) | ||
styler.hidden_columns = copy.deepcopy(self.hidden_columns) |
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.
think hidden_columns
is a list, does it need deepcopy?
) | ||
self.styler.set_tooltips(ttips) | ||
self.styler.cell_ids = not self.styler.cell_ids | ||
self.styler.format("{:.2%}") |
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.
not making any changes to _todo[]. maybe add .apply(..)
self.styler.table_styles[0]["selector"] = "ti" | ||
assert self.styler.table_styles != s2.table_styles | ||
if do_changes: # self.styler.tooltips is not None | ||
tm.assert_frame_equal(self.styler.tooltips.tt_data, s2.tooltips.tt_data) |
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.
possible to wrap the two test cases copy and deepcopy so that dont repeat so much code?
|
||
assert self.styler.table_styles is s2.table_styles | ||
assert self.styler.hidden_columns is s2.hidden_columns | ||
assert self.styler.cell_context is s2.cell_context |
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 group the shallow variables in test to make it clearer?
assert self.styler.table_styles == s2.table_styles | ||
assert self.styler.table_attributes == s2.table_attributes | ||
assert self.styler.cell_ids == s2.cell_ids | ||
assert self.styler.uuid_len == s2.uuid_len |
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.
uuid
is the key variable, uuid_len
is only used in construction of uuid
test for that instead.
doc/source/whatsnew/v1.3.0.rst
Outdated
@@ -482,6 +482,7 @@ Other | |||
- Bug in :class:`Styler` where rendered HTML was missing a column class identifier for certain header cells (:issue:`39716`) | |||
- Bug in :meth:`Styler.background_gradient` where text-color was not determined correctly (:issue:`39888`) | |||
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`) | |||
- Bug in :class:`Styler` copy methods which were not updated for recently introduced attributes (:issue:`39708`) |
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 may not be necessary if this changes were in 1.3 (which they might be); you can add this issue number with another change for Styler (e.g. where the attributes were changed)
When committing my changes based on your comments, do I always have to add 'BUG: ... (#39708)' to my commit messages? Or is it okay if I just write something like 'Mention issue number in enhancements section'? I prefer atomic commits, but do all my commits end up upstream after the PR is merged? Shall I do only one single commit for all my changes instead? |
when merged to master will be squashed to one commit. do as many commits as you want, when you need a new review generally ping for one, wait patiently, or request here. :) |
Thanks, and good to know! |
Hi, here's my next try! |
@@ -781,16 +781,29 @@ def _copy(self, deepcopy: bool = False) -> Styler: | |||
self.data, | |||
precision=self.precision, |
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.
is there a reason we actually care about deepcopy, IOW why are we not just always a shallow copy? is this actually used somewhere?
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.
Personally, I have never had use to copy a Styler
. But I can see a use case where, in a notebook or report, you might have a dataframe that has some preliminary, shared styling and then some divergence where you highlight different properties as part of a narrative.
I think, off the top of my head, in this scenario you would need a deepcopy to prevent any updates from intermingling with each other, since much of the core styling I think comes from the attributes where the shallow copy will have only single shared pointed reference.
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.
lgtm.
leaving the discussion about whether copy and/or deepcopy are needed, this I think fixes the existing functionality which has been broken by most of the recent changes.
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.
lgtm. @rlukevie can you merge master and ping on green
thanks @rlukevie |
copy
methods not updated for recent additions #39708I haven't made so many pull requests before, so I hope this is going into the right direction.