def test_tail(self, df_small): actual = df_small.tail(2) data = [4, 5] expected = DataFrame( OrderedDict( (('a', np.array(data, dtype=np.float32)), ('b', np.array(data)), ('c', np.array([' dC ', 'secrETariat'], dtype=np.dtype('|S11'))))), Index(np.array([3, 4]))) assert_dataframe_equal(actual, expected)
def test_filter(self, df_small): actual = df_small[Series(np.array([False, True, True, False, False]))] data = [2, 3] expected = DataFrame( OrderedDict( (('a', np.array(data, dtype=np.float32)), ('b', np.array(data)), ('c', np.array(['Abc', 'goosfraba'], dtype=np.dtype('|S11'))))), Index(np.array([1, 2]))) assert_dataframe_equal(actual, expected)
def test_merge_sorted_unique_multi_on_inner(self, df1, df2): actual = df1.merge(df2, on=['a', 'b'], is_on_sorted=True) expected = DataFrame( OrderedDict( (('index', np.array([5])), ('d', np.array(['def'], dtype=np.dtype('|S4'))), ('c', np.array([5])))), MultiIndex( [np.array([3]), np.array([4], dtype=np.float32)], ['a', 'b'])) assert_dataframe_equal(actual, expected)
def test_merge_sorted_unique_single_on_inner(self, df1, df2): actual = df1.merge(df2, on='a') expected = DataFrame( OrderedDict( (('index', np.array([3, 5])), ('b_x', np.array([2, 4], dtype=np.float32)), ('d', np.array(['abc', 'def'], dtype=np.dtype('|S4'))), ('b_y', Series(np.arange(3, 5, dtype=np.float32))), ('c', np.arange(4, 6)))), Index(np.array([1, 3]), np.dtype(np.int64), 'a')) assert_dataframe_equal(actual, expected)
def test_iloc_indices(self, df_small): indices = Series(np.array([0, 2, 3])) actual = df_small.iloc[indices] data = [1, 3, 4] expected = DataFrame( OrderedDict( (('a', np.array(data, dtype=np.float32)), ('b', np.array(data)), ('c', np.array(['a', 'goosfraba', ' dC '], dtype=np.dtype('|S11'))))), Index(np.array([0, 2, 3]))) assert_dataframe_equal(actual, expected)
def test_merge_sorted_unique_single_on_right(self, df1, df2): actual = df1.merge(df2, on='a', how='right') expected = DataFrame( OrderedDict( (('index', np.array([3, 5, -999])), ('b_x', np.array([2, 4, -999.], dtype=np.float32)), ('d', np.array(['abc', 'def', 'efgh'], dtype=np.dtype('|S4'))), ('b_y', Series(np.array([3., 4., 5.], dtype=np.float32))), ('c', np.array([4, 5, 6])))), Index(np.array([1, 3, 5]), np.dtype(np.int64), 'a')) assert_dataframe_equal(actual, expected)
def test_merge_sorted_unique_single_on_left(self, df1, df2): actual = df1.merge(df2, on='a', how='left') expected = DataFrame( OrderedDict( (('index', np.arange(2, 7)), ('b_x', np.arange(1, 6, dtype=np.float32)), ('d', np.array(['None', 'abc', 'None', 'def', 'None'], dtype=np.bytes_)), ('b_y', Series( np.array([-999., 3., -999., 4., -999.], dtype=np.float32))), ('c', np.array([-999, 4, -999, 5, -999])))), Index(np.arange(5), np.dtype(np.int64), 'a')) assert_dataframe_equal(actual, expected)
def df_empty(): return DataFrame()
def df_small(data_f32, series_i64, series_str, index_i64): return DataFrame(OrderedDict((('a', data_f32), ('b', series_i64), ('c', series_str))), index_i64)
def df_dupl(series_i64, index_i64): return DataFrame(OrderedDict((('a', np.array([0, 1, 1, 2, 3], dtype=np.float32)), ('b', [4, 5, 5, 6, 6]), ('c', series_i64))), index_i64)
def test_astype_dtype(self, df1, data_i64, index_i64_2): actual = df1.astype(np.dtype(np.int64)) expected = DataFrame( OrderedDict((('a', np.arange(5)), ('b', data_i64))), index_i64_2) assert_dataframe_equal(actual, expected)
def test_set_index(self, df_small, data_f32, data_i64, data_str): actual = df_small.set_index('b') expected = DataFrame(OrderedDict((('a', data_f32), ('c', data_str))), Index(data_i64, np.dtype(np.int64), 'b')) assert_dataframe_equal(actual, expected)
def test_drop_multi(self, df_small, series_i64, index_i64): actual = df_small.drop(['a', 'c']) expected = DataFrame({'b': series_i64}) assert_dataframe_equal(actual, expected)
def test_drop_single(self, df_small, data_f32, series_i64, index_i64): actual = df_small.drop('c') expected = DataFrame(OrderedDict((('a', data_f32), ('b', series_i64))), index_i64) assert_dataframe_equal(actual, expected)
def df1(data_f32, index_i64_2): return DataFrame(OrderedDict((('a', Series(np.arange(5))), ('b', data_f32))), index_i64_2)
def test_init_list(self): data = [1, 2, 3] actual = DataFrame({'a': data}) expected = DataFrame({'a': np.array(data)}) assert_dataframe_equal(actual, expected)
def df2(): return DataFrame(OrderedDict((('b', np.arange(3, 6, dtype=np.float32)), ('c', np.arange(4, 7)))), MultiIndex([np.array([1, 3, 5]), Index(np.array(['abc', 'def', 'efgh'], dtype=np.bytes_))], ['a', 'd']))
def test_empty_dataframe_init(): assert_dataframe_equal(DataFrame(), DataFrame({}, Index(np.empty(0, dtype=np.int64))))