def test_array_length(self): with pytest.raises(ValueError): mpd.DataFrame({'a': np.array([1, 2]), 'b': np.array([1])}) # correct construction. no error mpd.DataFrame({'a': np.array([1, 2]), 'b': np.array([5, 10])})
def test_pow(self): df_result = df5 ** 3 df_answer = mpd.DataFrame({'a': a5 ** 3, 'b': b5 ** 3}) assert_df_equals(df_result, df_answer) df_result = 2 ** df5 df_answer = mpd.DataFrame({'a': 2 ** a5, 'b': 2 ** b5}) assert_df_equals(df_result, df_answer)
def test_sub(self): df_result = df5 - 3 df_answer = mpd.DataFrame({'a': a5 - 3, 'b': b5 - 3}) assert_df_equals(df_result, df_answer) df_result = 3 - df5 df_answer = mpd.DataFrame({'a': 3 - a5, 'b': 3 - b5}) assert_df_equals(df_result, df_answer)
def test_gt_lt(self): df_result = df5 > 3 df_answer = mpd.DataFrame({'a': a5 > 3, 'b': b5 > 3}) assert_df_equals(df_result, df_answer) df_result = df5 < 2 df_answer = mpd.DataFrame({'a': a5 < 2, 'b': b5 < 2}) assert_df_equals(df_result, df_answer)
def test_ge_le(self): df_result = df5 >= 3 df_answer = mpd.DataFrame({'a': a5 >= 3, 'b': b5 >= 3}) assert_df_equals(df_result, df_answer) df_result = df5 < 2 df_answer = mpd.DataFrame({'a': a5 <= 2, 'b': b5 <= 2}) assert_df_equals(df_result, df_answer)
def test_floordiv(self): df_result = df5 // 3 df_answer = mpd.DataFrame({'a': a5 // 3, 'b': b5 // 3}) assert_df_equals(df_result, df_answer) df_result = 3 // df5 df_answer = mpd.DataFrame({'a': 3 // a5, 'b': 3 // b5}) assert_df_equals(df_result, df_answer)
def test_eq_ne(self): df_result = df5 == 3 df_answer = mpd.DataFrame({'a': a5 == 3, 'b': b5 == 3}) assert_df_equals(df_result, df_answer) df_result = df5 != 2 df_answer = mpd.DataFrame({'a': a5 != 2, 'b': b5 != 2}) assert_df_equals(df_result, df_answer)
def test_head_tail(self): df_result = df.head(2) df_answer = mpd.DataFrame({'a': a[:2], 'b': b[:2], 'c': c[:2], 'd': d[:2], 'e': e[:2]}) assert_df_equals(df_result, df_answer) df_result = df.tail(2) df_answer = mpd.DataFrame({'a': a[-2:], 'b': b[-2:], 'c': c[-2:], 'd':d[-2:], 'e': e[-2:]}) assert_df_equals(df_result, df_answer)
def test_value_counts(self): df_temp = mpd.DataFrame({'state': np.array(['texas', 'texas', 'texas', 'florida', 'florida', 'florida', 'florida', 'ohio']), 'fruit': np.array(['a', 'a', 'a', 'a', 'b', 'b', 'b', 'a'])}) df_results = df_temp.value_counts() df_answer = mpd.DataFrame({'state': np.array(['florida', 'texas', 'ohio'], dtype=object), 'count': np.array([4, 3, 1])}) assert_df_equals(df_results[0], df_answer) df_answer = mpd.DataFrame({'fruit': np.array(['a', 'b'], dtype=object), 'count': np.array([5, 3])}) assert_df_equals(df_results[1], df_answer)
def test_value_counts_normalize(self): df_temp = mpd.DataFrame({'state': np.array(['texas', 'texas', 'texas', 'florida', 'florida', 'florida', 'florida', 'ohio']), 'fruit': np.array(['a', 'a', 'a', 'a', 'b', 'b', 'b', 'a'])}) df_results = df_temp.value_counts(normalize=True) df_answer = mpd.DataFrame({'state': np.array(['florida', 'texas', 'ohio'], dtype=object), 'count': np.array([.5, .375, .125])}) assert_df_equals(df_results[0], df_answer) df_answer = mpd.DataFrame({'fruit': np.array(['a', 'b'], dtype=object), 'count': np.array([.625, .375])}) assert_df_equals(df_results[1], df_answer)
def test_col_slice(self): df_answer = mpd.DataFrame({'a': a, 'b': b, 'c': c}) assert_df_equals(df[:, :3], df_answer) df_answer = mpd.DataFrame({'a': a[::2], 'b': b[::2], 'c': c[::2]}) assert_df_equals(df[::2, :3], df_answer) df_answer = mpd.DataFrame({'a': a[::2], 'b': b[::2], 'c': c[::2], 'd': d[::2], 'e': e[::2]}) assert_df_equals(df[::2, :], df_answer) with pytest.raises(TypeError): df[:, set()]
def test_simple_boolean(self): bool_arr = np.array([True, False, False]) df_bool = mpd.DataFrame({'col': bool_arr}) df_result = df[df_bool] df_answer = mpd.DataFrame({'a': a[bool_arr], 'b': b[bool_arr], 'c': c[bool_arr], 'd': d[bool_arr], 'e': e[bool_arr]}) assert_df_equals(df_result, df_answer) with pytest.raises(ValueError): df_bool = mpd.DataFrame({'col': bool_arr, 'col2': bool_arr}) df[df_bool] with pytest.raises(TypeError): df_bool = mpd.DataFrame({'col': np.array[1, 2, 3]})
def test_sample(self): df_result = df7.sample(2, seed=1) df_answer = mpd.DataFrame({'a': np.array(['a', 'a'], dtype=object), 'b': np.array([2., 5.1])}) assert_df_equals(df_result, df_answer) df_result = df7.sample(frac=.7, seed=1) df_answer = mpd.DataFrame({'a': np.array(['a', 'a', 'b'], dtype=object), 'b': np.array([2., 5.1, 6.])}) assert_df_equals(df_result, df_answer) with pytest.raises(TypeError): df7.sample(2.5) with pytest.raises(ValueError): df7.sample(frac=-2)
def test_add(self): df_result = df5 + 3 df_answer = mpd.DataFrame({'a': a5 + 3, 'b': b5 + 3}) assert_df_equals(df_result, df_answer) df_result = 3 + df5 assert_df_equals(df_result, df_answer)
def test_data_types(self): df_result = df_emp.dtypes cols = np.array(['dept', 'race', 'gender', 'salary'], dtype='O') dtypes = np.array(['string', 'string', 'string', 'int'], dtype='O') df_answer = mpd.DataFrame({'Column Name': cols, 'Data Type': dtypes}) assert_df_equals(df_result, df_answer)
def test_mul(self): df_result = df5 * 3 df_answer = mpd.DataFrame({'a': a5 * 3, 'b': b5 * 3}) assert_df_equals(df_result, df_answer) df_result = 3 * df5 assert_df_equals(df_result, df_answer)
def test_input_types(self): with pytest.raises(TypeError): mpd.DataFrame([1, 2, 3]) with pytest.raises(TypeError): mpd.DataFrame({1: 5, 'b': 10}) with pytest.raises(TypeError): mpd.DataFrame({'a': np.array([1]), 'b': 10}) with pytest.raises(ValueError): mpd.DataFrame({'a': np.array([1]), 'b': np.array([[1]])}) # correct construction. no error mpd.DataFrame({'a': np.array([1]), 'b': np.array([1])})
def test_list_columns(self): df_answer = mpd.DataFrame({'c': c, 'e': e}) assert_df_equals(df[:, [2, 4]], df_answer) assert_df_equals(df[:, [2, 'e']], df_answer) assert_df_equals(df[:, ['c', 'e']], df_answer) df_result = df[2, ['a', 'e']] df_answer = mpd.DataFrame({'a': a[[2]], 'e': e[[2]]}) assert_df_equals(df_result, df_answer) df_answer = mpd.DataFrame({'c': c[[1, 2]], 'e': e[[1, 2]]}) assert_df_equals(df[[1, 2], ['c', 'e']], df_answer) df1 = mpd.DataFrame({'a': np.array([True, False, True]), 'b': np.array([1, 3, 5])}) df_answer = mpd.DataFrame({'c': c[[0, 2]], 'e': e[[0, 2]]}) assert_df_equals(df[df1['a'], ['c', 'e']], df_answer)
def test_dtypes(self): cols = np.array(['a', 'b', 'c', 'd', 'e'], dtype='O') dtypes = np.array(['string', 'string', 'float', 'bool', 'int'], dtype='O') df_result = df.dtypes df_answer = mpd.DataFrame({'Column Name': cols, 'Data Type': dtypes}) assert_df_equals(df_result, df_answer)
def test_all_row_selections(self): df1 = mpd.DataFrame({'a': np.array([True, False, True]), 'b': np.array([1, 3, 5])}) with pytest.raises(ValueError): df[df1, 'e'] with pytest.raises(TypeError): df[df1['b'], 'c'] df_result = df[df1['a'], 'c'] df_answer = mpd.DataFrame({'c': c[[True, False, True]]}) assert_df_equals(df_result, df_answer) df_result = df[[1, 2], 0] df_answer = mpd.DataFrame({'a': a[[1, 2]]}) assert_df_equals(df_result, df_answer) df_result = df[1:, 0] assert_df_equals(df_result, df_answer)
def test_pivot_table_rows_or_cols(self): df_result = df8.pivot_table(rows='a') df_answer = mpd.DataFrame({'a': np.array(['a', 'b'], dtype=object), 'size': np.array([5, 3])}) assert_df_equals(df_result, df_answer) df_result = df8.pivot_table(rows='a', values='c', aggfunc='sum') df_answer = mpd.DataFrame({'a': np.array(['a', 'b'], dtype=object), 'sum': np.array([22, 14])}) assert_df_equals(df_result, df_answer) df_result = df8.pivot_table(columns='b') df_answer = mpd.DataFrame({'A': np.array([4]), 'B': np.array([4])}) assert_df_equals(df_result, df_answer) df_result = df8.pivot_table(columns='a', values='c', aggfunc='sum') df_answer = mpd.DataFrame({'a': np.array([22]), 'b': np.array([14])}) assert_df_equals(df_result, df_answer)
def test_head(self): data = {'dept': np.array(['Houston Police Department-HPD', 'Houston Fire Department (HFD)', 'Houston Police Department-HPD', 'Public Works & Engineering-PWE', 'Houston Airport System (HAS)'], dtype='O'), 'race': np.array(['White', 'White', 'Black', 'Asian', 'White'], dtype='O'), 'gender': np.array(['Male', 'Male', 'Male', 'Male', 'Male'], dtype='O'), 'salary': np.array([45279, 63166, 66614, 71680, 42390])} result = df_emp.head() answer = mpd.DataFrame(data) assert_df_equals(result, answer)
def test_new_column(self): df_result = mpd.DataFrame({'a': a, 'b': b, 'c': c, 'd': d, 'e': e}) f = np.array([1.5, 23, 4.11]) df_result['f'] = f df_answer = mpd.DataFrame({'a': a, 'b': b, 'c': c, 'd': d, 'e': e, 'f': f}) assert_df_equals(df_result, df_answer) df_result = mpd.DataFrame({'a': a, 'b': b, 'c': c, 'd': d, 'e': e}) df_result['f'] = True f = np.repeat(True, 3) df_answer = mpd.DataFrame({'a': a, 'b': b, 'c': c, 'd': d, 'e': e, 'f': f}) assert_df_equals(df_result, df_answer) df_result = mpd.DataFrame({'a': a, 'b': b, 'c': c, 'd': d, 'e': e}) f = np.array([1.5, 23, 4.11]) df_result['c'] = f df_answer = mpd.DataFrame({'a': a, 'b': b, 'c': f, 'd': d, 'e': e}) assert_df_equals(df_result, df_answer) with pytest.raises(NotImplementedError): df[['a', 'b']] = 5 with pytest.raises(ValueError): df['a'] = np.random.rand(5, 5) with pytest.raises(ValueError): df['a'] = np.random.rand(5) with pytest.raises(ValueError): df['a'] = df[['a', 'b']] with pytest.raises(ValueError): df1 = mpd.DataFrame({'a': np.random.rand(5)}) df['a'] = df1 with pytest.raises(TypeError): df['a'] = set()
def test_istitle(self): result = df_string.str.istitle('num') num = np.array([False, False]) answer = mpd.DataFrame({'num': num}) assert_df_equals(result, answer)
def test_zfill(self): result = df_string.str.zfill('movie', 16) movie = np.array(['0field of dreams', '0000000star wars'], dtype='O') answer = mpd.DataFrame({'movie': movie}) assert_df_equals(result, answer)
def test_upper(self): result = df_string.str.upper('movie') movie = np.array(['FIELD OF DREAMS', 'STAR WARS'], dtype='O') answer = mpd.DataFrame({'movie': movie}) assert_df_equals(result, answer)
def test_title(self): result = df_string.str.title('movie') movie = np.array(['Field Of Dreams', 'Star Wars'], dtype='O') answer = mpd.DataFrame({'movie': movie}) assert_df_equals(result, answer)
def test_replace(self): result = df_string.str.replace('movie', 's', 'Z') movie = np.array(['field of dreamZ', 'Ztar warZ'], dtype='O') answer = mpd.DataFrame({'movie': movie}) assert_df_equals(result, answer)
def test_strip(self): result = df_string.str.strip('movie', 'fs') movie = np.array(['ield of dream', 'tar war'], dtype='O') answer = mpd.DataFrame({'movie': movie}) assert_df_equals(result, answer)
def test_isupper(self): result = df_string.str.isupper('movie') movie = np.array([False, False]) answer = mpd.DataFrame({'movie': movie}) assert_df_equals(result, answer)