-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: on .to_string(index=False) #25000
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,8 +247,15 @@ def _get_formatted_index(self): | |
|
||
def _get_formatted_values(self): | ||
values_to_format = self.tr_series._formatting_values() | ||
|
||
if self.index: | ||
leading_space = 'compat' | ||
else: | ||
leading_space = 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. is this really the idiom? These are both False values, so this is extra confusing. 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. shouldn't this just be 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. somehow it looks like there is a slight difference between if leading_space is False:
# False specifically, so that the default is
# to include a space if we get here.
tpl = u'{v}'
else:
tpl = u' {v}' 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. not sure who added this. It is just ripe for errors. Are there really 3 cases or just 2? If there are 3, then the api for leading_space should be a string that is checked and much more readable. 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. should have 2 cases... 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.
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. then let's make this more explicit and use a str argname then like 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. @charlesdong1991 can you fix this up |
||
return format_array(values_to_format, None, | ||
float_format=self.float_format, na_rep=self.na_rep) | ||
float_format=self.float_format, | ||
na_rep=self.na_rep, | ||
leading_space=leading_space) | ||
|
||
def to_string(self): | ||
series = self.tr_series | ||
|
@@ -712,9 +719,15 @@ def _format_col(self, i): | |
frame = self.tr_frame | ||
formatter = self._get_formatter(i) | ||
values_to_format = frame.iloc[:, i]._formatting_values() | ||
|
||
if self.index: | ||
leading_space = 'compat' | ||
else: | ||
leading_space = False | ||
return format_array(values_to_format, formatter, | ||
float_format=self.float_format, na_rep=self.na_rep, | ||
space=self.col_space, decimal=self.decimal) | ||
space=self.col_space, decimal=self.decimal, | ||
leading_space=leading_space) | ||
|
||
def to_html(self, classes=None, notebook=False, border=None): | ||
""" | ||
|
@@ -851,7 +864,7 @@ def _get_column_name_list(self): | |
|
||
def format_array(values, formatter, float_format=None, na_rep='NaN', | ||
digits=None, space=None, justify='right', decimal='.', | ||
leading_space=None): | ||
leading_space='compat'): | ||
""" | ||
Format an array for printing. | ||
|
||
|
@@ -865,7 +878,7 @@ def format_array(values, formatter, float_format=None, na_rep='NaN', | |
space | ||
justify | ||
decimal | ||
leading_space : bool, optional | ||
leading_space : bool, default is 'compat' | ||
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. | ||
|
@@ -915,7 +928,7 @@ class GenericArrayFormatter: | |
|
||
def __init__(self, values, digits=7, formatter=None, na_rep='NaN', | ||
space=12, float_format=None, justify='right', decimal='.', | ||
quoting=None, fixed_width=True, leading_space=None): | ||
quoting=None, fixed_width=True, leading_space='compat'): | ||
self.values = values | ||
self.digits = digits | ||
self.na_rep = na_rep | ||
|
@@ -973,7 +986,7 @@ def _format(x): | |
|
||
is_float_type = lib.map_infer(vals, is_float) & notna(vals) | ||
leading_space = self.leading_space | ||
if leading_space is None: | ||
if leading_space == 'compat': | ||
leading_space = is_float_type.any() | ||
|
||
fmt_values = [] | ||
|
@@ -1101,7 +1114,11 @@ def format_values_with(float_format): | |
# The default is otherwise to use str instead of a formatting string | ||
if self.float_format is None: | ||
if self.fixed_width: | ||
float_format = partial('{value: .{digits:d}f}'.format, | ||
if self.leading_space is not False: | ||
fmt_str = '{value: .{digits:d}f}' | ||
else: | ||
fmt_str = '{value:.{digits:d}f}' | ||
float_format = partial(fmt_str.format, | ||
digits=self.digits) | ||
else: | ||
float_format = self.float_format | ||
|
@@ -1133,7 +1150,11 @@ def format_values_with(float_format): | |
(abs_vals > 0)).any() | ||
|
||
if has_small_values or (too_long and has_large_values): | ||
float_format = partial('{value: .{digits:d}e}'.format, | ||
if self.leading_space is not False: | ||
fmt_str = '{value: .{digits:d}e}' | ||
else: | ||
fmt_str = '{value:.{digits:d}e}' | ||
float_format = partial(fmt_str.format, | ||
digits=self.digits) | ||
formatted_values = format_values_with(float_format) | ||
|
||
|
@@ -1150,7 +1171,12 @@ def _format_strings(self): | |
class IntArrayFormatter(GenericArrayFormatter): | ||
|
||
def _format_strings(self): | ||
formatter = self.formatter or (lambda x: '{x: d}'.format(x=x)) | ||
if self.leading_space is False: | ||
fmt_str = '{x:d}' | ||
else: | ||
fmt_str = '{x: d}' | ||
formatter = self.formatter or (lambda x: fmt_str.format(x=x)) | ||
# formatter = self.formatter or (lambda x: '{x: d}'.format(x=x)) | ||
fmt_values = [formatter(x) for x in self.values] | ||
return fmt_values | ||
|
||
|
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.
These test changes are concerning. Is this reverting an API change between 0.23.4 and 0.24.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.
i think this is the leading space that is wrongly added due to this bug