-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest_df.py
119 lines (93 loc) · 3.42 KB
/
test_df.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
from babypandas import DataFrame
from babypandas import Series
class TestDF:
def setup(self):
self.data = [
{'a': 1, 'b': 'banana', 'c': 33},
{'a': 3, 'b': 'cat', 'c': 3},
]
self.df = DataFrame(self.data)
def test_can_construct(self):
df = DataFrame()
def test_construct_from_list_of_dicts(self):
pass
def test_retrieve_column(self):
assert self.df['a'] == [1, 3]
assert self.df['b'] == ['banana', 'cat']
def test_columns_attribute(self):
assert set(self.df.columns) == {'a', 'b', 'c'}
def test_add_column_add(self):
self.df['d'] = 1
assert set(self.df.columns) == {'a', 'b', 'c', 'd'}
def test_set_value_for_existing_column(self):
self.df['a'] = 1
assert self.df['a'] == [1, 1]
def test_set_iterable_for_column(self):
self.df['a'] = [1, 2]
assert self.df['a'] == [1, 2]
def test_remove_column(self):
del self.df['c']
assert set(self.df.columns) == {'a', 'b'}
def test_isin_bool(self):
assert 'c' in self.df
assert 'd' not in self.df
def test_iterate_gives_col_names(self):
assert set(iter(self.df)) == {'a', 'b', 'c'}
def test_map_fn(self):
assert self.df['b'].map(str.upper) == ['BANANA', 'CAT']
# chain
assert self.df['b'].map(str.upper).map(str.lower) == ['banana', 'cat']
def test_addition(self):
self.df['d'] = self.df['a'] + self.df['c']
assert self.df['d'] == [34, 6]
def test_addition_scalar(self):
self.df['d'] = self.df['a'] + 1
assert self.df['d'] == [2, 4]
def test_multiplication(self):
d = self.df['a'] * self.df['c']
assert d == [33, 9]
def test_multiply_scalar(self):
d = self.df['a'] * 1
assert self.df['a'] == d
d = self.df['a'] * 2
assert d == [2, 6]
d = 2 * self.df['a']
assert d == [2, 6]
def test_bool_comparison_between_series(self):
result = self.df['a'] == self.df['a']
assert result == [True, True]
def test_bool_compare_scalar(self):
result = self.df['a'] == 1
assert result == [True, False]
result = self.df['a'] != 1
assert result == [False, True]
def test_bool_gt_etc_compare(self):
result = self.df['a'] < 2
assert result == [True, False]
result = self.df['a'] > 2
assert result == [False, True]
result = self.df['a'] < 3
assert result == [True, False]
result = self.df['a'] <= 3
assert result == [True, True]
#result = self.df['a'] < self.df['c']
result = self.df['a'] == self.df['a']
assert result == [True, True]
result = self.df['a'] < self.df['a']
assert result == [False, False]
def test_filter_by_boolean_indexing(self):
assert len(self.df[True, False]) == 1
result = self.df[self.df['a'] == 1]
assert result['a'] == [1]
assert len(result) == 1
def test_get_multiple_columns(self):
subset_df = self.df[['a', 'b']]
assert set(subset_df.columns) == {'a', 'b'}
def test_len(self):
assert len(self.df) == 2
assert len(DataFrame()) == 0
def test_divide(self):
assert Series([2, 4, 6]) / 2 == [1, 2, 3]
def test_series_sum(self):
assert self.df['a'].sum() == 4
#print 1.0 * self.df['a'] / self.df['a'].sum()