-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Fix repr of DataFrame with IntervalIndex #24134
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
Merged
jreback
merged 4 commits into
pandas-dev:master
from
jorisvandenbossche:interval-df-repr
Dec 13, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -840,7 +840,34 @@ def _get_column_name_list(self): | |
|
||
|
||
def format_array(values, formatter, float_format=None, na_rep='NaN', | ||
digits=None, space=None, justify='right', decimal='.'): | ||
digits=None, space=None, justify='right', decimal='.', | ||
leading_space=None): | ||
""" | ||
Format an array for printing. | ||
|
||
Parameters | ||
---------- | ||
values | ||
formatter | ||
float_format | ||
na_rep | ||
digits | ||
space | ||
justify | ||
decimal | ||
leading_space : bool, optional | ||
Whether the array should be formatted with a leading space. | ||
When an array as a column of a Series or DataFrame, we do want | ||
the leading space to pad between columns. | ||
|
||
When formatting an Index subclass | ||
(e.g. IntervalIndex._format_native_types), we don't want the | ||
leading space since it should be left-aligned. | ||
|
||
Returns | ||
------- | ||
List[str] | ||
""" | ||
|
||
if is_datetime64_dtype(values.dtype): | ||
fmt_klass = Datetime64Formatter | ||
|
@@ -868,7 +895,8 @@ def format_array(values, formatter, float_format=None, na_rep='NaN', | |
|
||
fmt_obj = fmt_klass(values, digits=digits, na_rep=na_rep, | ||
float_format=float_format, formatter=formatter, | ||
space=space, justify=justify, decimal=decimal) | ||
space=space, justify=justify, decimal=decimal, | ||
leading_space=leading_space) | ||
|
||
return fmt_obj.get_result() | ||
|
||
|
@@ -877,7 +905,7 @@ class GenericArrayFormatter(object): | |
|
||
def __init__(self, values, digits=7, formatter=None, na_rep='NaN', | ||
space=12, float_format=None, justify='right', decimal='.', | ||
quoting=None, fixed_width=True): | ||
quoting=None, fixed_width=True, leading_space=None): | ||
self.values = values | ||
self.digits = digits | ||
self.na_rep = na_rep | ||
|
@@ -888,6 +916,7 @@ def __init__(self, values, digits=7, formatter=None, na_rep='NaN', | |
self.decimal = decimal | ||
self.quoting = quoting | ||
self.fixed_width = fixed_width | ||
self.leading_space = leading_space | ||
|
||
def get_result(self): | ||
fmt_values = self._format_strings() | ||
|
@@ -927,7 +956,9 @@ def _format(x): | |
vals = vals.values | ||
|
||
is_float_type = lib.map_infer(vals, is_float) & notna(vals) | ||
leading_space = is_float_type.any() | ||
leading_space = self.leading_space | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you document when this is set. |
||
if leading_space is None: | ||
leading_space = is_float_type.any() | ||
|
||
fmt_values = [] | ||
for i, v in enumerate(vals): | ||
|
@@ -936,7 +967,13 @@ def _format(x): | |
elif is_float_type[i]: | ||
fmt_values.append(float_format(v)) | ||
else: | ||
fmt_values.append(u' {v}'.format(v=_format(v))) | ||
if leading_space is False: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is so hacky, we should not be doing this |
||
# False specifically, so that the default is | ||
# to include a space if we get here. | ||
tpl = u'{v}' | ||
else: | ||
tpl = u' {v}' | ||
fmt_values.append(tpl.format(v=_format(v))) | ||
|
||
return fmt_values | ||
|
||
|
@@ -1135,7 +1172,8 @@ def _format_strings(self): | |
formatter, | ||
float_format=self.float_format, | ||
na_rep=self.na_rep, digits=self.digits, | ||
space=self.space, justify=self.justify) | ||
space=self.space, justify=self.justify, | ||
leading_space=self.leading_space) | ||
return fmt_values | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why do we need this new arg? just change the output tests, which are incorrect
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.
In 0.23.4, we didn't have the leading space for indexes.
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.
wasn’t this the same issue you recently adjusted for DTi? this keywords just promote inconsistency
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 I don't think so.
AFAICT, this is happing because
IntervalIndex
andSeries[Interval]
are now both usingGenericArrayFormatter
to format the values. Series need a leading space, but indexes don't. So I think things should be more consistent. If you want I can remove the keyword and go back to the old implementation which just did the formatting on its own, but I suspect you don't want 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.
ok i guess