diff --git a/pandas/tests/io/formats/data/datetime64_hourformatter.html b/pandas/tests/io/formats/data/datetime64_hourformatter.html new file mode 100644 index 00000000000000..c92b7218eba767 --- /dev/null +++ b/pandas/tests/io/formats/data/datetime64_hourformatter.html @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + +
hod
010:10
112:12
diff --git a/pandas/tests/io/formats/data/datetime64_monthformatter.html b/pandas/tests/io/formats/data/datetime64_monthformatter.html new file mode 100644 index 00000000000000..589c8fba858a5f --- /dev/null +++ b/pandas/tests/io/formats/data/datetime64_monthformatter.html @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + +
months
02016-01
12016-02
diff --git a/pandas/tests/io/formats/data/escape_disabled.html b/pandas/tests/io/formats/data/escape_disabled.html new file mode 100644 index 00000000000000..260a04d26108b8 --- /dev/null +++ b/pandas/tests/io/formats/data/escape_disabled.html @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + +
co + co>l2
str + boldbold
stri>ng2 &boldbold
diff --git a/pandas/tests/io/formats/data/escaped.html b/pandas/tests/io/formats/data/escaped.html new file mode 100644 index 00000000000000..d68bdd3df79c48 --- /dev/null +++ b/pandas/tests/io/formats/data/escaped.html @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + +
co<l1co>l2
str<ing1 &amp;<type 'str'><type 'str'>
stri>ng2 &amp;<type 'str'><type 'str'>
diff --git a/pandas/tests/io/formats/data/gh12031_expected_output.html b/pandas/tests/io/formats/data/gh12031_expected_output.html new file mode 100644 index 00000000000000..896e154a2b3242 --- /dev/null +++ b/pandas/tests/io/formats/data/gh12031_expected_output.html @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + +
A
06,0
13,1
22,2
diff --git a/pandas/tests/io/formats/data/gh14882_expected_output_1.html b/pandas/tests/io/formats/data/gh14882_expected_output_1.html new file mode 100644 index 00000000000000..4cfd8785de8257 --- /dev/null +++ b/pandas/tests/io/formats/data/gh14882_expected_output_1.html @@ -0,0 +1,274 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
n
abc
1001010
21
32
43
54
65
76
2017
28
39
410
511
612
713
30114
215
316
417
518
619
720
20010121
222
323
424
525
626
727
20128
229
......
633
734
30135
236
337
438
539
640
741
30010142
243
344
445
546
647
748
20149
250
351
452
553
654
755
30156
257
358
459
560
661
762
diff --git a/pandas/tests/io/formats/data/gh14882_expected_output_2.html b/pandas/tests/io/formats/data/gh14882_expected_output_2.html new file mode 100644 index 00000000000000..d4e7fd9bd81350 --- /dev/null +++ b/pandas/tests/io/formats/data/gh14882_expected_output_2.html @@ -0,0 +1,258 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
n
abc
1001010
21
32
43
54
65
76
2017
28
39
410
511
612
713
30114
215
316
417
518
619
720
20010121
222
323
424
525
626
727
.........
30135
236
337
438
539
640
741
30010142
243
344
445
546
647
748
20149
250
351
452
553
654
755
30156
257
358
459
560
661
762
diff --git a/pandas/tests/io/formats/data/gh14998_expected_output.html b/pandas/tests/io/formats/data/gh14998_expected_output.html new file mode 100644 index 00000000000000..62b96493a8ecd3 --- /dev/null +++ b/pandas/tests/io/formats/data/gh14998_expected_output.html @@ -0,0 +1,12 @@ + + + + + + + + + + + +
A
1
diff --git a/pandas/tests/io/formats/data/gh6131_expected_output.html b/pandas/tests/io/formats/data/gh6131_expected_output.html new file mode 100644 index 00000000000000..cb3a3363ff0163 --- /dev/null +++ b/pandas/tests/io/formats/data/gh6131_expected_output.html @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
d
aaa...ac
bc
baca1.0...NaN
bbcbNaN...NaN
bcccNaN...3.0
diff --git a/pandas/tests/io/formats/data/gh8452_expected_output.html b/pandas/tests/io/formats/data/gh8452_expected_output.html new file mode 100644 index 00000000000000..81ce397a201e00 --- /dev/null +++ b/pandas/tests/io/formats/data/gh8452_expected_output.html @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ab
cdcd
0353
1464
diff --git a/pandas/tests/io/formats/data/index_1.html b/pandas/tests/io/formats/data/index_1.html new file mode 100644 index 00000000000000..41221865a7cb7a --- /dev/null +++ b/pandas/tests/io/formats/data/index_1.html @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ABC
foo11.2one
bar23.4two
baz35.6NaN
diff --git a/pandas/tests/io/formats/data/index_2.html b/pandas/tests/io/formats/data/index_2.html new file mode 100644 index 00000000000000..a86ba80a69bb1d --- /dev/null +++ b/pandas/tests/io/formats/data/index_2.html @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + +
ABC
11.2one
23.4two
35.6NaN
diff --git a/pandas/tests/io/formats/data/index_3.html b/pandas/tests/io/formats/data/index_3.html new file mode 100644 index 00000000000000..02edba4961bc78 --- /dev/null +++ b/pandas/tests/io/formats/data/index_3.html @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ABC
idx
foo11.2one
bar23.4two
baz35.6NaN
diff --git a/pandas/tests/io/formats/data/index_4.html b/pandas/tests/io/formats/data/index_4.html new file mode 100644 index 00000000000000..0d1bf9ffcd7173 --- /dev/null +++ b/pandas/tests/io/formats/data/index_4.html @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ABC
foocar11.2one
bike23.4two
barcar35.6NaN
diff --git a/pandas/tests/io/formats/data/index_5.html b/pandas/tests/io/formats/data/index_5.html new file mode 100644 index 00000000000000..c5ac12ecd630e1 --- /dev/null +++ b/pandas/tests/io/formats/data/index_5.html @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ABC
idx1idx2
foocar11.2one
bike23.4two
barcar35.6NaN
diff --git a/pandas/tests/io/formats/data/index_formatter.html b/pandas/tests/io/formats/data/index_formatter.html new file mode 100644 index 00000000000000..7a2f8a9f52a040 --- /dev/null +++ b/pandas/tests/io/formats/data/index_formatter.html @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
fooNone
a01
b23
c45
d67
diff --git a/pandas/tests/io/formats/data/justify.html b/pandas/tests/io/formats/data/justify.html new file mode 100644 index 00000000000000..33e4b5715260e7 --- /dev/null +++ b/pandas/tests/io/formats/data/justify.html @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ABC
061223442
13000020
22700001
diff --git a/pandas/tests/io/formats/data/multiindex_1.html b/pandas/tests/io/formats/data/multiindex_1.html new file mode 100644 index 00000000000000..88db1775459726 --- /dev/null +++ b/pandas/tests/io/formats/data/multiindex_1.html @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CL001
CL10101
0abcd
1efgh
diff --git a/pandas/tests/io/formats/data/multiindex_2.html b/pandas/tests/io/formats/data/multiindex_2.html new file mode 100644 index 00000000000000..289ea2202d6b93 --- /dev/null +++ b/pandas/tests/io/formats/data/multiindex_2.html @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
0123
0101
0abcd
1efgh
diff --git a/pandas/tests/io/formats/data/multiindex_sparsify_1.html b/pandas/tests/io/formats/data/multiindex_sparsify_1.html new file mode 100644 index 00000000000000..5b5bcf9ce0a96e --- /dev/null +++ b/pandas/tests/io/formats/data/multiindex_sparsify_1.html @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
01
foo
0001
123
1045
167
diff --git a/pandas/tests/io/formats/data/multiindex_sparsify_2.html b/pandas/tests/io/formats/data/multiindex_sparsify_2.html new file mode 100644 index 00000000000000..fd4c6bd23dae22 --- /dev/null +++ b/pandas/tests/io/formats/data/multiindex_sparsify_2.html @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
foo01
00
foo
0001
123
1045
167
diff --git a/pandas/tests/io/formats/data/multiindex_sparsify_false_multi_sparse_1.html b/pandas/tests/io/formats/data/multiindex_sparsify_false_multi_sparse_1.html new file mode 100644 index 00000000000000..42a5ea5eb58994 --- /dev/null +++ b/pandas/tests/io/formats/data/multiindex_sparsify_false_multi_sparse_1.html @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
01
foo
0001
0123
1045
1167
diff --git a/pandas/tests/io/formats/data/multiindex_sparsify_false_multi_sparse_2.html b/pandas/tests/io/formats/data/multiindex_sparsify_false_multi_sparse_2.html new file mode 100644 index 00000000000000..2be61392e85735 --- /dev/null +++ b/pandas/tests/io/formats/data/multiindex_sparsify_false_multi_sparse_2.html @@ -0,0 +1,48 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
foo01
00
foo
0001
0123
1045
1167
diff --git a/pandas/tests/io/formats/data/truncate.html b/pandas/tests/io/formats/data/truncate.html new file mode 100644 index 00000000000000..a5eb8c5cdbb9b1 --- /dev/null +++ b/pandas/tests/io/formats/data/truncate.html @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
01...1819
2001-01-01NaNNaN...NaNNaN
2001-01-02NaNNaN...NaNNaN
2001-01-03NaNNaN...NaNNaN
2001-01-04NaNNaN...NaNNaN
..................
2001-01-17NaNNaN...NaNNaN
2001-01-18NaNNaN...NaNNaN
2001-01-19NaNNaN...NaNNaN
2001-01-20NaNNaN...NaNNaN
diff --git a/pandas/tests/io/formats/data/truncate_multi_index.html b/pandas/tests/io/formats/data/truncate_multi_index.html new file mode 100644 index 00000000000000..8a295d66db1307 --- /dev/null +++ b/pandas/tests/io/formats/data/truncate_multi_index.html @@ -0,0 +1,101 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
barbaz...fooqux
onetwoone...twoonetwo
baroneNaNNaNNaN...NaNNaNNaN
twoNaNNaNNaN...NaNNaNNaN
bazoneNaNNaNNaN...NaNNaNNaN
...........................
footwoNaNNaNNaN...NaNNaNNaN
quxoneNaNNaNNaN...NaNNaNNaN
twoNaNNaNNaN...NaNNaNNaN
diff --git a/pandas/tests/io/formats/data/truncate_multi_index_sparse_off.html b/pandas/tests/io/formats/data/truncate_multi_index_sparse_off.html new file mode 100644 index 00000000000000..05c644dfbfe083 --- /dev/null +++ b/pandas/tests/io/formats/data/truncate_multi_index_sparse_off.html @@ -0,0 +1,94 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
barbarbaz...fooquxqux
onetwoone...twoonetwo
baroneNaNNaNNaN...NaNNaNNaN
bartwoNaNNaNNaN...NaNNaNNaN
bazoneNaNNaNNaN...NaNNaNNaN
footwoNaNNaNNaN...NaNNaNNaN
quxoneNaNNaNNaN...NaNNaNNaN
quxtwoNaNNaNNaN...NaNNaNNaN
diff --git a/pandas/tests/io/formats/data/unicode_1.html b/pandas/tests/io/formats/data/unicode_1.html new file mode 100644 index 00000000000000..72b810181badef --- /dev/null +++ b/pandas/tests/io/formats/data/unicode_1.html @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
σ
00.0
11.0
22.0
33.0
44.0
55.0
66.0
77.0
88.0
99.0
diff --git a/pandas/tests/io/formats/data/unicode_2.html b/pandas/tests/io/formats/data/unicode_2.html new file mode 100644 index 00000000000000..79c088093e539b --- /dev/null +++ b/pandas/tests/io/formats/data/unicode_2.html @@ -0,0 +1,14 @@ + + + + + + + + + + + + + +
A
0σ
diff --git a/pandas/tests/io/formats/data/with_classes.html b/pandas/tests/io/formats/data/with_classes.html new file mode 100644 index 00000000000000..8cee3f0c7052bb --- /dev/null +++ b/pandas/tests/io/formats/data/with_classes.html @@ -0,0 +1,9 @@ + + + + + + + + +
diff --git a/pandas/tests/io/formats/test_to_html.py b/pandas/tests/io/formats/test_to_html.py index 32cf21ddf5f38c..0da48f60f1b516 100644 --- a/pandas/tests/io/formats/test_to_html.py +++ b/pandas/tests/io/formats/test_to_html.py @@ -1,9 +1,8 @@ # -*- coding: utf-8 -*- import re -from textwrap import dedent from datetime import datetime -from distutils.version import LooseVersion +from io import open import pytest import numpy as np @@ -13,14 +12,6 @@ from pandas.util import testing as tm import pandas.io.formats.format as fmt -div_style = '' -try: - import IPython - if LooseVersion(IPython.__version__) < LooseVersion('3.0.0'): - div_style = ' style="max-width:1500px;overflow:auto;"' -except (ImportError, AttributeError): - pass - def expected_html(datapath, name): """ @@ -39,7 +30,7 @@ def expected_html(datapath, name): """ filename = '.'.join([name, 'html']) filepath = datapath('io', 'formats', 'data', filename) - with open(filepath) as f: + with open(filepath, encoding='utf-8') as f: html = f.read() return html.rstrip() @@ -63,49 +54,28 @@ def check_with_width(df, col_space): check_with_width(df, 50) def test_to_html_with_empty_string_label(self): - # GH3547, to_html regards empty string labels as repeated labels + # GH 3547, to_html regards empty string labels as repeated labels data = {'c1': ['a', 'b'], 'c2': ['a', ''], 'data': [1, 2]} df = DataFrame(data).set_index(['c1', 'c2']) - res = df.to_html() - assert "rowspan" not in res + result = df.to_html() + assert "rowspan" not in result - def test_to_html_unicode(self): + def test_to_html_unicode(self, datapath): df = DataFrame({u('\u03c3'): np.arange(10.)}) - expected = u'\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
\u03c3
00.0
11.0
22.0
33.0
44.0
55.0
66.0
77.0
88.0
99.0
' # noqa + expected = expected_html(datapath, 'unicode_1') assert df.to_html() == expected df = DataFrame({'A': [u('\u03c3')]}) - expected = u'\n \n \n \n \n \n \n \n \n \n \n \n \n
A
0\u03c3
' # noqa + expected = expected_html(datapath, 'unicode_2') assert df.to_html() == expected - def test_to_html_decimal(self): + def test_to_html_decimal(self, datapath): # GH 12031 df = DataFrame({'A': [6.0, 3.1, 2.2]}) result = df.to_html(decimal=',') - expected = ('\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '
A
06,0
13,1
22,2
') + expected = expected_html(datapath, 'gh12031_expected_output') assert result == expected - def test_to_html_escaped(self): + def test_to_html_escaped(self, datapath): a = 'str"}, 'co>l2': {a: "", b: ""}} - rs = DataFrame(test_dict).to_html() - xp = """ - - - - - - - - - - - - - - - - - - - -
co<l1co>l2
str<ing1 &amp;<type 'str'><type 'str'>
stri>ng2 &amp;<type 'str'><type 'str'>
""" - - assert xp == rs - - def test_to_html_escape_disabled(self): + result = DataFrame(test_dict).to_html() + expected = expected_html(datapath, 'escaped') + assert result == expected + + def test_to_html_escape_disabled(self, datapath): a = 'strbold"}, 'co>l2': {a: "bold", b: "bold"}} - rs = DataFrame(test_dict).to_html(escape=False) - xp = """ - - - - - - - - - - - - - - - - - -
co - co>l2
str - boldbold
stri>ng2 &boldbold
""" - - assert xp == rs - - def test_to_html_multiindex_index_false(self): - # issue 8452 + result = DataFrame(test_dict).to_html(escape=False) + expected = expected_html(datapath, 'escape_disabled') + assert result == expected + + def test_to_html_multiindex_index_false(self, datapath): + # GH 8452 df = DataFrame({ 'a': range(2), 'b': range(3, 5), @@ -181,257 +109,47 @@ def test_to_html_multiindex_index_false(self): }) df.columns = MultiIndex.from_product([['a', 'b'], ['c', 'd']]) result = df.to_html(index=False) - expected = """\ - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ab
cdcd
0353
1464
""" - + expected = expected_html(datapath, 'gh8452_expected_output') assert result == expected df.index = Index(df.index.values, name='idx') result = df.to_html(index=False) assert result == expected - def test_to_html_multiindex_sparsify_false_multi_sparse(self): + def test_to_html_multiindex_sparsify_false_multi_sparse(self, datapath): with option_context('display.multi_sparse', False): index = MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]], names=['foo', None]) df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], index=index) - result = df.to_html() - expected = """\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
01
foo
0001
0123
1045
1167
""" - + expected = expected_html( + datapath, 'multiindex_sparsify_false_multi_sparse_1') assert result == expected df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], columns=index[::2], index=index) - result = df.to_html() - expected = """\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
foo01
00
foo
0001
0123
1045
1167
""" - + expected = expected_html( + datapath, 'multiindex_sparsify_false_multi_sparse_2') assert result == expected - def test_to_html_multiindex_sparsify(self): + def test_to_html_multiindex_sparsify(self, datapath): index = MultiIndex.from_arrays([[0, 0, 1, 1], [0, 1, 0, 1]], names=['foo', None]) df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], index=index) - result = df.to_html() - expected = """ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
01
foo
0001
123
1045
167
""" - + expected = expected_html(datapath, 'multiindex_sparsify_1') assert result == expected df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], columns=index[::2], index=index) - result = df.to_html() - expected = """\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
foo01
00
foo
0001
123
1045
167
""" - + expected = expected_html(datapath, 'multiindex_sparsify_2') assert result == expected - def test_to_html_multiindex_odd_even_truncate(self): + def test_to_html_multiindex_odd_even_truncate(self, datapath): # GH 14882 - Issue on truncation with odd length DataFrame mi = MultiIndex.from_product([[100, 200, 300], [10, 20, 30], @@ -439,616 +157,34 @@ def test_to_html_multiindex_odd_even_truncate(self): names=['a', 'b', 'c']) df = DataFrame({'n': range(len(mi))}, index=mi) result = df.to_html(max_rows=60) - expected = """\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
n
abc
1001010
21
32
43
54
65
76
2017
28
39
410
511
612
713
30114
215
316
417
518
619
720
20010121
222
323
424
525
626
727
20128
229
......
633
734
30135
236
337
438
539
640
741
30010142
243
344
445
546
647
748
20149
250
351
452
553
654
755
30156
257
358
459
560
661
762
""" + expected = expected_html(datapath, 'gh14882_expected_output_1') assert result == expected # Test that ... appears in a middle level result = df.to_html(max_rows=56) - expected = """\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
n
abc
1001010
21
32
43
54
65
76
2017
28
39
410
511
612
713
30114
215
316
417
518
619
720
20010121
222
323
424
525
626
727
.........
30135
236
337
438
539
640
741
30010142
243
344
445
546
647
748
20149
250
351
452
553
654
755
30156
257
358
459
560
661
762
""" + expected = expected_html(datapath, 'gh14882_expected_output_2') assert result == expected - def test_to_html_index_formatter(self): + def test_to_html_index_formatter(self, datapath): df = DataFrame([[0, 1], [2, 3], [4, 5], [6, 7]], columns=['foo', None], index=lrange(4)) f = lambda x: 'abcd' [x] result = df.to_html(formatters={'__index__': f}) - expected = """\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
fooNone
a01
b23
c45
d67
""" - + expected = expected_html(datapath, 'index_formatter') assert result == expected - def test_to_html_datetime64_monthformatter(self): + def test_to_html_datetime64_monthformatter(self, datapath): months = [datetime(2016, 1, 1), datetime(2016, 2, 2)] x = DataFrame({'months': months}) def format_func(x): return x.strftime('%Y-%m') result = x.to_html(formatters={'months': format_func}) - expected = """\ - - - - - - - - - - - - - - - - - -
months
02016-01
12016-02
""" + expected = expected_html(datapath, 'datetime64_monthformatter') assert result == expected - def test_to_html_datetime64_hourformatter(self): + def test_to_html_datetime64_hourformatter(self, datapath): x = DataFrame({'hod': pd.to_datetime(['10:10:10.100', '12:12:12.120'], format='%H:%M:%S.%f')}) @@ -1056,25 +192,7 @@ def test_to_html_datetime64_hourformatter(self): def format_func(x): return x.strftime('%H:%M') result = x.to_html(formatters={'hod': format_func}) - expected = """\ - - - - - - - - - - - - - - - - - -
hod
010:10
112:12
""" + expected = expected_html(datapath, 'datetime64_hourformatter') assert result == expected def test_to_html_regression_GH6098(self): @@ -1087,309 +205,28 @@ def test_to_html_regression_GH6098(self): # it works df.pivot_table(index=[u('clé1')], columns=[u('clé2')])._repr_html_() - def test_to_html_truncate(self): + def test_to_html_truncate(self, datapath): index = pd.DatetimeIndex(start='20010101', freq='D', periods=20) df = DataFrame(index=index, columns=range(20)) result = df.to_html(max_rows=8, max_cols=4) - expected = '''\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
01...1819
2001-01-01NaNNaN...NaNNaN
2001-01-02NaNNaN...NaNNaN
2001-01-03NaNNaN...NaNNaN
2001-01-04NaNNaN...NaNNaN
..................
2001-01-17NaNNaN...NaNNaN
2001-01-18NaNNaN...NaNNaN
2001-01-19NaNNaN...NaNNaN
2001-01-20NaNNaN...NaNNaN
''' + expected = expected_html(datapath, 'truncate') assert result == expected - def test_to_html_truncate_multi_index(self): + def test_to_html_truncate_multi_index(self, datapath): arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] df = DataFrame(index=arrays, columns=arrays) result = df.to_html(max_rows=7, max_cols=7) - expected = '''\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
barbaz...fooqux
onetwoone...twoonetwo
baroneNaNNaNNaN...NaNNaNNaN
twoNaNNaNNaN...NaNNaNNaN
bazoneNaNNaNNaN...NaNNaNNaN
...........................
footwoNaNNaNNaN...NaNNaNNaN
quxoneNaNNaNNaN...NaNNaNNaN
twoNaNNaNNaN...NaNNaNNaN
''' + expected = expected_html(datapath, 'truncate_multi_index') assert result == expected @pytest.mark.xfail(reason='GH22887 TypeError', strict=True) - def test_to_html_truncate_multi_index_sparse_off(self): + def test_to_html_truncate_multi_index_sparse_off(self, datapath): arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'], ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']] df = DataFrame(index=arrays, columns=arrays) result = df.to_html(max_rows=7, max_cols=7, sparsify=False) - expected = '''\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
barbarbaz...fooquxqux
onetwoone...twoonetwo
baroneNaNNaNNaN...NaNNaNNaN
bartwoNaNNaNNaN...NaNNaNNaN
bazoneNaNNaNNaN...NaNNaNNaN
footwoNaNNaNNaN...NaNNaNNaN
quxoneNaNNaNNaN...NaNNaNNaN
quxtwoNaNNaNNaN...NaNNaNNaN
''' + expected = expected_html(datapath, 'truncate_multi_index_sparse_off') assert result == expected def test_to_html_border(self): @@ -1399,7 +236,7 @@ def test_to_html_border(self): def test_to_html_border_option(self): df = DataFrame({'A': [1, 2]}) - with pd.option_context('display.html.border', 0): + with option_context('display.html.border', 0): result = df.to_html() assert 'border="0"' in result assert 'border="0"' in df._repr_html_() @@ -1472,45 +309,13 @@ def test_to_html_columns_arg(self): result = frame.to_html(columns=['A']) assert 'B' not in result - def test_to_html_multiindex(self): + def test_to_html_multiindex(self, datapath): columns = MultiIndex.from_tuples(list(zip(np.arange(2).repeat(2), np.mod(lrange(4), 2))), names=['CL0', 'CL1']) df = DataFrame([list('abcd'), list('efgh')], columns=columns) result = df.to_html(justify='left') - expected = ('\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '
CL001
CL10101
0abcd
1efgh
') - + expected = expected_html(datapath, 'multiindex_1') assert result == expected columns = MultiIndex.from_tuples(list(zip( @@ -1519,237 +324,53 @@ def test_to_html_multiindex(self): df = DataFrame([list('abcd'), list('efgh')], columns=columns) result = df.to_html(justify='right') - expected = ('\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '
0123
0101
0abcd
1efgh
') - + expected = expected_html(datapath, 'multiindex_2') assert result == expected @pytest.mark.parametrize("justify", fmt._VALID_JUSTIFY_PARAMETERS) - def test_to_html_justify(self, justify): + def test_to_html_justify(self, justify, datapath): df = DataFrame({'A': [6, 30000, 2], 'B': [1, 2, 70000], 'C': [223442, 0, 1]}, columns=['A', 'B', 'C']) result = df.to_html(justify=justify) - expected = ('\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '
ABC
061223442
13000020
22700001
'.format(justify=justify)) + expected = expected_html(datapath, 'justify').format(justify=justify) 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 + # GH 17527 df = DataFrame() msg = "Invalid value for justify parameter" with pytest.raises(ValueError, match=msg): df.to_html(justify=justify) - def test_to_html_index(self): + def test_to_html_index(self, datapath): index = ['foo', 'bar', 'baz'] df = DataFrame({'A': [1, 2, 3], 'B': [1.2, 3.4, 5.6], 'C': ['one', 'two', np.nan]}, columns=['A', 'B', 'C'], index=index) - expected_with_index = ('\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '
ABC
foo11.2one
bar23.4two
baz35.6NaN
') + expected_with_index = expected_html(datapath, 'index_1') assert df.to_html() == expected_with_index - expected_without_index = ('\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '
ABC
11.2one
23.4two
35.6NaN
') + expected_without_index = expected_html(datapath, 'index_2') result = df.to_html(index=False) for i in index: assert i not in result assert result == expected_without_index df.index = Index(['foo', 'bar', 'baz'], name='idx') - expected_with_index = ('\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '
ABC
idx
foo11.2one
bar23.4two
baz35.6NaN
') + expected_with_index = expected_html(datapath, 'index_3') assert df.to_html() == expected_with_index assert df.to_html(index=False) == expected_without_index tuples = [('foo', 'car'), ('foo', 'bike'), ('bar', 'car')] df.index = MultiIndex.from_tuples(tuples) - expected_with_index = ('\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '
ABC
foocar11.2one
bike23.4two
barcar35.6NaN
') + expected_with_index = expected_html(datapath, 'index_4') assert df.to_html() == expected_with_index result = df.to_html(index=False) @@ -1759,90 +380,27 @@ def test_to_html_index(self): assert result == expected_without_index df.index = MultiIndex.from_tuples(tuples, names=['idx1', 'idx2']) - expected_with_index = ('\n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - ' \n' - '
ABC
idx1idx2
foocar11.2one
bike23.4two
barcar35.6NaN
') + expected_with_index = expected_html(datapath, 'index_5') assert df.to_html() == expected_with_index assert df.to_html(index=False) == expected_without_index - def test_to_html_with_classes(self): + def test_to_html_with_classes(self, datapath): df = DataFrame() result = df.to_html(classes="sortable draggable") - expected = dedent(""" - - - - - - - - - -
- - """).strip() + expected = expected_html(datapath, 'with_classes') assert result == expected result = df.to_html(classes=["sortable", "draggable"]) assert result == expected - def test_to_html_no_index_max_rows(self): - # GH https://github.com/pandas-dev/pandas/issues/14998 + def test_to_html_no_index_max_rows(self, datapath): + # GH 14998 df = DataFrame({"A": [1, 2, 3, 4]}) result = df.to_html(index=False, max_rows=1) - expected = dedent("""\ - - - - - - - - - - - -
A
1
""") + expected = expected_html(datapath, 'gh14998_expected_output') assert result == expected - def test_to_html_multiindex_max_cols(self): + def test_to_html_multiindex_max_cols(self, datapath): # GH 6131 index = MultiIndex(levels=[['ba', 'bb', 'bc'], ['ca', 'cb', 'cc']], labels=[[0, 1, 2], [0, 1, 2]], @@ -1854,53 +412,7 @@ def test_to_html_multiindex_max_cols(self): [[1., np.nan, np.nan], [np.nan, 2., np.nan], [np.nan, np.nan, 3.]]) df = DataFrame(data, index, columns) result = df.to_html(max_cols=2) - expected = dedent("""\ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
d
aaa...ac
bc
baca1.0...NaN
bbcbNaN...NaN
bcccNaN...3.0
""") + expected = expected_html(datapath, 'gh6131_expected_output') assert result == expected @pytest.mark.parametrize('index', [False, 0]) @@ -1911,7 +423,7 @@ def test_to_html_truncation_index_false_max_rows(self, datapath, index): [1.867558, -0.977278], [0.950088, -0.151357], [-0.103219, 0.410599]] - df = pd.DataFrame(data) + df = DataFrame(data) result = df.to_html(max_rows=4, index=index) expected = expected_html(datapath, 'gh15019_expected_output') assert result == expected @@ -1921,35 +433,35 @@ def test_to_html_truncation_index_false_max_cols(self, datapath, index): # GH 22783 data = [[1.764052, 0.400157, 0.978738, 2.240893, 1.867558], [-0.977278, 0.950088, -0.151357, -0.103219, 0.410599]] - df = pd.DataFrame(data) + df = DataFrame(data) result = df.to_html(max_cols=4, index=index) expected = expected_html(datapath, 'gh22783_expected_output') assert result == expected def test_to_html_notebook_has_style(self): - df = pd.DataFrame({"A": [1, 2, 3]}) + df = DataFrame({"A": [1, 2, 3]}) result = df.to_html(notebook=True) assert "tbody tr th:only-of-type" in result assert "vertical-align: middle;" in result assert "thead th" in result def test_to_html_notebook_has_no_style(self): - df = pd.DataFrame({"A": [1, 2, 3]}) + df = DataFrame({"A": [1, 2, 3]}) result = df.to_html() assert "tbody tr th:only-of-type" not in result assert "vertical-align: middle;" not in result assert "thead th" not in result def test_to_html_with_index_names_false(self): - # gh-16493 - df = pd.DataFrame({"A": [1, 2]}, index=pd.Index(['a', 'b'], - name='myindexname')) + # GH 16493 + df = DataFrame({"A": [1, 2]}, index=Index(['a', 'b'], + name='myindexname')) result = df.to_html(index_names=False) assert 'myindexname' not in result def test_to_html_with_id(self): - # gh-8496 - df = pd.DataFrame({"A": [1, 2]}, index=pd.Index(['a', 'b'], - name='myindexname')) + # GH 8496 + df = DataFrame({"A": [1, 2]}, index=Index(['a', 'b'], + name='myindexname')) result = df.to_html(index_names=False, table_id="TEST_ID") assert ' id="TEST_ID"' in result