Skip to content

BUG: Validate the justify parameter in to_html #17766

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
merged 4 commits into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v0.21.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,8 @@ I/O
- Bug in :func:`read_stata` where the index was not set (:issue:`16342`)
- Bug in :func:`read_html` where import check fails when run in multiple threads (:issue:`16928`)
- Bug in :func:`read_csv` where automatic delimiter detection caused a ``TypeError`` to be thrown when a bad line was encountered rather than the correct error message (:issue:`13374`)
- Bug in ``DataFrame.to_html()`` with ``notebook=True`` where DataFrames with named indices or non-MultiIndex indices had undesired horizontal or vertical alignment for column or row labels, respectively (:issue:`16792`)
- Bug in :meth:`DataFrame.to_html` with ``notebook=True`` where DataFrames with named indices or non-MultiIndex indices had undesired horizontal or vertical alignment for column or row labels, respectively (:issue:`16792`)
- Bug in :meth:`DataFrame.to_html` in which there was no validation of the ``justify`` parameter (:issue:`17527`)
- Bug in :func:`HDFStore.select` when reading a contiguous mixed-data table featuring VLArray (:issue:`17021`)

Plotting
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,10 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True,
.. versionadded:: 0.19.0
"""

if (justify is not None and
justify not in fmt._VALID_JUSTIFY_PARAMETERS):
raise ValueError("Invalid value for justify parameter")

formatter = fmt.DataFrameFormatter(self, buf=buf, columns=columns,
col_space=col_space, na_rep=na_rep,
formatters=formatters,
Expand Down
10 changes: 8 additions & 2 deletions pandas/io/formats/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,15 @@
line_width : int, optional
Width to wrap a line in characters, default no wrap"""

_VALID_JUSTIFY_PARAMETERS = ("left", "right", "center", "justify",
"justify-all", "start", "end", "inherit",
"match-parent", "initial", "unset")

justify_docstring = """
justify : {'left', 'right'}, default None
Left or right-justify the column labels. If None uses the option from
justify : {'left', 'right', 'center', 'justify',
'justify-all', 'start', 'end', 'inherit',
'match-parent', 'initial', 'unset'}, default None
How to justify the column labels. If None uses the option from
the print configuration (controlled by set_option), 'right' out
of the box."""

Expand Down
50 changes: 14 additions & 36 deletions pandas/tests/io/formats/test_to_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -1557,15 +1557,16 @@ def test_to_html_multiindex(self):

assert result == expected

def test_to_html_justify(self):
@pytest.mark.parametrize("justify", fmt._VALID_JUSTIFY_PARAMETERS)
def test_to_html_justify(self, justify):
df = DataFrame({'A': [6, 30000, 2],
'B': [1, 2, 70000],
'C': [223442, 0, 1]},
columns=['A', 'B', 'C'])
result = df.to_html(justify='left')
result = df.to_html(justify=justify)
expected = ('<table border="1" class="dataframe">\n'
' <thead>\n'
' <tr style="text-align: left;">\n'
' <tr style="text-align: {justify};">\n'
' <th></th>\n'
' <th>A</th>\n'
' <th>B</th>\n'
Expand All @@ -1592,41 +1593,18 @@ def test_to_html_justify(self):
' <td>1</td>\n'
' </tr>\n'
' </tbody>\n'
'</table>')
'</table>'.format(justify=justify))
assert result == expected

result = df.to_html(justify='right')
expected = ('<table border="1" class="dataframe">\n'
' <thead>\n'
' <tr style="text-align: right;">\n'
' <th></th>\n'
' <th>A</th>\n'
' <th>B</th>\n'
' <th>C</th>\n'
' </tr>\n'
' </thead>\n'
' <tbody>\n'
' <tr>\n'
' <th>0</th>\n'
' <td>6</td>\n'
' <td>1</td>\n'
' <td>223442</td>\n'
' </tr>\n'
' <tr>\n'
' <th>1</th>\n'
' <td>30000</td>\n'
' <td>2</td>\n'
' <td>0</td>\n'
' </tr>\n'
' <tr>\n'
' <th>2</th>\n'
' <td>2</td>\n'
' <td>70000</td>\n'
' <td>1</td>\n'
' </tr>\n'
' </tbody>\n'
'</table>')
assert result == expected
@pytest.mark.parametrize("justify", ["super-right", "small-left",
"noinherit", "tiny", "pandas"])
def test_to_html_invalid_justify(self, justify):
# see gh-17527
df = DataFrame()
msg = "Invalid value for justify parameter"

with tm.assert_raises_regex(ValueError, msg):
df.to_html(justify=justify)

def test_to_html_index(self):
index = ['foo', 'bar', 'baz']
Expand Down