Skip to content

WIP: Better formatting for .set_precision #11667

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
wants to merge 10 commits into from
47 changes: 41 additions & 6 deletions pandas/core/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from contextlib import contextmanager
from uuid import uuid1
import copy
import numbers
from collections import defaultdict

try:
Expand Down Expand Up @@ -113,7 +114,13 @@ class Styler(object):
{% for c in r %}
<{{c.type}} id="T_{{uuid}}{{c.id}}" class="{{c.class}}">
{% if c.value is number %}
{{c.value|round(precision)}}
{% if c.precision is defined and c.precision is number %}
{{c.value|round(c.precision)}}
{% elif c.precision is defined and c.precision is string %}
{{c.precision.format(c.value)}}
{% else %}
{{c.value|round(precision.__default__)}}
{% endif %}
{% else %}
{{c.value}}
{% endif %}
Expand Down Expand Up @@ -144,7 +151,18 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None,
self.table_styles = table_styles
self.caption = caption
if precision is None:
precision = pd.options.display.precision
precision = {'__default__': pd.options.display.precision}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just call .set_precision instead of repeating this (and all of this logic should be there).

Further I think that a .set_precision() should reset to the default (e.g. pd.options.display.precision), so pls put in a test for this.

elif isinstance(precision, numbers.Integral):
precision = {'__default__': pd.options.display.precision}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use com.is_integer(...) instead here

elif isinstance(precision, dict):
precision = {k: precision[k] for k in precision} # prevent tampering
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is going to be a column mapping? need to check that

if '__default__' not in precision:
precision['__default__'] = pd.options.display.precision
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would alway set the precision to the default (or if a number is passed override to that). Then you can further set precision BY column with another call.

raise TypeError('Precision is of an unknown type, it has to be None,\
an integer or a dict containing formats for specific\
columns.')

self.precision = precision
self.table_attributes = table_attributes

Expand Down Expand Up @@ -214,8 +232,12 @@ def _translate(self):
for c, col in enumerate(self.data.columns):
cs = [DATA_CLASS, "row%s" % r, "col%s" % c]
cs.extend(cell_context.get("data", {}).get(r, {}).get(c, []))
row_es.append({"type": "td", "value": self.data.iloc[r][c],
"class": " ".join(cs), "id": "_".join(cs[1:])})
row_d = {"type": "td", "value": self.data.iloc[r][c],
"class": " ".join(cs), "id": "_".join(cs[1:])}

if col in self.precision:
row_d['precision'] = self.precision[col]
row_es.append(row_d)
props = []
for x in ctx[r, c]:
# have to handle empty styles like ['']
Expand Down Expand Up @@ -399,7 +421,7 @@ def applymap(self, func, subset=None, **kwargs):
kwargs))
return self

def set_precision(self, precision):
def set_precision(self, precision=None, subsets={}):
"""
Set the precision used to render.

Expand All @@ -413,7 +435,20 @@ def set_precision(self, precision):
-------
self
"""
self.precision = precision

if not subsets and precision is None:
# reset everything
self.precision = {'__default__': pd.options.display.precision}
return self

if precision is None:
self.precision['__default__'] = pd.options.display.precision
elif isinstance(precision, numbers.Integral):
self.precision['__default__'] = precision

for k in subsets:
self.precision[k] = subsets[k]

return self

def set_table_attributes(self, attributes):
Expand Down