Skip to content

to_html formatter not called for float values in a mixed-type column (2) #26000

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
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
71e8b31
to_html formatter not called for float values in a mixed-type column
simonjayhawkins Apr 3, 2019
2a2bb57
changes to test as requested
simonjayhawkins Apr 4, 2019
1e5615b
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Apr 4, 2019
4ca48a1
Merge branch 'master' into GenericArrayFormatter
simonjayhawkins Apr 4, 2019
4ef3149
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Apr 5, 2019
d7a8510
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Apr 5, 2019
4b60e4b
shortcut format_array
simonjayhawkins Apr 5, 2019
5ac441b
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Apr 5, 2019
8a64459
add shortcut parameter to format_array
simonjayhawkins Apr 5, 2019
9c1354c
add whatsnew for #26002
simonjayhawkins Apr 5, 2019
d0df1d6
remove shortcut parameter from format_array
simonjayhawkins Apr 6, 2019
c74b0aa
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Apr 6, 2019
4262113
remove whatsnew for #26002
simonjayhawkins Apr 6, 2019
f0cf9b7
defer to GenericArrayFormatter for IntervalArray
simonjayhawkins Apr 7, 2019
d6bee41
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Jun 13, 2019
1c535a1
Merge remote-tracking branch 'upstream/master' into GenericArrayForma…
simonjayhawkins Jun 18, 2019
5ecf91a
pre-format instead of shortcut
simonjayhawkins Jun 18, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove shortcut parameter from format_array
  • Loading branch information
simonjayhawkins committed Apr 6, 2019
commit d0df1d63d9596bcedf13a2bd820152b9eb1391fe
1 change: 1 addition & 0 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ def _format_native_types(self, na_rep='NaN', quoting=None, **kwargs):
""" actually format my specific types """
from pandas.io.formats.format import ExtensionArrayFormatter
return ExtensionArrayFormatter(values=self,
formatter=False,
na_rep=na_rep,
justify='all',
leading_space=False).get_result()
Expand Down
24 changes: 10 additions & 14 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,11 +717,9 @@ def _format_col(self, i):
frame = self.tr_frame
formatter = self._get_formatter(i)
values_to_format = frame.iloc[:, i]._formatting_values()
shortcut = formatter is not None
return format_array(values_to_format, formatter,
float_format=self.float_format, na_rep=self.na_rep,
space=self.col_space, decimal=self.decimal,
shortcut=shortcut)
space=self.col_space, decimal=self.decimal)

def to_html(self, classes=None, notebook=False, border=None):
"""
Expand Down Expand Up @@ -858,7 +856,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, shortcut=False):
leading_space=None):
"""
Format an array for printing.

Expand All @@ -880,9 +878,6 @@ def format_array(values, formatter, float_format=None, na_rep='NaN',
When formatting an Index subclass
(e.g. IntervalIndex._format_native_types), we don't want the
leading space since it should be left-aligned.
shortcut : bool, optional, default False
Whether to shortcut the formatting options. Used when specifying
custom formatters in to_string, to_latex and to_html

Returns
-------
Expand Down Expand Up @@ -916,7 +911,7 @@ 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,
leading_space=leading_space, shortcut=shortcut)
leading_space=leading_space)

return fmt_obj.get_result()

Expand All @@ -925,8 +920,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, leading_space=None,
shortcut=False):
quoting=None, fixed_width=True, leading_space=None):
self.values = values
self.digits = digits
self.na_rep = na_rep
Expand All @@ -938,16 +932,15 @@ def __init__(self, values, digits=7, formatter=None, na_rep='NaN',
self.quoting = quoting
self.fixed_width = fixed_width
self.leading_space = leading_space
self.shortcut = shortcut

def get_result(self):
fmt_values = self._format_strings()
return _make_fixed_width(fmt_values, self.justify)

def _format_strings(self):
# shortcut
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you give some more explanation here

if self.formatter is not None and self.shortcut:
return [self.formatter(x) for x in self.values]
if self.formatter is not None:
return [' {}'.format(self.formatter(x)) for x in self.values]

if self.float_format is None:
float_format = get_option("display.float_format")
Expand Down Expand Up @@ -1199,7 +1192,10 @@ def _format_strings(self):
if isinstance(values, (ABCIndexClass, ABCSeries)):
values = values._values

formatter = values._formatter(boxed=True)
if self.formatter is None:
formatter = values._formatter(boxed=True)
elif self.formatter is False:
formatter = None

if is_categorical_dtype(values.dtype):
# Categorical is special for now, so that we can preserve tzinfo
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/io/formats/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,10 +511,10 @@ def test_to_string_with_formatters(self):
('object', lambda x: '-{x!s}-'.format(x=x))]
result = df.to_string(formatters=dict(formatters))
result2 = df.to_string(formatters=lzip(*formatters)[1])
assert result == (' int float object\n'
'0 0x1 [ 1.0] -(1, 2)-\n'
'1 0x2 [ 2.0] -True-\n'
'2 0x3 [ 3.0] -False-')
assert result == (' int float object\n'
'0 0x1 [ 1.0] -(1, 2)-\n'
'1 0x2 [ 2.0] -True-\n'
'2 0x3 [ 3.0] -False-')
assert result == result2

def test_to_string_with_datetime64_monthformatter(self):
Expand Down
8 changes: 4 additions & 4 deletions pandas/tests/io/formats/test_to_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,11 @@ def test_to_latex_with_formatters(self):

expected = r"""\begin{tabular}{llrrl}
\toprule
{} & datetime64 & float & int & object \\
{} & datetime64 & float & int & object \\
\midrule
index: 0 & 2016-01 & [ 1.0] & 0x1 & -(1, 2)- \\
index: 1 & 2016-02 & [ 2.0] & 0x2 & -True- \\
index: 2 & 2016-03 & [ 3.0] & 0x3 & -False- \\
index: 0 & 2016-01 & [ 1.0] & 0x1 & -(1, 2)- \\
index: 1 & 2016-02 & [ 2.0] & 0x2 & -True- \\
index: 2 & 2016-03 & [ 3.0] & 0x3 & -False- \\
\bottomrule
\end{tabular}
"""
Expand Down