forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_xlwt.py
136 lines (108 loc) · 4.36 KB
/
test_xlwt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import re
import numpy as np
import pytest
from pandas import (
DataFrame,
MultiIndex,
options,
)
import pandas._testing as tm
from pandas.io.excel import (
ExcelWriter,
_XlwtWriter,
)
xlwt = pytest.importorskip("xlwt")
pytestmark = pytest.mark.parametrize("ext,", [".xls"])
def test_excel_raise_error_on_multiindex_columns_and_no_index(ext):
# MultiIndex as columns is not yet implemented 9794
cols = MultiIndex.from_tuples(
[("site", ""), ("2014", "height"), ("2014", "weight")]
)
df = DataFrame(np.random.randn(10, 3), columns=cols)
msg = (
"Writing to Excel with MultiIndex columns and no index "
"\\('index'=False\\) is not yet implemented."
)
with pytest.raises(NotImplementedError, match=msg):
with tm.ensure_clean(ext) as path:
df.to_excel(path, index=False)
def test_excel_multiindex_columns_and_index_true(ext):
cols = MultiIndex.from_tuples(
[("site", ""), ("2014", "height"), ("2014", "weight")]
)
df = DataFrame(np.random.randn(10, 3), columns=cols)
with tm.ensure_clean(ext) as path:
df.to_excel(path, index=True)
def test_excel_multiindex_index(ext):
# MultiIndex as index works so assert no error #9794
cols = MultiIndex.from_tuples(
[("site", ""), ("2014", "height"), ("2014", "weight")]
)
df = DataFrame(np.random.randn(3, 10), index=cols)
with tm.ensure_clean(ext) as path:
df.to_excel(path, index=False)
def test_to_excel_styleconverter(ext):
hstyle = {
"font": {"bold": True},
"borders": {"top": "thin", "right": "thin", "bottom": "thin", "left": "thin"},
"alignment": {"horizontal": "center", "vertical": "top"},
}
xls_style = _XlwtWriter._convert_to_style(hstyle)
assert xls_style.font.bold
assert xlwt.Borders.THIN == xls_style.borders.top
assert xlwt.Borders.THIN == xls_style.borders.right
assert xlwt.Borders.THIN == xls_style.borders.bottom
assert xlwt.Borders.THIN == xls_style.borders.left
assert xlwt.Alignment.HORZ_CENTER == xls_style.alignment.horz
assert xlwt.Alignment.VERT_TOP == xls_style.alignment.vert
def test_write_append_mode_raises(ext):
msg = "Append mode is not supported with xlwt!"
with tm.ensure_clean(ext) as f:
with pytest.raises(ValueError, match=msg):
ExcelWriter(f, engine="xlwt", mode="a")
def test_to_excel_xlwt_warning(ext):
# GH 26552
df = DataFrame(np.random.randn(3, 10))
with tm.ensure_clean(ext) as path:
with tm.assert_produces_warning(
FutureWarning,
match="As the xlwt package is no longer maintained",
):
df.to_excel(path)
def test_option_xls_writer_deprecated(ext):
# GH 26552
with tm.assert_produces_warning(
FutureWarning,
match="As the xlwt package is no longer maintained",
check_stacklevel=False,
):
options.io.excel.xls.writer = "xlwt"
@pytest.mark.parametrize("style_compression", [0, 2])
def test_kwargs(ext, style_compression):
# GH 42286
kwargs = {"style_compression": style_compression}
with tm.ensure_clean(ext) as f:
msg = re.escape("Use of **kwargs is deprecated")
with tm.assert_produces_warning(FutureWarning, match=msg):
with ExcelWriter(f, engine="xlwt", **kwargs) as writer:
assert (
writer.book._Workbook__styles.style_compression == style_compression
)
# xlwt won't allow us to close without writing something
DataFrame().to_excel(writer)
@pytest.mark.parametrize("style_compression", [0, 2])
def test_engine_kwargs(ext, style_compression):
# GH 42286
engine_kwargs = {"style_compression": style_compression}
with tm.ensure_clean(ext) as f:
with ExcelWriter(f, engine="xlwt", engine_kwargs=engine_kwargs) as writer:
assert writer.book._Workbook__styles.style_compression == style_compression
# xlwt won't allow us to close without writing something
DataFrame().to_excel(writer)
def test_book_and_sheets_consistent(ext):
# GH#45687 - Ensure sheets is updated if user modifies book
with tm.ensure_clean(ext) as f:
with ExcelWriter(f) as writer:
assert writer.sheets == {}
sheet = writer.book.add_sheet("test_name")
assert writer.sheets == {"test_name": sheet}