Python pandas.util.testing.assert_dict_equal() Examples

The following are 30 code examples of pandas.util.testing.assert_dict_equal(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pandas.util.testing , or try the search function .
Example #1
Source File: test_categorical.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_observed_groups(observed):
    # gh-20583
    # test that we have the appropriate groups

    cat = pd.Categorical(['a', 'c', 'a'], categories=['a', 'b', 'c'])
    df = pd.DataFrame({'cat': cat, 'vals': [1, 2, 3]})
    g = df.groupby('cat', observed=observed)

    result = g.groups
    if observed:
        expected = {'a': Index([0, 2], dtype='int64'),
                    'c': Index([1], dtype='int64')}
    else:
        expected = {'a': Index([0, 2], dtype='int64'),
                    'b': Index([], dtype='int64'),
                    'c': Index([1], dtype='int64')}

    tm.assert_dict_equal(result, expected) 
Example #2
Source File: test_grouping.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_groupby_multiindex_tuple(self):
        # GH 17979
        df = pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]],
                          columns=pd.MultiIndex.from_arrays(
                              [['a', 'b', 'b', 'c'],
                               [1, 1, 2, 2]]))
        expected = df.groupby([('b', 1)]).groups
        result = df.groupby(('b', 1)).groups
        tm.assert_dict_equal(expected, result)

        df2 = pd.DataFrame(df.values,
                           columns=pd.MultiIndex.from_arrays(
                               [['a', 'b', 'b', 'c'],
                                ['d', 'd', 'e', 'e']]))
        expected = df2.groupby([('b', 'd')]).groups
        result = df.groupby(('b', 1)).groups
        tm.assert_dict_equal(expected, result)

        df3 = pd.DataFrame(df.values,
                           columns=[('a', 'd'), ('b', 'd'), ('b', 'e'), 'c'])
        expected = df3.groupby([('b', 'd')]).groups
        result = df.groupby(('b', 1)).groups
        tm.assert_dict_equal(expected, result) 
Example #3
Source File: test_grouping.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_multiindex_columns_empty_level(self):
        lst = [['count', 'values'], ['to filter', '']]
        midx = MultiIndex.from_tuples(lst)

        df = DataFrame([[long(1), 'A']], columns=midx)

        grouped = df.groupby('to filter').groups
        assert grouped['A'] == [0]

        grouped = df.groupby([('to filter', '')]).groups
        assert grouped['A'] == [0]

        df = DataFrame([[long(1), 'A'], [long(2), 'B']], columns=midx)

        expected = df.groupby('to filter').groups
        result = df.groupby([('to filter', '')]).groups
        assert result == expected

        df = DataFrame([[long(1), 'A'], [long(2), 'A']], columns=midx)

        expected = df.groupby('to filter').groups
        result = df.groupby([('to filter', '')]).groups
        tm.assert_dict_equal(result, expected) 
Example #4
Source File: test_categorical.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_observed_groups(observed):
    # gh-20583
    # test that we have the appropriate groups

    cat = pd.Categorical(['a', 'c', 'a'], categories=['a', 'b', 'c'])
    df = pd.DataFrame({'cat': cat, 'vals': [1, 2, 3]})
    g = df.groupby('cat', observed=observed)

    result = g.groups
    if observed:
        expected = {'a': Index([0, 2], dtype='int64'),
                    'c': Index([1], dtype='int64')}
    else:
        expected = {'a': Index([0, 2], dtype='int64'),
                    'b': Index([], dtype='int64'),
                    'c': Index([1], dtype='int64')}

    tm.assert_dict_equal(result, expected) 
Example #5
Source File: test_grouping.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_list_grouper_with_nat(self):
        # GH 14715
        df = pd.DataFrame({'date': pd.date_range('1/1/2011',
                                                 periods=365, freq='D')})
        df.iloc[-1] = pd.NaT
        grouper = pd.Grouper(key='date', freq='AS')

        # Grouper in a list grouping
        result = df.groupby([grouper])
        expected = {pd.Timestamp('2011-01-01'): pd.Index(list(range(364)))}
        tm.assert_dict_equal(result.groups, expected)

        # Test case without a list
        result = df.groupby(grouper)
        expected = {pd.Timestamp('2011-01-01'): 365}
        tm.assert_dict_equal(result.groups, expected)


# get_group
# -------------------------------- 
Example #6
Source File: test_numeric.py    From recruit with Apache License 2.0 6 votes vote down vote up
def test_index_groupby(self):
        int_idx = Index(range(6))
        float_idx = Index(np.arange(0, 0.6, 0.1))
        obj_idx = Index('A B C D E F'.split())
        dt_idx = pd.date_range('2013-01-01', freq='M', periods=6)

        for idx in [int_idx, float_idx, obj_idx, dt_idx]:
            to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1])
            tm.assert_dict_equal(idx.groupby(to_groupby),
                                 {1.0: idx[[0, 5]], 2.0: idx[[1, 4]]})

            to_groupby = Index([datetime(2011, 11, 1),
                                datetime(2011, 12, 1),
                                pd.NaT,
                                pd.NaT,
                                datetime(2011, 12, 1),
                                datetime(2011, 11, 1)],
                               tz='UTC').values

            ex_keys = [Timestamp('2011-11-01'), Timestamp('2011-12-01')]
            expected = {ex_keys[0]: idx[[0, 5]],
                        ex_keys[1]: idx[[1, 4]]}
            tm.assert_dict_equal(idx.groupby(to_groupby), expected) 
Example #7
Source File: test_numeric.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_index_groupby(self):
        int_idx = Index(range(6))
        float_idx = Index(np.arange(0, 0.6, 0.1))
        obj_idx = Index('A B C D E F'.split())
        dt_idx = pd.date_range('2013-01-01', freq='M', periods=6)

        for idx in [int_idx, float_idx, obj_idx, dt_idx]:
            to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1])
            tm.assert_dict_equal(idx.groupby(to_groupby),
                                 {1.0: idx[[0, 5]], 2.0: idx[[1, 4]]})

            to_groupby = Index([datetime(2011, 11, 1),
                                datetime(2011, 12, 1),
                                pd.NaT,
                                pd.NaT,
                                datetime(2011, 12, 1),
                                datetime(2011, 11, 1)],
                               tz='UTC').values

            ex_keys = [Timestamp('2011-11-01'), Timestamp('2011-12-01')]
            expected = {ex_keys[0]: idx[[0, 5]],
                        ex_keys[1]: idx[[1, 4]]}
            tm.assert_dict_equal(idx.groupby(to_groupby), expected) 
Example #8
Source File: test_grouping.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_multiindex_columns_empty_level(self):
        lst = [['count', 'values'], ['to filter', '']]
        midx = MultiIndex.from_tuples(lst)

        df = DataFrame([[long(1), 'A']], columns=midx)

        grouped = df.groupby('to filter').groups
        assert grouped['A'] == [0]

        grouped = df.groupby([('to filter', '')]).groups
        assert grouped['A'] == [0]

        df = DataFrame([[long(1), 'A'], [long(2), 'B']], columns=midx)

        expected = df.groupby('to filter').groups
        result = df.groupby([('to filter', '')]).groups
        assert result == expected

        df = DataFrame([[long(1), 'A'], [long(2), 'A']], columns=midx)

        expected = df.groupby('to filter').groups
        result = df.groupby([('to filter', '')]).groups
        tm.assert_dict_equal(result, expected) 
Example #9
Source File: test_grouping.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def test_groupby_multiindex_tuple(self):
        # GH 17979
        df = pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]],
                          columns=pd.MultiIndex.from_arrays(
                              [['a', 'b', 'b', 'c'],
                               [1, 1, 2, 2]]))
        expected = df.groupby([('b', 1)]).groups
        result = df.groupby(('b', 1)).groups
        tm.assert_dict_equal(expected, result)

        df2 = pd.DataFrame(df.values,
                           columns=pd.MultiIndex.from_arrays(
                               [['a', 'b', 'b', 'c'],
                                ['d', 'd', 'e', 'e']]))
        expected = df2.groupby([('b', 'd')]).groups
        result = df.groupby(('b', 1)).groups
        tm.assert_dict_equal(expected, result)

        df3 = pd.DataFrame(df.values,
                           columns=[('a', 'd'), ('b', 'd'), ('b', 'e'), 'c'])
        expected = df3.groupby([('b', 'd')]).groups
        result = df.groupby(('b', 1)).groups
        tm.assert_dict_equal(expected, result) 
Example #10
Source File: test_numeric.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_index_groupby(self):
        int_idx = Index(range(6))
        float_idx = Index(np.arange(0, 0.6, 0.1))
        obj_idx = Index('A B C D E F'.split())
        dt_idx = pd.date_range('2013-01-01', freq='M', periods=6)

        for idx in [int_idx, float_idx, obj_idx, dt_idx]:
            to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1])
            tm.assert_dict_equal(idx.groupby(to_groupby),
                                 {1.0: idx[[0, 5]], 2.0: idx[[1, 4]]})

            to_groupby = Index([datetime(2011, 11, 1),
                                datetime(2011, 12, 1),
                                pd.NaT,
                                pd.NaT,
                                datetime(2011, 12, 1),
                                datetime(2011, 11, 1)],
                               tz='UTC').values

            ex_keys = [Timestamp('2011-11-01'), Timestamp('2011-12-01')]
            expected = {ex_keys[0]: idx[[0, 5]],
                        ex_keys[1]: idx[[1, 4]]}
            tm.assert_dict_equal(idx.groupby(to_groupby), expected) 
Example #11
Source File: test_categorical.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_observed_groups(observed):
    # gh-20583
    # test that we have the appropriate groups

    cat = pd.Categorical(['a', 'c', 'a'], categories=['a', 'b', 'c'])
    df = pd.DataFrame({'cat': cat, 'vals': [1, 2, 3]})
    g = df.groupby('cat', observed=observed)

    result = g.groups
    if observed:
        expected = {'a': Index([0, 2], dtype='int64'),
                    'c': Index([1], dtype='int64')}
    else:
        expected = {'a': Index([0, 2], dtype='int64'),
                    'b': Index([], dtype='int64'),
                    'c': Index([1], dtype='int64')}

    tm.assert_dict_equal(result, expected) 
Example #12
Source File: test_grouping.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_multiindex_columns_empty_level(self):
        lst = [['count', 'values'], ['to filter', '']]
        midx = MultiIndex.from_tuples(lst)

        df = DataFrame([[long(1), 'A']], columns=midx)

        grouped = df.groupby('to filter').groups
        assert grouped['A'] == [0]

        grouped = df.groupby([('to filter', '')]).groups
        assert grouped['A'] == [0]

        df = DataFrame([[long(1), 'A'], [long(2), 'B']], columns=midx)

        expected = df.groupby('to filter').groups
        result = df.groupby([('to filter', '')]).groups
        assert result == expected

        df = DataFrame([[long(1), 'A'], [long(2), 'A']], columns=midx)

        expected = df.groupby('to filter').groups
        result = df.groupby([('to filter', '')]).groups
        tm.assert_dict_equal(result, expected) 
Example #13
Source File: test_grouping.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_groupby_multiindex_tuple(self):
        # GH 17979
        df = pd.DataFrame([[1, 2, 3, 4], [3, 4, 5, 6], [1, 4, 2, 3]],
                          columns=pd.MultiIndex.from_arrays(
                              [['a', 'b', 'b', 'c'],
                               [1, 1, 2, 2]]))
        expected = df.groupby([('b', 1)]).groups
        result = df.groupby(('b', 1)).groups
        tm.assert_dict_equal(expected, result)

        df2 = pd.DataFrame(df.values,
                           columns=pd.MultiIndex.from_arrays(
                               [['a', 'b', 'b', 'c'],
                                ['d', 'd', 'e', 'e']]))
        expected = df2.groupby([('b', 'd')]).groups
        result = df.groupby(('b', 1)).groups
        tm.assert_dict_equal(expected, result)

        df3 = pd.DataFrame(df.values,
                           columns=[('a', 'd'), ('b', 'd'), ('b', 'e'), 'c'])
        expected = df3.groupby([('b', 'd')]).groups
        result = df.groupby(('b', 1)).groups
        tm.assert_dict_equal(expected, result) 
Example #14
Source File: test_grouping.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def test_list_grouper_with_nat(self):
        # GH 14715
        df = pd.DataFrame({'date': pd.date_range('1/1/2011',
                                                 periods=365, freq='D')})
        df.iloc[-1] = pd.NaT
        grouper = pd.Grouper(key='date', freq='AS')

        # Grouper in a list grouping
        result = df.groupby([grouper])
        expected = {pd.Timestamp('2011-01-01'): pd.Index(list(range(364)))}
        tm.assert_dict_equal(result.groups, expected)

        # Test case without a list
        result = df.groupby(grouper)
        expected = {pd.Timestamp('2011-01-01'): 365}
        tm.assert_dict_equal(result.groups, expected)


# get_group
# -------------------------------- 
Example #15
Source File: test_numeric.py    From elasticintel with GNU General Public License v3.0 6 votes vote down vote up
def test_index_groupby(self):
        int_idx = Index(range(6))
        float_idx = Index(np.arange(0, 0.6, 0.1))
        obj_idx = Index('A B C D E F'.split())
        dt_idx = pd.date_range('2013-01-01', freq='M', periods=6)

        for idx in [int_idx, float_idx, obj_idx, dt_idx]:
            to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1])
            tm.assert_dict_equal(idx.groupby(to_groupby),
                                 {1.0: idx[[0, 5]], 2.0: idx[[1, 4]]})

            to_groupby = Index([datetime(2011, 11, 1),
                                datetime(2011, 12, 1),
                                pd.NaT,
                                pd.NaT,
                                datetime(2011, 12, 1),
                                datetime(2011, 11, 1)],
                               tz='UTC').values

            ex_keys = [Timestamp('2011-11-01'), Timestamp('2011-12-01')]
            expected = {ex_keys[0]: idx[[0, 5]],
                        ex_keys[1]: idx[[1, 4]]}
            tm.assert_dict_equal(idx.groupby(to_groupby), expected) 
Example #16
Source File: test_numeric.py    From coffeegrindsize with MIT License 6 votes vote down vote up
def test_index_groupby(self):
        int_idx = Index(range(6))
        float_idx = Index(np.arange(0, 0.6, 0.1))
        obj_idx = Index('A B C D E F'.split())
        dt_idx = pd.date_range('2013-01-01', freq='M', periods=6)

        for idx in [int_idx, float_idx, obj_idx, dt_idx]:
            to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1])
            tm.assert_dict_equal(idx.groupby(to_groupby),
                                 {1.0: idx[[0, 5]], 2.0: idx[[1, 4]]})

            to_groupby = Index([datetime(2011, 11, 1),
                                datetime(2011, 12, 1),
                                pd.NaT,
                                pd.NaT,
                                datetime(2011, 12, 1),
                                datetime(2011, 11, 1)],
                               tz='UTC').values

            ex_keys = [Timestamp('2011-11-01'), Timestamp('2011-12-01')]
            expected = {ex_keys[0]: idx[[0, 5]],
                        ex_keys[1]: idx[[1, 4]]}
            tm.assert_dict_equal(idx.groupby(to_groupby), expected) 
Example #17
Source File: test_numeric.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_index_groupby(self):
        int_idx = Index(range(6))
        float_idx = Index(np.arange(0, 0.6, 0.1))
        obj_idx = Index('A B C D E F'.split())
        dt_idx = pd.date_range('2013-01-01', freq='M', periods=6)

        for idx in [int_idx, float_idx, obj_idx, dt_idx]:
            to_groupby = np.array([1, 2, np.nan, np.nan, 2, 1])
            tm.assert_dict_equal(idx.groupby(to_groupby),
                                 {1.0: idx[[0, 5]], 2.0: idx[[1, 4]]})

            to_groupby = Index([datetime(2011, 11, 1),
                                datetime(2011, 12, 1),
                                pd.NaT,
                                pd.NaT,
                                datetime(2011, 12, 1),
                                datetime(2011, 11, 1)],
                               tz='UTC').values

            ex_keys = [Timestamp('2011-11-01'), Timestamp('2011-12-01')]
            expected = {ex_keys[0]: idx[[0, 5]],
                        ex_keys[1]: idx[[1, 4]]}
            tm.assert_dict_equal(idx.groupby(to_groupby), expected) 
Example #18
Source File: test_categorical.py    From twitter-stock-recommendation with MIT License 6 votes vote down vote up
def test_observed_groups(observed):
    # gh-20583
    # test that we have the appropriate groups

    cat = pd.Categorical(['a', 'c', 'a'], categories=['a', 'b', 'c'])
    df = pd.DataFrame({'cat': cat, 'vals': [1, 2, 3]})
    g = df.groupby('cat', observed=observed)

    result = g.groups
    if observed:
        expected = {'a': Index([0, 2], dtype='int64'),
                    'c': Index([1], dtype='int64')}
    else:
        expected = {'a': Index([0, 2], dtype='int64'),
                    'b': Index([], dtype='int64'),
                    'c': Index([1], dtype='int64')}

    tm.assert_dict_equal(result, expected) 
Example #19
Source File: test_events.py    From outrigger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test__mutually_exclusive_exon(self, splice_graph, exon1_i, exon1_name,
                                      mutually_exclusive_events):
        test = splice_graph._mutually_exclusive_exon(exon1_i, exon1_name)
        true = mutually_exclusive_events
        pdt.assert_dict_equal(test, true) 
Example #20
Source File: test_adjacencies.py    From outrigger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test__junctions_genome_adjacent_to_exon(self, adjacencies, snap25_exon,
                                                adjacent_in_genome):
        true = adjacent_in_genome
        test = adjacencies._junctions_genome_adjacent_to_exon(snap25_exon)

        pdt.assert_dict_equal(test, true) 
Example #21
Source File: test_packers.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_dict_numpy_complex(self):
        x = {'foo': np.complex128(1.0 + 1.0j),
             'bar': np.complex128(2.0 + 2.0j)}
        x_rec = self.encode_decode(x)
        tm.assert_dict_equal(x, x_rec)

        for key in x:
            tm.assert_class_equal(x[key], x_rec[key], obj="numpy complex128") 
Example #22
Source File: test_packers.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_dict_complex(self):
        x = {'foo': 1.0 + 1.0j, 'bar': 2.0 + 2.0j}
        x_rec = self.encode_decode(x)
        tm.assert_dict_equal(x, x_rec)

        for key in x:
            tm.assert_class_equal(x[key], x_rec[key], obj="complex value") 
Example #23
Source File: test_na_values.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def test_na_values_dict_aliasing(all_parsers):
    parser = all_parsers
    na_values = {"a": 2, "b": 1}
    na_values_copy = na_values.copy()

    names = ["a", "b"]
    data = "1,2\n2,1"

    expected = DataFrame([[1.0, 2.0], [np.nan, np.nan]], columns=names)
    result = parser.read_csv(StringIO(data), names=names, na_values=na_values)

    tm.assert_frame_equal(result, expected)
    tm.assert_dict_equal(na_values, na_values_copy) 
Example #24
Source File: test_events.py    From outrigger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test__skipped_exon(self, splice_graph, exon1_i, exon1_name,
                           skipped_exon_events):
        test = splice_graph._skipped_exon(exon1_i, exon1_name)
        true = skipped_exon_events
        pdt.assert_dict_equal(test, true) 
Example #25
Source File: test_bam.py    From outrigger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test__report_read_positions(bamfile):
    from outrigger.io.bam import _report_read_positions

    bam = pysam.AlignmentFile(bamfile, 'rb')

    test = collections.Counter()

    for read in bam:
        _report_read_positions(read, test)
        break
    bam.close()

    true = {('chr2', 136713559, 136713559, '+'): 1}
    pdt.assert_dict_equal(test, true) 
Example #26
Source File: test_multi.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_groupby(self):
        groups = self.index.groupby(np.array([1, 1, 1, 2, 2, 2]))
        labels = self.index.get_values().tolist()
        exp = {1: labels[:3], 2: labels[3:]}
        tm.assert_dict_equal(groups, exp)

        # GH5620
        groups = self.index.groupby(self.index)
        exp = dict((key, [key]) for key in self.index)
        tm.assert_dict_equal(groups, exp) 
Example #27
Source File: test_base.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_groupby(self):
        idx = Index(range(5))
        groups = idx.groupby(np.array([1, 1, 2, 2, 2]))
        exp = {1: pd.Index([0, 1]), 2: pd.Index([2, 3, 4])}
        tm.assert_dict_equal(groups, exp) 
Example #28
Source File: test_eval.py    From recruit with Apache License 2.0 5 votes vote down vote up
def test_query_inplace(self):
        # see gh-11149
        df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
        expected = df.copy()
        expected = expected[expected['a'] == 2]
        df.query('a == 2', inplace=True)
        assert_frame_equal(expected, df)

        df = {}
        expected = {"a": 3}

        self.eval("a = 1 + 2", target=df, inplace=True)
        tm.assert_dict_equal(df, expected) 
Example #29
Source File: test_eval.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def test_query_inplace(self):
        # see gh-11149
        df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
        expected = df.copy()
        expected = expected[expected['a'] == 2]
        df.query('a == 2', inplace=True)
        assert_frame_equal(expected, df)

        df = {}
        expected = {"a": 3}

        self.eval("a = 1 + 2", target=df, inplace=True)
        tm.assert_dict_equal(df, expected) 
Example #30
Source File: test_events.py    From outrigger with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_single_exon_alternative_events(self, splice_graph, exon1_i,
                                            exon1_name,
                                            mutually_exclusive_events,
                                            skipped_exon_events):
        test = splice_graph.single_exon_alternative_events(
            exon1_i, exon1_name)
        true = {'se': skipped_exon_events, 'mxe': mutually_exclusive_events}
        pdt.assert_dict_equal(test, true)