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

Conversation

drewrey
Copy link
Contributor

@drewrey drewrey commented Aug 4, 2016

Use 'f' as a string conversion type to specify the number of decimals to
represent. The 'g' format will instead fall back to the 'e' format
causing the unexpected behavior seen in GHI13257.

See: old-string-formatting

Replicated code from the issue, with output:
example output with precision

closes GHI13257

Use 'f' as a string conversion type to specify the number of decimals to
represent. The 'g' format will instead fall back to the 'e' format
causing the unexpected behavior seen in GHI13257.

See:
https://docs.python.org/3.5/library/stdtypes.html#old-string-formatting
@@ -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.

@sinhrks sinhrks added Bug Output-Formatting __repr__ of pandas objects, to_string labels Aug 4, 2016
@sinhrks
Copy link
Member

sinhrks commented Aug 4, 2016

Also, add tests with Float64Index like:

df = pd.DataFrame(data = np.random.randn(4,4),
                  index=[11.1, 22.2, 33.3, 44.4]
                  columns=[1.1111, 2.2222, 3.3333, 4.4444])

@codecov-io
Copy link

Current coverage is 85.28% (diff: 100%)

Merging #13897 into master will decrease coverage by <.01%

@@             master     #13897   diff @@
==========================================
  Files           139        139          
  Lines         50057      50057          
  Methods           0          0          
  Messages          0          0          
  Branches          0          0          
==========================================
- Hits          42692      42691     -1   
- Misses         7365       7366     +1   
  Partials          0          0          

Powered by Codecov. Last update 45d54d0...0731bcb

@jreback
Copy link
Contributor

jreback commented Aug 4, 2016

@TomAugspurger ?

@drewrey pls add a whatsnew note (bug fix section)

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Aug 4, 2016

Changing it to g was intentional to avoid things like

In [1]: "{:f}".format(1)
Out[1]: '1.000000'

and instead have that formatted as 1. I glanced quickly and didn't see any tests for that :/

Could you add a test with an integer and a float column like 1.0, use the default precision / formatter, and verify that the display value is 1, and then make sure your change passes that test?

@TomAugspurger
Copy link
Contributor

TomAugspurger commented Aug 4, 2016

Looking further, it seems like we have a bug in master.

m = pd.DataFrame({"a": [1], "b": [1.]})

m.style._translate()['body'][0][1]['display_value'] # should be '1', is correct

m.style._translate()['body'][0][2]['display_value'] # should be '1.0', currently is '1', is incorrect

Ideally df.style would have the same output as df._repr_html() by default.

@jreback
Copy link
Contributor

jreback commented Sep 9, 2016

can you rebase / update?

@jreback
Copy link
Contributor

jreback commented Nov 16, 2016

can you add a whatnew note in 0.19.2

@TomAugspurger ?

@jreback
Copy link
Contributor

jreback commented Dec 26, 2016

closing, but if you'd like to update, pls do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Output-Formatting __repr__ of pandas objects, to_string
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Pandas style.set_precision not working as expected
5 participants