Skip to content

Commit e356f98

Browse files
y-pwesm
authored andcommitted
ENH: add option chop_threshold to control display of small numerical values as zero GH2735
Doesn't cover complex numbers for now.
1 parent bb13ff4 commit e356f98

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

pandas/core/config_init.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@
120120
Default 80
121121
When printing wide DataFrames, this is the width of each line.
122122
"""
123+
pc_chop_threshold_doc = """
124+
: float or None
125+
Default None
126+
if set to a float value, all float values smaller then the given threshold
127+
will be displayed as exactly 0 by repr and friends.
128+
"""
123129

124130
with cf.config_prefix('display'):
125131
cf.register_option('precision', 7, pc_precision_doc, validator=is_int)
@@ -146,6 +152,7 @@
146152
validator=is_text)
147153
cf.register_option('expand_frame_repr', True, pc_expand_repr_doc)
148154
cf.register_option('line_width', 80, pc_line_width_doc)
155+
cf.register_option('chop_threshold', None, pc_chop_threshold_doc)
149156

150157
tc_sim_interactive_doc = """
151158
: boolean

pandas/core/format.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,8 +1098,21 @@ def __init__(self, *args, **kwargs):
10981098
self.formatter = self.float_format
10991099

11001100
def _format_with(self, fmt_str):
1101-
fmt_values = [fmt_str % x if notnull(x) else self.na_rep
1102-
for x in self.values]
1101+
def _val(x, threshold):
1102+
if notnull(x):
1103+
if threshold is None or abs(x) > get_option("display.chop_threshold"):
1104+
return fmt_str % x
1105+
else:
1106+
if fmt_str.endswith("e"): # engineering format
1107+
return "0"
1108+
else:
1109+
return fmt_str % 0
1110+
else:
1111+
1112+
return self.na_rep
1113+
1114+
threshold = get_option("display.chop_threshold")
1115+
fmt_values = [ _val(x, threshold) for x in self.values]
11031116
return _trim_zeros(fmt_values, self.na_rep)
11041117

11051118
def get_result(self):

pandas/tests/test_format.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ def test_repr_truncation(self):
100100
with option_context("display.max_colwidth", max_len + 2):
101101
self.assert_('...' not in repr(df))
102102

103+
def test_repr_chop_threshold(self):
104+
df = DataFrame([[0.1, 0.5],[0.5, -0.1]])
105+
pd.reset_option("display.chop_threshold") # default None
106+
self.assertEqual(repr(df), ' 0 1\n0 0.1 0.5\n1 0.5 -0.1')
107+
108+
with option_context("display.chop_threshold", 0.2 ):
109+
self.assertEqual(repr(df), ' 0 1\n0 0.0 0.5\n1 0.5 0.0')
110+
111+
with option_context("display.chop_threshold", 0.6 ):
112+
self.assertEqual(repr(df), ' 0 1\n0 0 0\n1 0 0')
113+
114+
with option_context("display.chop_threshold", None ):
115+
self.assertEqual(repr(df), ' 0 1\n0 0.1 0.5\n1 0.5 -0.1')
116+
117+
103118
def test_repr_should_return_str(self):
104119
"""
105120
http://docs.python.org/py3k/reference/datamodel.html#object.__repr__

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy