-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_transpose.py
More file actions
317 lines (279 loc) · 9.55 KB
/
test_transpose.py
File metadata and controls
317 lines (279 loc) · 9.55 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#!/usr/bin/env python3
import datetime
import unittest
from collections import namedtuple
from typing import NamedTuple
import numpy as np
import pandas as pd
from pandas.api.types import is_datetime64_dtype, is_numeric_dtype
from pandas.testing import assert_frame_equal
import transpose
from cjwmodule.testing.i18n import cjwmodule_i18n_message, i18n_message
Column = namedtuple("Column", ("name", "type")) # transpose ignores 'format'
class DefaultSettings(NamedTuple):
MAX_COLUMNS_PER_TABLE: int = 1000
MAX_BYTES_PER_COLUMN_NAME: int = 100
def render(table, firstcolname="", input_columns=None, settings=DefaultSettings()):
def _infer_type(series):
if is_numeric_dtype(series):
return "number"
elif is_datetime64_dtype(series):
return "timestamp"
else:
return "text"
def _infer_column(colname):
return Column(colname, _infer_type(table[colname]))
if input_columns is None:
input_columns = {c: _infer_column(c) for c in table.columns}
return transpose.render(
table,
{"firstcolname": firstcolname},
input_columns=input_columns,
settings=settings,
)
class MigrateParamsTest(unittest.TestCase):
def test_v0(self):
self.assertEqual(transpose.migrate_params({}), {"firstcolname": ""})
def test_v1(self):
# as of 2019-02-11
self.assertEqual(
transpose.migrate_params({"firstcolname": "A"}), {"firstcolname": "A"}
)
class RenderTest(unittest.TestCase):
def test_normal(self):
# A B C
# 0 b c d
# 1 c d e
#
# Transposed (with A as headers) becomes:
#
# A b c
# 0 B c d
# 1 C d e
table = pd.DataFrame({"A": ["b", "c"], "B": ["c", "d"], "C": ["d", "e"]})
result = render(table)
assert_frame_equal(
result, pd.DataFrame({"A": ["B", "C"], "b": ["c", "d"], "c": ["d", "e"]})
)
def test_rename_first_column(self):
# As above, but with a user supplied first column name
table = pd.DataFrame({"A": ["b", "c"], "B": ["c", "d"], "C": ["d", "e"]})
result = render(table, "Fish")
assert_frame_equal(
result, pd.DataFrame({"Fish": ["B", "C"], "b": ["c", "d"], "c": ["d", "e"]})
)
def test_empty_input(self):
table = pd.DataFrame()
result = render(table)
assert_frame_equal(result, pd.DataFrame())
def test_empty_input_with_columns(self):
table = pd.DataFrame({"A": [], "B": []}, dtype=object)
result = render(table)
assert_frame_equal(result, pd.DataFrame({"A": ["B"]}))
def test_colnames_to_str(self):
# A B C
# 0 b c d
# 1 1 d e
# 2 dt e f
# 3 na f g
#
# Transposed (with A as headers) becomes:
#
# Column b 1 dt unnamed
# 0 B c d e f
# 1 C d e f g
dt = datetime.datetime(2018, 10, 16, 12, 7)
table = pd.DataFrame(
{
"A": [1.1, 2.2, 3.3, None],
"B": ["c", "d", "e", "f"],
"C": ["d", "e", "f", "g"],
}
)
result = render(table)
assert_frame_equal(
result[0],
pd.DataFrame(
{
"A": ["B", "C"],
"1.1": ["c", "d"],
"2.2": ["d", "e"],
"3.3": ["e", "f"],
"Column 5": ["f", "g"],
}
),
)
def test_warn_and_rename_on_duplicates(self):
# A B C
# 0 b c d
# 1 b d e
#
# Transposed (with header A, allowing duplicates) becomes:
#
# Column b b
# 0 B c d
# 1 C d e
table = pd.DataFrame({"A": ["b", "b"], "B": ["c", "d"], "C": ["d", "e"]})
result = render(table)
self.assertEqual(
result[1],
[
cjwmodule_i18n_message(
"util.colnames.warnings.numbered",
{"n_columns": 1, "first_colname": "b 2"},
)
],
)
assert_frame_equal(
result[0],
pd.DataFrame({"A": ["B", "C"], "b": ["c", "d"], "b 2": ["d", "e"]}),
)
def test_warn_and_rename_on_empty_and_unnamed_colname(self):
table = pd.DataFrame(
{"A": ["x", "", "Column 3", np.nan], "B": ["b1", "b2", "b3", "b4"]}
)
result = render(table)
self.assertEqual(
result[1],
[
cjwmodule_i18n_message(
"util.colnames.warnings.default",
{"n_columns": 2, "first_colname": "Column 4"},
),
cjwmodule_i18n_message(
"util.colnames.warnings.numbered",
{"n_columns": 1, "first_colname": "Column 4"},
),
],
)
assert_frame_equal(
result[0],
pd.DataFrame(
{
"A": ["B"],
"x": ["b1"],
"Column 4": ["b2"],
"Column 3": ["b3"],
"Column 5": ["b4"],
}
),
)
def test_warn_on_convert_to_str_including_column_header(self):
table = pd.DataFrame({"A": [1, 2], "B": ["x", "y"], "C": [3, 4]})
result = render(table)
assert_frame_equal(
result[0],
pd.DataFrame({"A": ["B", "C"], "1": ["x", "3"], "2": ["y", "4"]}),
)
self.assertEqual(
result[1],
[
{
"message": i18n_message(
"warnings.headersConvertedToText.message", {"column_name": "A"}
),
"quickFixes": [
{
"text": i18n_message(
"warnings.headersConvertedToText.quickFix.text",
{"column_name": '"A"'},
),
"action": "prependModule",
"args": ["converttotext", {"colnames": ["A"]}],
}
],
},
{
"message": i18n_message(
"warnings.differentColumnTypes.message",
{"n_columns": 1, "first_colname": "C"},
),
"quickFixes": [
{
"text": i18n_message(
"warnings.differentColumnTypes.quickFix.text",
{"n_columns": 1},
),
"action": "prependModule",
"args": ["converttotext", {"colnames": ["C"]}],
}
],
},
],
)
def test_allow_max_n_columns(self):
table = pd.DataFrame(
{
"A": ["a1", "a2", "a3"],
"B": ["b1", "b2", "b3"],
}
)
result = render(table, settings=DefaultSettings(MAX_COLUMNS_PER_TABLE=3))
assert_frame_equal(
result,
pd.DataFrame(
{
"A": ["B"],
"a1": ["b1"],
"a2": ["b2"],
"a3": ["b3"],
}
),
)
def test_truncate_past_max_n_columns(self):
table = pd.DataFrame(
{
"A": ["a1", "a2", "a3", "a4"],
"B": ["b1", "b2", "b3", "b4"],
}
)
result = render(table, settings=DefaultSettings(MAX_COLUMNS_PER_TABLE=3))
assert_frame_equal(
result[0],
pd.DataFrame(
{
"A": ["B"],
"a1": ["b1"],
"a2": ["b2"],
"a3": ["b3"],
}
),
)
self.assertEqual(
result[1],
[i18n_message("warnings.tooManyRows", {"max_columns": 3})],
)
def test_transpose_categorical_and_rename_index(self):
# Avoid TypeError: cannot insert an item into a CategoricalIndex
# that is not already an existing category
#
# Akin to https://github.com/pandas-dev/pandas/issues/19136
#
# Column names should strings, not a CategoricalIndex.
table = pd.DataFrame(
{
"A": pd.Series(["a1", "a2"], dtype="category"), # becomes ret.columns
"B": pd.Series(["b1", "b2"]),
}
)
result = render(table, firstcolname="X")
assert_frame_equal(
result, pd.DataFrame({"X": ["B"], "a1": ["b1"], "a2": ["b2"]})
)
def test_warn_and_rename_column_if_firstcolname_conflicts(self):
table = pd.DataFrame({"X": ["B", "C"], "A": ["c", "d"]})
result = render(table, firstcolname="B")
self.assertEqual(
result[1],
[
cjwmodule_i18n_message(
"util.colnames.warnings.numbered",
{"n_columns": 1, "first_colname": "B 2"},
)
],
)
assert_frame_equal(
result[0], pd.DataFrame({"B": ["A"], "B 2": ["c"], "C": ["d"]})
)
if __name__ == "__main__":
unittest.main()