-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
BUG: Fix float formatting when a string is passed as float_format arg #22308
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
Conversation
Fixes GH21625 and GH22270
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would need a whatsnew entry as well
@@ -1328,6 +1328,12 @@ def test_to_string_float_formatting(self): | |||
'1 2.512000e-01') | |||
assert df_s == expected | |||
|
|||
df = DataFrame({'x': [0.19999, 100.0]}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make a new test, pls add the issue number as a comment
@@ -974,6 +974,7 @@ def __init__(self, *args, **kwargs): | |||
# float_format is expected to be a string | |||
# formatter should be used to pass a function | |||
if self.float_format is not None and self.formatter is None: | |||
self.fixed_width = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you see if u can change fixed_width
to a cached property instead of setting it (need to remove from the signature as well)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a good chance I'm not understanding the comment, but I'm not sure we can set this as a cached property (using the cache_readonly
decorator?) since it is set in the base class. As I say I might be missing something...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can set it in each of the subclasses. I just don't think we actually need this. try doing this as a property first to see if it works.
can you add the test from #21625 as well |
Codecov Report
@@ Coverage Diff @@
## master #22308 +/- ##
==========================================
+ Coverage 92.29% 92.29% +<.01%
==========================================
Files 161 161
Lines 51497 51501 +4
==========================================
+ Hits 47530 47534 +4
Misses 3967 3967
Continue to review full report at Codecov.
|
doc/source/whatsnew/v0.24.0.txt
Outdated
- | ||
- :func:`DataFrame.to_string()`, :func:`DataFrame.to_html()` and | ||
:func:`DataFrame.to_latex()` will correctly format output when a string is | ||
passed as the ``float_format`` argument (:issue:`21625` and :issue:`22270`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use a ',' rather than 'and'
@@ -974,6 +974,7 @@ def __init__(self, *args, **kwargs): | |||
# float_format is expected to be a string | |||
# formatter should be used to pass a function | |||
if self.float_format is not None and self.formatter is None: | |||
self.fixed_width = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this possible?
can you rebase |
can you rebase and see if you can address comments above |
Sorry I am away at the moment. I can have a look at this next week. |
can you merge master and update to comments |
Hello @tomneep! Thanks for updating the PR.
Comment last updated on November 26, 2018 at 12:27 Hours UTC |
@@ -1359,6 +1359,18 @@ def test_to_string_float_formatting(self): | |||
'1 2.512000e-01') | |||
assert df_s == expected | |||
|
|||
def test_to_string_float_format_no_fixed_width(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you would need to add tests for to_latex and to_html in the appropriate files
Changes above are just merging with the current master. With fresh eyes I agree this probably isn't the best way to solve this but I'm a bit stuck how better to proceed. grepping the code I see that A slightly different way of doing essentially the same thing (but without touching the Any suggestions would be welcome. |
the way the formatters is done now is not super flexible, so your change is ok. if you can add tests for latex & html would be good. |
@@ -0,0 +1,14 @@ | |||
<table border="1" class="dataframe"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't have a very strong opinion atm but wondering if we want dedicated files for tests this small. Could maybe include HTML as part of the test or alternately just test for the existence of the appropriate format in the result or the expression.
This doesn't need to be addressed in this PR but just bringing up as a general discussion point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
thanks! |
closes #21625
closes #22270.
If a string
float_format
argument is passed by the user toto_string
,to_latex
,to_html
, etc, then disable fixed width, so that now e.g.will give the same result as
git diff upstream/master -u -- "*.py" | flake8 --diff