-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Removing unnecessary whitespace when formatting to a HTML table. #5012
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
@@ -734,7 +734,7 @@ def _write_body(self, indent): | |||
|
|||
fmt_values = {} | |||
for i in range(len(self.columns)): | |||
fmt_values[i] = self.fmt._format_col(i) | |||
fmt_values[i] = [item.strip() for item in self.fmt._format_col(i)] |
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 it possible to pass an argument to _format_col that controls this? e.g. in the HTMLFormatter it will not include the whitespace, all other formatters will be unchanged.
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.
@jreback It uses the format_array
function, which uses an appropriate ArrayFormatter
. They all call _make_fixed_width
which can justify either right
or left
. A third value can be added justify=None
, but then every ArrayFormatter
should be changed to support justify=None
. I can add that, no problem :), but it will be a lot of changes, and I didn't wanted to do that just for this small enhancement that could be fixed in one line. If you think that the more general solution is better, I'll do that.
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.
@jreback But maybe this isn't such a bad idea since then I can add justify=center
too. Currently only left
and right
are supported.
yep |
@jreback OK, I'll implement the additional justifications. |
It turned out easier than I thought. Hope I haven't missed something though... |
justfunc = lambda self, x: self.rjust(x) | ||
elif justify == 'center': | ||
justfunc = lambda self, x: self.center(x) |
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 these be of the form str.center
, str.rjust
etc?
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.
No, because it can also be a unicode
.
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.
sorry i meant compat.text_type.center
... or are we allowing str
and unicode
? if allowing both then disregard my suggestion
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.
That's just unicode
in python 2 and will throw an error if receives a str
:
>>> pd.compat.text_type.center('test', 30)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'center' requires a 'unicode' object but received a 'str'
And as I said in the previous comment, the same thing applies for the other way round:
>>> pd.compat.binary_type.center(u'test', 30)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: descriptor 'center' requires a 'str' object but received a 'unicode'
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.
okay forget it then :)
@alefnula can u add tests for the new justification options |
@cpcloud I changed the HTML tests so the None option is working... For the other, will something like this be enough? In [2]: df = pd.DataFrame({'foo': [1, 2, 3], 'bar': [4, 5, 6], 'baz': [7, 8, 9]})
In [3]: print(df.to_string(justify='left'))
bar baz foo
0 4 7 1
1 5 8 2
2 6 9 3
In [4]: print(df.to_string(justify='right'))
bar baz foo
0 4 7 1
1 5 8 2
2 6 9 3
In [5]: print(df.to_string(justify='center'))
bar baz foo
0 4 7 1
1 5 8 2
2 6 9 3 |
Although, maybe |
There was some really weird code here... The alignment of the columns was done twice (sometimes even 3 times, which is ridiculous - recursive calls...) I really couldn't find the reason why so I removed that. Also there is a bug in the current string column formatting. The column header is not aligned. The bug is also on the master branch. I'll try to fix it here if this patch can be accepted. Example of the bug: import numpy as np
import pandas as pd
ind = pd.date_range('2013-01-01', freq='H', periods=10)
df = pd.DataFrame(np.random.randn(10, 3), index=ind)
df = df.reset_index()
df['test'] = 'foo bar baz'
print(df.to_string(justify='left')) Produces:
You can see that the Finally, maybe |
@alefnula i would put the column alignment in a separate issue. |
@cpcloud The problem is that I don't even know if that's a bug or a feature :D That's why I asked. |
@cpcloud ok to merge? |
@alefnula relize no release notes...can you add? |
@jreback I don't think this should be merged yet. The |
you can just put justify = 'right' as the default which iirc was what it was |
That's fine with me :) I'll do it that way then. I should probably change the docs also. Just to add that the 'center' option is also available. |
@cpcloud this looks ok....so when you are ready can merge (as I believe you are waiting to merge something else) |
@jreback I just rebased it, didn't changed anything yet. :) There is one more problem with this (I'm really sorry to be such a hassle :( but I just don't want to break anything), docs:
But now if justify is None:
self.justify = get_option("display.colheader_justify") If it's ok to change the functionality to justify the whole columns I need a sentinel for
|
@cpcloud what do you think? |
@jreback In that case this whole pull request should be discarded since it justifies the whole columns... I could maybe separate the column and header justification and use |
do you want to defer this to 0.14....as templates hopefully will solve these types of things? |
defering this to 0.14 |
Stale, Please open an updated PR if the problems mentioned are sorted out. |
Closes #4987.
The problem is that all formatters always call
_make_fixed_width
. So, the only solution I found is just to strip the unnecessary whitespace.