예제 #1
0
    def test_indexes_support_numpy_like_take_by(self):
        """ Verifies numpy_like.take can handle SDC index types as indices """

        from sdc.functions import numpy_like

        def pyfunc(arr, index):
            return np.take(arr, index)

        @self.jit
        def sdc_func(arr, index):
            return numpy_like.take(arr, index)

        n, k = 1000, 200
        np.random.seed(0)
        arr = np.arange(n) * 2
        indexes_to_test = [
            get_sample_index(k, PositionalIndexType),
            get_sample_index(k, RangeIndexType),
            get_sample_index(k, Int64IndexType),
        ]
        for index in indexes_to_test:
            with self.subTest(index=index):
                result = sdc_func(arr, index)
                result_ref = pyfunc(arr, index)
                np.testing.assert_array_equal(result, result_ref)
예제 #2
0
    def test_positional_index_append(self):
        def test_impl(index, other):
            return index.append(other)
        sdc_func = self.jit(test_impl)

        n = 11
        other_indexes = [
            get_sample_index(n, PositionalIndexType),
            get_sample_index(n, RangeIndexType),
            get_sample_index(n, Int64IndexType),
        ]
        for index, other in product(
                _generate_positional_indexes_fixed(n),
                other_indexes):
            with self.subTest(index=index, other=other):
                result = sdc_func(index, other)
                result_ref = test_impl(index, other)
                pd.testing.assert_index_equal(result, result_ref)
예제 #3
0
    def test_multi_index_nparray(self):
        def test_impl(index):
            return np.array(index)
        sdc_func = self.jit(test_impl)

        n = 11
        index = get_sample_index(n, MultiIndexType)
        result = sdc_func(index)
        result_ref = test_impl(index)
        np.testing.assert_array_equal(result, result_ref)
예제 #4
0
    def test_multi_index_ravel(self):
        def test_impl(index):
            return index.ravel()
        sdc_func = self.jit(test_impl)

        n = 11
        index = get_sample_index(n, MultiIndexType)
        result = sdc_func(index)
        result_ref = test_impl(index)
        # SDC MultiIndex.values return list but not numpy array
        np.testing.assert_array_equal(result, list(result_ref))
예제 #5
0
    def test_positional_index_join(self):
        def test_impl(index, other):
            return index.join(other, 'outer', return_indexers=True)
        sdc_func = self.jit(test_impl)

        n = 11
        other_indexes = [
            get_sample_index(2 * n, PositionalIndexType),
            get_sample_index(2 * n, RangeIndexType),
            get_sample_index(2 * n, Int64IndexType),
        ]
        for index, other in product(
                _generate_positional_indexes_fixed(n),
                other_indexes):
            with self.subTest(index=index, other=other):
                result = sdc_func(index, other)
                result_ref = test_impl(index, other)
                # check_names=False, since pandas behavior is not type-stable
                pd.testing.assert_index_equal(result[0], result_ref[0], check_names=False)
                np.testing.assert_array_equal(result[1], result_ref[1])
                np.testing.assert_array_equal(result[2], result_ref[2])
예제 #6
0
    def test_multi_index_iterator_1(self):
        def test_impl(index):
            res = []
            for i, label in enumerate(index):
                res.append((i, label))
            return res
        sdc_func = self.jit(test_impl)

        n = 11
        index = get_sample_index(n, MultiIndexType)
        result = sdc_func(index)
        result_ref = test_impl(index)
        self.assertEqual(result, result_ref)
예제 #7
0
    def test_multi_index_getitem_scalar(self):
        def test_impl(index, idx):
            return index[idx]
        sdc_func = self.jit(test_impl)

        n = 11
        index = get_sample_index(n, MultiIndexType)
        idxs_to_test = [0, n // 2, n - 1, -1]
        for idx in idxs_to_test:
            with self.subTest(idx=idx):
                result = sdc_func(index, idx)
                result_ref = test_impl(index, idx)
                self.assertEqual(result, result_ref)
예제 #8
0
    def test_multi_index_contains(self):
        def test_impl(index, value):
            return value in index
        sdc_func = self.jit(test_impl)

        n = 11
        index = get_sample_index(n, MultiIndexType)
        values_to_test = [('a', 1), ('a', 4), ('e', 1), ('x', 5)]
        for value in values_to_test:
            with self.subTest(value=value):
                result = sdc_func(index, value)
                result_ref = test_impl(index, value)
                np.testing.assert_array_equal(result, result_ref)
예제 #9
0
    def test_multi_index_attribute_name(self):
        def test_impl(index):
            return index.name
        sdc_func = self.jit(test_impl)

        n = 11
        index = get_sample_index(n, MultiIndexType)
        for name in test_global_index_names:
            index.name = name
            with self.subTest(name=name):
                result = sdc_func(index)
                result_ref = test_impl(index)
                self.assertEqual(result, result_ref)
예제 #10
0
    def test_multi_index_attribute_dtype(self):
        from numba.typed import List

        # index dtype cannot be returned (boxed), thus it only checks it can be used
        def test_impl(index):
            return List.empty_list(index.dtype)
        sdc_func = self.jit(test_impl)

        n = 11
        index = get_sample_index(n, MultiIndexType)
        result = sdc_func(index)
        expected = types.Tuple.from_types([types.unicode_type, types.intp])
        self.assertEqual(result._dtype, expected)
예제 #11
0
    def test_multi_index_reindex_equal_indexes(self):

        def test_func(index1, index2):
            return index1.reindex(index2)
        sdc_func = self.jit(test_func)

        n = 10
        index1 = get_sample_index(n, MultiIndexType)
        index2 = index1.copy(deep=True)

        result = sdc_func(index1, index2)
        result_ref = test_func(index1, index2)
        pd.testing.assert_index_equal(result[0], result_ref[0])
        np.testing.assert_array_equal(result[1], result_ref[1])
예제 #12
0
    def test_multi_index_iterator_2(self):
        def test_impl(index):
            res = []
            for label in index:
                str_part, _ = label
                if str_part == 'a':
                    res.append(label)
            return res
        sdc_func = self.jit(test_impl)

        n = 11
        index = get_sample_index(n, MultiIndexType)
        result = sdc_func(index)
        result_ref = test_impl(index)
        self.assertEqual(result, result_ref)
예제 #13
0
    def test_multi_index_operator_eq_scalar(self):
        def test_impl(A, B):
            return A == B
        sdc_func = self.jit(test_impl)

        n = 11
        A = get_sample_index(n, MultiIndexType)
        scalars_to_test = [('a', 1), ('a', 4), ('e', 1), ('x', 5)]
        for B in scalars_to_test:
            for swap_operands in (False, True):
                if swap_operands:
                    A, B = B, A
                with self.subTest(left=A, right=B):
                    result = np.asarray(sdc_func(A, B))  # FIXME_Numba#5157: remove np.asarray
                    result_ref = test_impl(A, B)
                    np.testing.assert_array_equal(result, result_ref)
예제 #14
0
    def test_multi_index_getitem_slice(self):
        def test_impl(index, idx):
            return index[idx]
        sdc_func = self.jit(test_impl)

        n = 17
        index = get_sample_index(n, MultiIndexType)
        slices_params = combinations_with_replacement(
            [None, 0, -1, n // 2, n, n - 3, n + 3, -(n + 3)],
            2
        )

        for slice_start, slice_stop in slices_params:
            for slice_step in [1, -1, 2]:
                idx = slice(slice_start, slice_stop, slice_step)
                with self.subTest(idx=idx):
                    result = sdc_func(index, idx)
                    result_ref = test_impl(index, idx)
                    pd.testing.assert_index_equal(result, result_ref)
예제 #15
0
    def test_multi_index_getitem_scalar_idx_bounds(self):
        def test_impl(index, idx):
            return index[idx]
        sdc_func = self.jit(test_impl)

        n = 11
        index = get_sample_index(n, MultiIndexType)
        idxs_to_test = [-(n + 1), n]
        for idx in idxs_to_test:
            with self.subTest(idx=idx):
                with self.assertRaises(Exception) as context:
                    test_impl(index, idx)
                pandas_exception = context.exception

                with self.assertRaises(type(pandas_exception)) as context:
                    sdc_func(index, idx)
                sdc_exception = context.exception
                self.assertIsInstance(sdc_exception, type(pandas_exception))
                self.assertIn("out of bounds", str(sdc_exception))