pandas.util.testing.assert_numpy_array_equal - python examples

Here are the examples of the python api pandas.util.testing.assert_numpy_array_equal taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

145 Examples 7

0 View Complete Implementation : test_base.py
Copyright Apache License 2.0
Author : Frank-qlu
    def test_factorize(self):
        for orig in self.objs:
            o = orig.copy()

            if isinstance(o, Index) and o.is_boolean():
                exp_arr = np.array([0, 1] + [0] * 8, dtype=np.intp)
                exp_uniques = o
                exp_uniques = Index([False, True])
            else:
                exp_arr = np.array(range(len(o)), dtype=np.intp)
                exp_uniques = o
            labels, uniques = o.factorize()

            tm.astert_numpy_array_equal(labels, exp_arr)
            if isinstance(o, Series):
                tm.astert_index_equal(uniques, Index(orig),
                                      check_names=False)
            else:
                # factorize explicitly resets name
                tm.astert_index_equal(uniques, exp_uniques,
                                      check_names=False)

0 View Complete Implementation : test_assert_numpy_array_equal.py
Copyright Apache License 2.0
Author : Frank-qlu
@pytest.mark.parametrize("other_type", ["same", "copy"])
@pytest.mark.parametrize("check_same", ["same", "copy"])
def test_numpy_array_equal_copy_flag(other_type, check_same):
    a = np.array([1, 2, 3])
    msg = None

    if other_type == "same":
        other = a.view()
    else:
        other = a.copy()

    if check_same != other_type:
        msg = (r"array\(\[1, 2, 3\]\) is not array\(\[1, 2, 3\]\)"
               if check_same == "same"
               else r"array\(\[1, 2, 3\]\) is array\(\[1, 2, 3\]\)")

    if msg is not None:
        with pytest.raises(astertionError, match=msg):
            astert_numpy_array_equal(a, other, check_same=check_same)
    else:
        astert_numpy_array_equal(a, other, check_same=check_same)

0 View Complete Implementation : test_ops.py
Copyright Apache License 2.0
Author : Frank-qlu
    @pytest.mark.parametrize('freq', ['D', 'M'])
    def test_equals(self, freq):
        # GH#13107
        idx = pd.PeriodIndex(['2011-01-01', '2011-01-02', 'NaT'],
                             freq=freq)
        astert idx.equals(idx)
        astert idx.equals(idx.copy())
        astert idx.equals(idx.astype(object))
        astert idx.astype(object).equals(idx)
        astert idx.astype(object).equals(idx.astype(object))
        astert not idx.equals(list(idx))
        astert not idx.equals(pd.Series(idx))

        idx2 = pd.PeriodIndex(['2011-01-01', '2011-01-02', 'NaT'],
                              freq='H')
        astert not idx.equals(idx2)
        astert not idx.equals(idx2.copy())
        astert not idx.equals(idx2.astype(object))
        astert not idx.astype(object).equals(idx2)
        astert not idx.equals(list(idx2))
        astert not idx.equals(pd.Series(idx2))

        # same internal, different tz
        idx3 = pd.PeriodIndex._simple_new(
            idx._values._simple_new(idx._values.asi8, freq="H")
        )
        tm.astert_numpy_array_equal(idx.asi8, idx3.asi8)
        astert not idx.equals(idx3)
        astert not idx.equals(idx3.copy())
        astert not idx.equals(idx3.astype(object))
        astert not idx.astype(object).equals(idx3)
        astert not idx.equals(list(idx3))
        astert not idx.equals(pd.Series(idx3))

0 View Complete Implementation : test_constructors.py
Copyright Apache License 2.0
Author : Frank-qlu
    def test_categorical_sideeffects_free(self):
        # Pasting a categorical to a Series and then changing values in either
        # the series or the categorical should not change the values in the
        # other one, IF you specify copy!
        cat = Categorical(["a", "b", "c", "a"])
        s = Series(cat, copy=True)
        astert s.cat is not cat
        s.cat.categories = [1, 2, 3]
        exp_s = np.array([1, 2, 3, 1], dtype=np.int64)
        exp_cat = np.array(["a", "b", "c", "a"], dtype=np.object_)
        tm.astert_numpy_array_equal(s.__array__(), exp_s)
        tm.astert_numpy_array_equal(cat.__array__(), exp_cat)

        # setting
        s[0] = 2
        exp_s2 = np.array([2, 2, 3, 1], dtype=np.int64)
        tm.astert_numpy_array_equal(s.__array__(), exp_s2)
        tm.astert_numpy_array_equal(cat.__array__(), exp_cat)

        # however, copy is False by default
        # so this WILL change values
        cat = Categorical(["a", "b", "c", "a"])
        s = Series(cat)
        astert s.values is cat
        s.cat.categories = [1, 2, 3]
        exp_s = np.array([1, 2, 3, 1], dtype=np.int64)
        tm.astert_numpy_array_equal(s.__array__(), exp_s)
        tm.astert_numpy_array_equal(cat.__array__(), exp_s)

        s[0] = 2
        exp_s2 = np.array([2, 2, 3, 1], dtype=np.int64)
        tm.astert_numpy_array_equal(s.__array__(), exp_s2)
        tm.astert_numpy_array_equal(cat.__array__(), exp_s2)

0 View Complete Implementation : test_libsparse.py
Copyright Apache License 2.0
Author : Frank-qlu
    def _op_tests(self, sparse_op, python_op):
        def _check_case(xloc, xlen, yloc, ylen, eloc, elen):
            xindex = BlockIndex(TEST_LENGTH, xloc, xlen)
            yindex = BlockIndex(TEST_LENGTH, yloc, ylen)

            xdindex = xindex.to_int_index()
            ydindex = yindex.to_int_index()

            x = np.arange(xindex.npoints) * 10. + 1
            y = np.arange(yindex.npoints) * 100. + 1

            xfill = 0
            yfill = 2

            result_block_vals, rb_index, bfill = sparse_op(x, xindex, xfill, y,
                                                           yindex, yfill)
            result_int_vals, ri_index, ifill = sparse_op(x, xdindex, xfill, y,
                                                         ydindex, yfill)

            astert rb_index.to_int_index().equals(ri_index)
            tm.astert_numpy_array_equal(result_block_vals, result_int_vals)
            astert bfill == ifill

            # check versus Series...
            xseries = Series(x, xdindex.indices)
            xseries = xseries.reindex(np.arange(TEST_LENGTH)).fillna(xfill)

            yseries = Series(y, ydindex.indices)
            yseries = yseries.reindex(np.arange(TEST_LENGTH)).fillna(yfill)

            series_result = python_op(xseries, yseries)
            series_result = series_result.reindex(ri_index.indices)

            tm.astert_numpy_array_equal(result_block_vals,
                                        series_result.values)
            tm.astert_numpy_array_equal(result_int_vals, series_result.values)

        check_cases(_check_case)

0 View Complete Implementation : test_category.py
Copyright Apache License 2.0
Author : Frank-qlu
    def test_reindexing(self):
        np.random.seed(123456789)

        ci = self.create_index()
        oidx = Index(np.array(ci))

        for n in [1, 2, 5, len(ci)]:
            finder = oidx[np.random.randint(0, len(ci), size=n)]
            expected = oidx.get_indexer_non_unique(finder)[0]

            actual = ci.get_indexer(finder)
            tm.astert_numpy_array_equal(expected, actual)

        # see gh-17323
        #
        # Even when indexer is equal to the
        # members in the index, we should
        # respect duplicates instead of taking
        # the fast-track path.
        for finder in [list("aabbca"), list("aababca")]:
            expected = oidx.get_indexer_non_unique(finder)[0]

            actual = ci.get_indexer(finder)
            tm.astert_numpy_array_equal(expected, actual)

0 View Complete Implementation : test_internals.py
Copyright Apache License 2.0
Author : Frank-qlu
    def test_delete(self):
        newb = self.fblock.copy()
        newb.delete(0)
        astert isinstance(newb.mgr_locs, BlockPlacement)
        tm.astert_numpy_array_equal(newb.mgr_locs.as_array,
                                    np.array([2, 4], dtype=np.int64))
        astert (newb.values[0] == 1).all()

        newb = self.fblock.copy()
        newb.delete(1)
        astert isinstance(newb.mgr_locs, BlockPlacement)
        tm.astert_numpy_array_equal(newb.mgr_locs.as_array,
                                    np.array([0, 4], dtype=np.int64))
        astert (newb.values[1] == 2).all()

        newb = self.fblock.copy()
        newb.delete(2)
        tm.astert_numpy_array_equal(newb.mgr_locs.as_array,
                                    np.array([0, 2], dtype=np.int64))
        astert (newb.values[1] == 1).all()

        newb = self.fblock.copy()
        with pytest.raises(Exception):
            newb.delete(3)

0 View Complete Implementation : test_join.py
Copyright Apache License 2.0
Author : Frank-qlu
def test_outer_join_indexer():
    a = np.array([1, 2, 3, 4, 5], dtype=np.int64)
    b = np.array([0, 3, 5, 7, 9], dtype=np.int64)

    index, ares, bres = _join.outer_join_indexer_int64(a, b)

    index_exp = np.array([0, 1, 2, 3, 4, 5, 7, 9], dtype=np.int64)
    astert_almost_equal(index, index_exp)

    aexp = np.array([-1, 0, 1, 2, 3, 4, -1, -1], dtype=np.int64)
    bexp = np.array([0, -1, -1, 1, -1, 2, 3, 4], dtype=np.int64)
    astert_almost_equal(ares, aexp)
    astert_almost_equal(bres, bexp)

    a = np.array([5], dtype=np.int64)
    b = np.array([5], dtype=np.int64)

    index, ares, bres = _join.outer_join_indexer_int64(a, b)
    tm.astert_numpy_array_equal(index, np.array([5], dtype=np.int64))
    tm.astert_numpy_array_equal(ares, np.array([0], dtype=np.int64))
    tm.astert_numpy_array_equal(bres, np.array([0], dtype=np.int64))

0 View Complete Implementation : test_array_to_datetime.py
Copyright Apache License 2.0
Author : Frank-qlu
@pytest.mark.parametrize("errors", ["ignore", "coerce"])
def test_coerce_of_invalid_datetimes(errors):
    arr = np.array(["01-01-2013", "not_a_date", "1"], dtype=object)
    kwargs = dict(values=arr, errors=errors)

    if errors == "ignore":
        # Without coercing, the presence of any invalid
        # dates prevents any values from being converted.
        result, _ = tslib.array_to_datetime(**kwargs)
        tm.astert_numpy_array_equal(result, arr)
    else:  # coerce.
        # With coercing, the invalid dates becomes iNaT
        result, _ = tslib.array_to_datetime(arr, errors="coerce")
        expected = ["2013-01-01T00:00:00.000000000-0000",
                    iNaT,
                    iNaT]

        tm.astert_numpy_array_equal(
            result,
            np_array_datetime64_compat(expected, dtype="M8[ns]"))

0 View Complete Implementation : test_base.py
Copyright Apache License 2.0
Author : Frank-qlu
    def test_factorize_repeated(self):
        for orig in self.objs:
            o = orig.copy()

            # don't test boolean
            if isinstance(o, Index) and o.is_boolean():
                continue

            # sort by value, and create duplicates
            if isinstance(o, Series):
                o = o.sort_values()
                n = o.iloc[5:].append(o)
            else:
                indexer = o.argsort()
                o = o.take(indexer)
                n = o[5:].append(o)

            exp_arr = np.array([5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
                               dtype=np.intp)
            labels, uniques = n.factorize(sort=True)

            tm.astert_numpy_array_equal(labels, exp_arr)
            if isinstance(o, Series):
                tm.astert_index_equal(uniques, Index(orig).sort_values(),
                                      check_names=False)
            else:
                tm.astert_index_equal(uniques, o, check_names=False)

            exp_arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4],
                               np.intp)
            labels, uniques = n.factorize(sort=False)
            tm.astert_numpy_array_equal(labels, exp_arr)

            if isinstance(o, Series):
                expected = Index(o.iloc[5:10].append(o.iloc[:5]))
                tm.astert_index_equal(uniques, expected, check_names=False)
            else:
                expected = o[5:10].append(o[:5])
                tm.astert_index_equal(uniques, expected, check_names=False)