Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

ENH: add option chop_threshold to control display of small numerical values as zero #2739

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+37 −2
Split
@@ -120,6 +120,12 @@
Default 80
When printing wide DataFrames, this is the width of each line.
"""
+pc_chop_threshold_doc = """
+: float or None
+ Default None
+ if set to a float value, all float values smaller then the given threshold
+ will be displayed as exactly 0 by repr and friends.
+"""
with cf.config_prefix('display'):
cf.register_option('precision', 7, pc_precision_doc, validator=is_int)
@@ -146,6 +152,7 @@
validator=is_text)
cf.register_option('expand_frame_repr', True, pc_expand_repr_doc)
cf.register_option('line_width', 80, pc_line_width_doc)
+ cf.register_option('chop_threshold', None, pc_chop_threshold_doc)
tc_sim_interactive_doc = """
: boolean
View
@@ -1091,8 +1091,21 @@ def __init__(self, *args, **kwargs):
self.formatter = self.float_format
def _format_with(self, fmt_str):
- fmt_values = [fmt_str % x if notnull(x) else self.na_rep
- for x in self.values]
+ def _val(x, threshold):
+ if notnull(x):
+ if threshold is None or abs(x) > get_option("display.chop_threshold"):
+ return fmt_str % x
+ else:
+ if fmt_str.endswith("e"): # engineering format
+ return "0"
+ else:
+ return fmt_str % 0
+ else:
+
+ return self.na_rep
+
+ threshold = get_option("display.chop_threshold")
+ fmt_values = [ _val(x, threshold) for x in self.values]
return _trim_zeros(fmt_values, self.na_rep)
def get_result(self):
@@ -100,6 +100,21 @@ def test_repr_truncation(self):
with option_context("display.max_colwidth", max_len + 2):
self.assert_('...' not in repr(df))
+ def test_repr_chop_threshold(self):
+ df = DataFrame([[0.1, 0.5],[0.5, -0.1]])
+ pd.reset_option("display.chop_threshold") # default None
+ self.assertEqual(repr(df), ' 0 1\n0 0.1 0.5\n1 0.5 -0.1')
+
+ with option_context("display.chop_threshold", 0.2 ):
+ self.assertEqual(repr(df), ' 0 1\n0 0.0 0.5\n1 0.5 0.0')
+
+ with option_context("display.chop_threshold", 0.6 ):
+ self.assertEqual(repr(df), ' 0 1\n0 0 0\n1 0 0')
+
+ with option_context("display.chop_threshold", None ):
+ self.assertEqual(repr(df), ' 0 1\n0 0.1 0.5\n1 0.5 -0.1')
+
+
def test_repr_should_return_str(self):
"""
http://docs.python.org/py3k/reference/datamodel.html#object.__repr__