-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_droprowsbyposition.py
174 lines (155 loc) · 5.13 KB
/
test_droprowsbyposition.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
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
import unittest
import pandas as pd
from pandas.testing import assert_frame_equal
from droprowsbyposition import migrate_params, render
from cjwmodule.testing.i18n import i18n_message
class TestMigrateParams(unittest.TestCase):
def test_v0_with_first_row_and_last_row(self):
# droprowsbyposition got some behavior prior to our migrate_params()
# idea. So it actually has two logic paths....
self.assertEqual(
migrate_params(
{
"rows": "",
"first_row": 2,
"last_row": 54,
}
),
{
"rows": "2-54",
},
)
def test_v0_with_rows(self):
self.assertEqual(
migrate_params(
{
"rows": "1-23",
# first_row and last_row are 'backup' values, only for when 'rows'
# is not set. So they'll be dropped.
"first_row": 2,
"last_row": 54,
}
),
{
"rows": "1-23",
},
)
def test_v1(self):
self.assertEqual(
migrate_params(
{
"rows": "1-23",
}
),
{
"rows": "1-23",
},
)
class TestDropRowsByPosition(unittest.TestCase):
def setUp(self):
# Test data includes some partially and completely empty rows because
# this tends to freak out Pandas
self.table = pd.DataFrame(
{
"A": [1, 2, 3, 4],
"B": [2, 3, None, 5],
"C": [None, None, None, None],
}
)
def test_default_params_NOP(self):
out = render(self.table, {"rows": ""})
assert_frame_equal(out, self.table)
def test_empty_input_NOP(self):
# https://www.pivotaltracker.com/n/projects/2132449/stories/161945860
out = render(pd.DataFrame(), {"rows": ""})
self.assertTrue(out.empty)
def test_drop_one_row(self):
out = render(self.table, {"rows": "2"})
assert_frame_equal(
out,
pd.DataFrame(
{
"A": [1, 3, 4],
"B": [2, None, 5],
"C": [None, None, None],
}
),
)
def test_drop_first_rows(self):
out = render(self.table, {"rows": "1-2"})
assert_frame_equal(
out,
pd.DataFrame(
{
"A": [3, 4],
"B": [None, 5],
"C": [None, None],
}
),
)
def test_drop_last_rows(self):
out = render(self.table, {"rows": "3-4"})
assert_frame_equal(
out,
pd.DataFrame(
{
"A": [1, 2],
"B": [2.0, 3.0],
"C": [None, None],
}
),
)
def test_drop_multiple_interval_ranges(self):
out = render(self.table, {"rows": "1, 3-4"})
assert_frame_equal(
out,
pd.DataFrame(
{
"A": [2],
"B": [3.0],
"C": [None],
}
),
)
def test_drop_overlapping_interval_ranges(self):
result = render(pd.DataFrame({"A": [1, 2, 3, 4, 5]}), {"rows": "1-2,2"})
assert_frame_equal(result, pd.DataFrame({"A": [3, 4, 5]}))
def test_drop_everything(self):
out = render(self.table, {"rows": "1-4"})
expected = pd.DataFrame({"A": [1], "B": [1.0], "C": [None]})[0:0]
# Let's assert the same thing, three different ways...:
self.assertEqual(len(out), 0)
self.assertTrue(out.empty)
assert_frame_equal(out, expected)
def test_zero_gives_error(self):
out = render(self.table, {"rows": "0-1"})
self.assertEqual(
out, i18n_message("badParam.rows.invalidRange", {"value": "0-1"})
)
def test_backwards_range_gives_error(self):
out = render(self.table, {"rows": "3-1"})
self.assertEqual(
out, i18n_message("badParam.rows.backwardsRange", {"value": "3-1"})
)
def test_drop_after_end(self):
out = render(self.table, {"rows": "5-8"})
assert_frame_equal(
out,
pd.DataFrame(
{
"A": [1, 2, 3, 4],
"B": [2, 3, None, 5],
"C": [None, None, None, None],
}
),
)
def test_remove_unused_categories(self):
result = render(
pd.DataFrame({"A": ["a", "b", "c", "d"]}, dtype="category"), {"rows": "1-2"}
)
assert_frame_equal(result, pd.DataFrame({"A": ["c", "d"]}, dtype="category"))
def test_remove_unused_categories_for_empty_row(self):
result = render(
pd.DataFrame({"A": ["a", "b", "c", "d"]}, dtype="category"), {"rows": "1-4"}
)
assert_frame_equal(result, pd.DataFrame({"A": []}, dtype="category"))