Skip to content

Commit 81694dc

Browse files
authored
BUG: Validate the justify parameter in to_html (pandas-dev#17766)
BUG: Validate the justify parameter in to_html Closes pandas-devgh-17527.
1 parent 69024a0 commit 81694dc

File tree

4 files changed

+28
-39
lines changed

4 files changed

+28
-39
lines changed

doc/source/whatsnew/v0.21.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,8 @@ I/O
777777
- Bug in :func:`read_stata` where the index was not set (:issue:`16342`)
778778
- Bug in :func:`read_html` where import check fails when run in multiple threads (:issue:`16928`)
779779
- 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`)
780-
- 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`)
780+
- 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`)
781+
- Bug in :meth:`DataFrame.to_html` in which there was no validation of the ``justify`` parameter (:issue:`17527`)
781782
- Bug in :func:`HDFStore.select` when reading a contiguous mixed-data table featuring VLArray (:issue:`17021`)
782783

783784
Plotting

pandas/core/frame.py

+4
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,10 @@ def to_html(self, buf=None, columns=None, col_space=None, header=True,
16851685
.. versionadded:: 0.19.0
16861686
"""
16871687

1688+
if (justify is not None and
1689+
justify not in fmt._VALID_JUSTIFY_PARAMETERS):
1690+
raise ValueError("Invalid value for justify parameter")
1691+
16881692
formatter = fmt.DataFrameFormatter(self, buf=buf, columns=columns,
16891693
col_space=col_space, na_rep=na_rep,
16901694
formatters=formatters,

pandas/io/formats/format.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,15 @@
7979
line_width : int, optional
8080
Width to wrap a line in characters, default no wrap"""
8181

82+
_VALID_JUSTIFY_PARAMETERS = ("left", "right", "center", "justify",
83+
"justify-all", "start", "end", "inherit",
84+
"match-parent", "initial", "unset")
85+
8286
justify_docstring = """
83-
justify : {'left', 'right'}, default None
84-
Left or right-justify the column labels. If None uses the option from
87+
justify : {'left', 'right', 'center', 'justify',
88+
'justify-all', 'start', 'end', 'inherit',
89+
'match-parent', 'initial', 'unset'}, default None
90+
How to justify the column labels. If None uses the option from
8591
the print configuration (controlled by set_option), 'right' out
8692
of the box."""
8793

pandas/tests/io/formats/test_to_html.py

+14-36
Original file line numberDiff line numberDiff line change
@@ -1557,15 +1557,16 @@ def test_to_html_multiindex(self):
15571557

15581558
assert result == expected
15591559

1560-
def test_to_html_justify(self):
1560+
@pytest.mark.parametrize("justify", fmt._VALID_JUSTIFY_PARAMETERS)
1561+
def test_to_html_justify(self, justify):
15611562
df = DataFrame({'A': [6, 30000, 2],
15621563
'B': [1, 2, 70000],
15631564
'C': [223442, 0, 1]},
15641565
columns=['A', 'B', 'C'])
1565-
result = df.to_html(justify='left')
1566+
result = df.to_html(justify=justify)
15661567
expected = ('<table border="1" class="dataframe">\n'
15671568
' <thead>\n'
1568-
' <tr style="text-align: left;">\n'
1569+
' <tr style="text-align: {justify};">\n'
15691570
' <th></th>\n'
15701571
' <th>A</th>\n'
15711572
' <th>B</th>\n'
@@ -1592,41 +1593,18 @@ def test_to_html_justify(self):
15921593
' <td>1</td>\n'
15931594
' </tr>\n'
15941595
' </tbody>\n'
1595-
'</table>')
1596+
'</table>'.format(justify=justify))
15961597
assert result == expected
15971598

1598-
result = df.to_html(justify='right')
1599-
expected = ('<table border="1" class="dataframe">\n'
1600-
' <thead>\n'
1601-
' <tr style="text-align: right;">\n'
1602-
' <th></th>\n'
1603-
' <th>A</th>\n'
1604-
' <th>B</th>\n'
1605-
' <th>C</th>\n'
1606-
' </tr>\n'
1607-
' </thead>\n'
1608-
' <tbody>\n'
1609-
' <tr>\n'
1610-
' <th>0</th>\n'
1611-
' <td>6</td>\n'
1612-
' <td>1</td>\n'
1613-
' <td>223442</td>\n'
1614-
' </tr>\n'
1615-
' <tr>\n'
1616-
' <th>1</th>\n'
1617-
' <td>30000</td>\n'
1618-
' <td>2</td>\n'
1619-
' <td>0</td>\n'
1620-
' </tr>\n'
1621-
' <tr>\n'
1622-
' <th>2</th>\n'
1623-
' <td>2</td>\n'
1624-
' <td>70000</td>\n'
1625-
' <td>1</td>\n'
1626-
' </tr>\n'
1627-
' </tbody>\n'
1628-
'</table>')
1629-
assert result == expected
1599+
@pytest.mark.parametrize("justify", ["super-right", "small-left",
1600+
"noinherit", "tiny", "pandas"])
1601+
def test_to_html_invalid_justify(self, justify):
1602+
# see gh-17527
1603+
df = DataFrame()
1604+
msg = "Invalid value for justify parameter"
1605+
1606+
with tm.assert_raises_regex(ValueError, msg):
1607+
df.to_html(justify=justify)
16301608

16311609
def test_to_html_index(self):
16321610
index = ['foo', 'bar', 'baz']

0 commit comments

Comments
 (0)