Skip to content

BUG: fix precision to specify decimal places #13897

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 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/formats/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(self, data, precision=None, table_styles=None, uuid=None,

def default_display_func(x):
if is_float(x):
return '{:>.{precision}g}'.format(x, precision=self.precision)
return '{:>.{precision}f}'.format(x, precision=self.precision)
else:
return x

Expand Down
16 changes: 8 additions & 8 deletions pandas/tests/formats/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,37 +507,37 @@ def test_display_subset(self):
ctx = df.style.format({"a": "{:0.1f}", "b": "{0:.2%}"},
subset=pd.IndexSlice[0, :])._translate()
expected = '0.1'
precise_11 = '1.123400' # with the default precision
Copy link
Member

Choose a reason for hiding this comment

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

pls also test changing the precision from default.

self.assertEqual(ctx['body'][0][1]['display_value'], expected)
self.assertEqual(ctx['body'][1][1]['display_value'], '1.1234')
self.assertEqual(ctx['body'][1][1]['display_value'], precise_11)
self.assertEqual(ctx['body'][0][2]['display_value'], '12.34%')

raw_11 = '1.1234'
ctx = df.style.format("{:0.1f}",
subset=pd.IndexSlice[0, :])._translate()
self.assertEqual(ctx['body'][0][1]['display_value'], expected)
self.assertEqual(ctx['body'][1][1]['display_value'], raw_11)
self.assertEqual(ctx['body'][1][1]['display_value'], precise_11)

ctx = df.style.format("{:0.1f}",
subset=pd.IndexSlice[0, :])._translate()
self.assertEqual(ctx['body'][0][1]['display_value'], expected)
self.assertEqual(ctx['body'][1][1]['display_value'], raw_11)
self.assertEqual(ctx['body'][1][1]['display_value'], precise_11)

ctx = df.style.format("{:0.1f}",
subset=pd.IndexSlice['a'])._translate()
self.assertEqual(ctx['body'][0][1]['display_value'], expected)
self.assertEqual(ctx['body'][0][2]['display_value'], '0.1234')
self.assertEqual(ctx['body'][0][2]['display_value'], '0.123400')

ctx = df.style.format("{:0.1f}",
subset=pd.IndexSlice[0, 'a'])._translate()
self.assertEqual(ctx['body'][0][1]['display_value'], expected)
self.assertEqual(ctx['body'][1][1]['display_value'], raw_11)
self.assertEqual(ctx['body'][1][1]['display_value'], precise_11)

ctx = df.style.format("{:0.1f}",
subset=pd.IndexSlice[[0, 1], ['a']])._translate()
self.assertEqual(ctx['body'][0][1]['display_value'], expected)
self.assertEqual(ctx['body'][1][1]['display_value'], '1.1')
self.assertEqual(ctx['body'][0][2]['display_value'], '0.1234')
self.assertEqual(ctx['body'][1][2]['display_value'], '1.1234')
self.assertEqual(ctx['body'][0][2]['display_value'], '0.123400')
self.assertEqual(ctx['body'][1][2]['display_value'], '1.123400')

def test_display_dict(self):
df = pd.DataFrame([[.1234, .1234], [1.1234, 1.1234]],
Expand Down