8th day of python challenges 111-117
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
import pytest
|
||||
|
||||
|
||||
@pytest.fixture(params=[True, False])
|
||||
def check_dtype(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[True, False])
|
||||
def check_exact(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[True, False])
|
||||
def check_index_type(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[True, False])
|
||||
def check_less_precise(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[True, False])
|
||||
def check_categorical(request):
|
||||
return request.param
|
@@ -0,0 +1,364 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import DataFrame, Index, Series, Timestamp
|
||||
from pandas.util.testing import assert_almost_equal
|
||||
|
||||
|
||||
def _assert_almost_equal_both(a, b, **kwargs):
|
||||
"""
|
||||
Check that two objects are approximately equal.
|
||||
|
||||
This check is performed commutatively.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
a : object
|
||||
The first object to compare.
|
||||
b : object
|
||||
The second object to compare.
|
||||
kwargs : dict
|
||||
The arguments passed to `assert_almost_equal`.
|
||||
"""
|
||||
assert_almost_equal(a, b, **kwargs)
|
||||
assert_almost_equal(b, a, **kwargs)
|
||||
|
||||
|
||||
def _assert_not_almost_equal(a, b, **kwargs):
|
||||
"""
|
||||
Check that two objects are not approximately equal.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
a : object
|
||||
The first object to compare.
|
||||
b : object
|
||||
The second object to compare.
|
||||
kwargs : dict
|
||||
The arguments passed to `assert_almost_equal`.
|
||||
"""
|
||||
try:
|
||||
assert_almost_equal(a, b, **kwargs)
|
||||
msg = (
|
||||
"{a} and {b} were approximately equal when they shouldn't have been"
|
||||
).format(a=a, b=b)
|
||||
pytest.fail(msg=msg)
|
||||
except AssertionError:
|
||||
pass
|
||||
|
||||
|
||||
def _assert_not_almost_equal_both(a, b, **kwargs):
|
||||
"""
|
||||
Check that two objects are not approximately equal.
|
||||
|
||||
This check is performed commutatively.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
a : object
|
||||
The first object to compare.
|
||||
b : object
|
||||
The second object to compare.
|
||||
kwargs : dict
|
||||
The arguments passed to `tm.assert_almost_equal`.
|
||||
"""
|
||||
_assert_not_almost_equal(a, b, **kwargs)
|
||||
_assert_not_almost_equal(b, a, **kwargs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"a,b",
|
||||
[
|
||||
(1.1, 1.1),
|
||||
(1.1, 1.100001),
|
||||
(np.int16(1), 1.000001),
|
||||
(np.float64(1.1), 1.1),
|
||||
(np.uint32(5), 5),
|
||||
],
|
||||
)
|
||||
def test_assert_almost_equal_numbers(a, b):
|
||||
_assert_almost_equal_both(a, b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("a,b", [(1.1, 1), (1.1, True), (1, 2), (1.0001, np.int16(1))])
|
||||
def test_assert_not_almost_equal_numbers(a, b):
|
||||
_assert_not_almost_equal_both(a, b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("a,b", [(0, 0), (0, 0.0), (0, np.float64(0)), (0.000001, 0)])
|
||||
def test_assert_almost_equal_numbers_with_zeros(a, b):
|
||||
_assert_almost_equal_both(a, b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("a,b", [(0.001, 0), (1, 0)])
|
||||
def test_assert_not_almost_equal_numbers_with_zeros(a, b):
|
||||
_assert_not_almost_equal_both(a, b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("a,b", [(1, "abc"), (1, [1]), (1, object())])
|
||||
def test_assert_not_almost_equal_numbers_with_mixed(a, b):
|
||||
_assert_not_almost_equal_both(a, b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"left_dtype", ["M8[ns]", "m8[ns]", "float64", "int64", "object"]
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"right_dtype", ["M8[ns]", "m8[ns]", "float64", "int64", "object"]
|
||||
)
|
||||
def test_assert_almost_equal_edge_case_ndarrays(left_dtype, right_dtype):
|
||||
# Empty compare.
|
||||
_assert_almost_equal_both(
|
||||
np.array([], dtype=left_dtype),
|
||||
np.array([], dtype=right_dtype),
|
||||
check_dtype=False,
|
||||
)
|
||||
|
||||
|
||||
def test_assert_almost_equal_dicts():
|
||||
_assert_almost_equal_both({"a": 1, "b": 2}, {"a": 1, "b": 2})
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"a,b",
|
||||
[
|
||||
({"a": 1, "b": 2}, {"a": 1, "b": 3}),
|
||||
({"a": 1, "b": 2}, {"a": 1, "b": 2, "c": 3}),
|
||||
({"a": 1}, 1),
|
||||
({"a": 1}, "abc"),
|
||||
({"a": 1}, [1]),
|
||||
],
|
||||
)
|
||||
def test_assert_not_almost_equal_dicts(a, b):
|
||||
_assert_not_almost_equal_both(a, b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("val", [1, 2])
|
||||
def test_assert_almost_equal_dict_like_object(val):
|
||||
dict_val = 1
|
||||
real_dict = dict(a=val)
|
||||
|
||||
class DictLikeObj:
|
||||
def keys(self):
|
||||
return ("a",)
|
||||
|
||||
def __getitem__(self, item):
|
||||
if item == "a":
|
||||
return dict_val
|
||||
|
||||
func = (
|
||||
_assert_almost_equal_both if val == dict_val else _assert_not_almost_equal_both
|
||||
)
|
||||
func(real_dict, DictLikeObj(), check_dtype=False)
|
||||
|
||||
|
||||
def test_assert_almost_equal_strings():
|
||||
_assert_almost_equal_both("abc", "abc")
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"a,b", [("abc", "abcd"), ("abc", "abd"), ("abc", 1), ("abc", [1])]
|
||||
)
|
||||
def test_assert_not_almost_equal_strings(a, b):
|
||||
_assert_not_almost_equal_both(a, b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"a,b", [([1, 2, 3], [1, 2, 3]), (np.array([1, 2, 3]), np.array([1, 2, 3]))]
|
||||
)
|
||||
def test_assert_almost_equal_iterables(a, b):
|
||||
_assert_almost_equal_both(a, b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"a,b",
|
||||
[
|
||||
# Class is different.
|
||||
(np.array([1, 2, 3]), [1, 2, 3]),
|
||||
# Dtype is different.
|
||||
(np.array([1, 2, 3]), np.array([1.0, 2.0, 3.0])),
|
||||
# Can't compare generators.
|
||||
(iter([1, 2, 3]), [1, 2, 3]),
|
||||
([1, 2, 3], [1, 2, 4]),
|
||||
([1, 2, 3], [1, 2, 3, 4]),
|
||||
([1, 2, 3], 1),
|
||||
],
|
||||
)
|
||||
def test_assert_not_almost_equal_iterables(a, b):
|
||||
_assert_not_almost_equal(a, b)
|
||||
|
||||
|
||||
def test_assert_almost_equal_null():
|
||||
_assert_almost_equal_both(None, None)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("a,b", [(None, np.NaN), (None, 0), (np.NaN, 0)])
|
||||
def test_assert_not_almost_equal_null(a, b):
|
||||
_assert_not_almost_equal(a, b)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"a,b",
|
||||
[
|
||||
(np.inf, np.inf),
|
||||
(np.inf, float("inf")),
|
||||
(np.array([np.inf, np.nan, -np.inf]), np.array([np.inf, np.nan, -np.inf])),
|
||||
(
|
||||
np.array([np.inf, None, -np.inf], dtype=np.object_),
|
||||
np.array([np.inf, np.nan, -np.inf], dtype=np.object_),
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_assert_almost_equal_inf(a, b):
|
||||
_assert_almost_equal_both(a, b)
|
||||
|
||||
|
||||
def test_assert_not_almost_equal_inf():
|
||||
_assert_not_almost_equal_both(np.inf, 0)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"a,b",
|
||||
[
|
||||
(Index([1.0, 1.1]), Index([1.0, 1.100001])),
|
||||
(Series([1.0, 1.1]), Series([1.0, 1.100001])),
|
||||
(np.array([1.1, 2.000001]), np.array([1.1, 2.0])),
|
||||
(DataFrame({"a": [1.0, 1.1]}), DataFrame({"a": [1.0, 1.100001]})),
|
||||
],
|
||||
)
|
||||
def test_assert_almost_equal_pandas(a, b):
|
||||
_assert_almost_equal_both(a, b)
|
||||
|
||||
|
||||
def test_assert_almost_equal_object():
|
||||
a = [Timestamp("2011-01-01"), Timestamp("2011-01-01")]
|
||||
b = [Timestamp("2011-01-01"), Timestamp("2011-01-01")]
|
||||
_assert_almost_equal_both(a, b)
|
||||
|
||||
|
||||
def test_assert_almost_equal_value_mismatch():
|
||||
msg = "expected 2\\.00000 but got 1\\.00000, with decimal 5"
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal(1, 2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"a,b,klass1,klass2",
|
||||
[(np.array([1]), 1, "ndarray", "int"), (1, np.array([1]), "int", "ndarray")],
|
||||
)
|
||||
def test_assert_almost_equal_class_mismatch(a, b, klass1, klass2):
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array classes are different
|
||||
\\[left\\]: {klass1}
|
||||
\\[right\\]: {klass2}""".format(
|
||||
klass1=klass1, klass2=klass2
|
||||
)
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal(a, b)
|
||||
|
||||
|
||||
def test_assert_almost_equal_value_mismatch1():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(66\\.66667 %\\)
|
||||
\\[left\\]: \\[nan, 2\\.0, 3\\.0\\]
|
||||
\\[right\\]: \\[1\\.0, nan, 3\\.0\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal(np.array([np.nan, 2, 3]), np.array([1, np.nan, 3]))
|
||||
|
||||
|
||||
def test_assert_almost_equal_value_mismatch2():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(50\\.0 %\\)
|
||||
\\[left\\]: \\[1, 2\\]
|
||||
\\[right\\]: \\[1, 3\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal(np.array([1, 2]), np.array([1, 3]))
|
||||
|
||||
|
||||
def test_assert_almost_equal_value_mismatch3():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(16\\.66667 %\\)
|
||||
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\], \\[5, 6\\]\\]
|
||||
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\], \\[5, 6\\]\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal(
|
||||
np.array([[1, 2], [3, 4], [5, 6]]), np.array([[1, 3], [3, 4], [5, 6]])
|
||||
)
|
||||
|
||||
|
||||
def test_assert_almost_equal_value_mismatch4():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(25\\.0 %\\)
|
||||
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\]\\]
|
||||
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\]\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal(np.array([[1, 2], [3, 4]]), np.array([[1, 3], [3, 4]]))
|
||||
|
||||
|
||||
def test_assert_almost_equal_shape_mismatch_override():
|
||||
msg = """Index are different
|
||||
|
||||
Index shapes are different
|
||||
\\[left\\]: \\(2L*,\\)
|
||||
\\[right\\]: \\(3L*,\\)"""
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal(np.array([1, 2]), np.array([3, 4, 5]), obj="Index")
|
||||
|
||||
|
||||
def test_assert_almost_equal_unicode():
|
||||
# see gh-20503
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(33\\.33333 %\\)
|
||||
\\[left\\]: \\[á, à, ä\\]
|
||||
\\[right\\]: \\[á, à, å\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal(np.array(["á", "à", "ä"]), np.array(["á", "à", "å"]))
|
||||
|
||||
|
||||
def test_assert_almost_equal_timestamp():
|
||||
a = np.array([Timestamp("2011-01-01"), Timestamp("2011-01-01")])
|
||||
b = np.array([Timestamp("2011-01-01"), Timestamp("2011-01-02")])
|
||||
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(50\\.0 %\\)
|
||||
\\[left\\]: \\[2011-01-01 00:00:00, 2011-01-01 00:00:00\\]
|
||||
\\[right\\]: \\[2011-01-01 00:00:00, 2011-01-02 00:00:00\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal(a, b)
|
||||
|
||||
|
||||
def test_assert_almost_equal_iterable_length_mismatch():
|
||||
msg = """Iterable are different
|
||||
|
||||
Iterable length are different
|
||||
\\[left\\]: 2
|
||||
\\[right\\]: 3"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal([1, 2], [3, 4, 5])
|
||||
|
||||
|
||||
def test_assert_almost_equal_iterable_values_mismatch():
|
||||
msg = """Iterable are different
|
||||
|
||||
Iterable values are different \\(50\\.0 %\\)
|
||||
\\[left\\]: \\[1, 2\\]
|
||||
\\[right\\]: \\[1, 3\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_almost_equal([1, 2], [1, 3])
|
@@ -0,0 +1,92 @@
|
||||
import pytest
|
||||
|
||||
from pandas import Categorical
|
||||
from pandas.util.testing import assert_categorical_equal
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"c",
|
||||
[Categorical([1, 2, 3, 4]), Categorical([1, 2, 3, 4], categories=[1, 2, 3, 4, 5])],
|
||||
)
|
||||
def test_categorical_equal(c):
|
||||
assert_categorical_equal(c, c)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("check_category_order", [True, False])
|
||||
def test_categorical_equal_order_mismatch(check_category_order):
|
||||
c1 = Categorical([1, 2, 3, 4], categories=[1, 2, 3, 4])
|
||||
c2 = Categorical([1, 2, 3, 4], categories=[4, 3, 2, 1])
|
||||
kwargs = dict(check_category_order=check_category_order)
|
||||
|
||||
if check_category_order:
|
||||
msg = """Categorical\\.categories are different
|
||||
|
||||
Categorical\\.categories values are different \\(100\\.0 %\\)
|
||||
\\[left\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
|
||||
\\[right\\]: Int64Index\\(\\[4, 3, 2, 1\\], dtype='int64'\\)"""
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_categorical_equal(c1, c2, **kwargs)
|
||||
else:
|
||||
assert_categorical_equal(c1, c2, **kwargs)
|
||||
|
||||
|
||||
def test_categorical_equal_categories_mismatch():
|
||||
msg = """Categorical\\.categories are different
|
||||
|
||||
Categorical\\.categories values are different \\(25\\.0 %\\)
|
||||
\\[left\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)
|
||||
\\[right\\]: Int64Index\\(\\[1, 2, 3, 5\\], dtype='int64'\\)"""
|
||||
|
||||
c1 = Categorical([1, 2, 3, 4])
|
||||
c2 = Categorical([1, 2, 3, 5])
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_categorical_equal(c1, c2)
|
||||
|
||||
|
||||
def test_categorical_equal_codes_mismatch():
|
||||
categories = [1, 2, 3, 4]
|
||||
msg = """Categorical\\.codes are different
|
||||
|
||||
Categorical\\.codes values are different \\(50\\.0 %\\)
|
||||
\\[left\\]: \\[0, 1, 3, 2\\]
|
||||
\\[right\\]: \\[0, 1, 2, 3\\]"""
|
||||
|
||||
c1 = Categorical([1, 2, 4, 3], categories=categories)
|
||||
c2 = Categorical([1, 2, 3, 4], categories=categories)
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_categorical_equal(c1, c2)
|
||||
|
||||
|
||||
def test_categorical_equal_ordered_mismatch():
|
||||
data = [1, 2, 3, 4]
|
||||
msg = """Categorical are different
|
||||
|
||||
Attribute "ordered" are different
|
||||
\\[left\\]: False
|
||||
\\[right\\]: True"""
|
||||
|
||||
c1 = Categorical(data, ordered=False)
|
||||
c2 = Categorical(data, ordered=True)
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_categorical_equal(c1, c2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("obj", ["index", "foo", "pandas"])
|
||||
def test_categorical_equal_object_override(obj):
|
||||
data = [1, 2, 3, 4]
|
||||
msg = """{obj} are different
|
||||
|
||||
Attribute "ordered" are different
|
||||
\\[left\\]: False
|
||||
\\[right\\]: True""".format(
|
||||
obj=obj
|
||||
)
|
||||
|
||||
c1 = Categorical(data, ordered=False)
|
||||
c2 = Categorical(data, ordered=True)
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_categorical_equal(c1, c2, obj=obj)
|
@@ -0,0 +1,107 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas.core.arrays.sparse import SparseArray
|
||||
from pandas.util.testing import assert_extension_array_equal
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"kwargs",
|
||||
[
|
||||
dict(), # Default is check_exact=False
|
||||
dict(check_exact=False),
|
||||
dict(check_exact=True),
|
||||
],
|
||||
)
|
||||
def test_assert_extension_array_equal_not_exact(kwargs):
|
||||
# see gh-23709
|
||||
arr1 = SparseArray([-0.17387645482451206, 0.3414148016424936])
|
||||
arr2 = SparseArray([-0.17387645482451206, 0.3414148016424937])
|
||||
|
||||
if kwargs.get("check_exact", False):
|
||||
msg = """\
|
||||
ExtensionArray are different
|
||||
|
||||
ExtensionArray values are different \\(50\\.0 %\\)
|
||||
\\[left\\]: \\[-0\\.17387645482.*, 0\\.341414801642.*\\]
|
||||
\\[right\\]: \\[-0\\.17387645482.*, 0\\.341414801642.*\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_extension_array_equal(arr1, arr2, **kwargs)
|
||||
else:
|
||||
assert_extension_array_equal(arr1, arr2, **kwargs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"check_less_precise", [True, False, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
)
|
||||
def test_assert_extension_array_equal_less_precise(check_less_precise):
|
||||
arr1 = SparseArray([0.5, 0.123456])
|
||||
arr2 = SparseArray([0.5, 0.123457])
|
||||
|
||||
kwargs = dict(check_less_precise=check_less_precise)
|
||||
|
||||
if check_less_precise is False or check_less_precise >= 5:
|
||||
msg = """\
|
||||
ExtensionArray are different
|
||||
|
||||
ExtensionArray values are different \\(50\\.0 %\\)
|
||||
\\[left\\]: \\[0\\.5, 0\\.123456\\]
|
||||
\\[right\\]: \\[0\\.5, 0\\.123457\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_extension_array_equal(arr1, arr2, **kwargs)
|
||||
else:
|
||||
assert_extension_array_equal(arr1, arr2, **kwargs)
|
||||
|
||||
|
||||
def test_assert_extension_array_equal_dtype_mismatch(check_dtype):
|
||||
end = 5
|
||||
kwargs = dict(check_dtype=check_dtype)
|
||||
|
||||
arr1 = SparseArray(np.arange(end, dtype="int64"))
|
||||
arr2 = SparseArray(np.arange(end, dtype="int32"))
|
||||
|
||||
if check_dtype:
|
||||
msg = """\
|
||||
ExtensionArray are different
|
||||
|
||||
Attribute "dtype" are different
|
||||
\\[left\\]: Sparse\\[int64, 0\\]
|
||||
\\[right\\]: Sparse\\[int32, 0\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_extension_array_equal(arr1, arr2, **kwargs)
|
||||
else:
|
||||
assert_extension_array_equal(arr1, arr2, **kwargs)
|
||||
|
||||
|
||||
def test_assert_extension_array_equal_missing_values():
|
||||
arr1 = SparseArray([np.nan, 1, 2, np.nan])
|
||||
arr2 = SparseArray([np.nan, 1, 2, 3])
|
||||
|
||||
msg = """\
|
||||
ExtensionArray NA mask are different
|
||||
|
||||
ExtensionArray NA mask values are different \\(25\\.0 %\\)
|
||||
\\[left\\]: \\[True, False, False, True\\]
|
||||
\\[right\\]: \\[True, False, False, False\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_extension_array_equal(arr1, arr2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("side", ["left", "right"])
|
||||
def test_assert_extension_array_equal_non_extension_array(side):
|
||||
numpy_array = np.arange(5)
|
||||
extension_array = SparseArray(numpy_array)
|
||||
|
||||
msg = "{side} is not an ExtensionArray".format(side=side)
|
||||
args = (
|
||||
(numpy_array, extension_array)
|
||||
if side == "left"
|
||||
else (extension_array, numpy_array)
|
||||
)
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_extension_array_equal(*args)
|
@@ -0,0 +1,229 @@
|
||||
import pytest
|
||||
|
||||
from pandas import DataFrame
|
||||
from pandas.util.testing import assert_frame_equal
|
||||
|
||||
|
||||
@pytest.fixture(params=[True, False])
|
||||
def by_blocks_fixture(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=["DataFrame", "Series"])
|
||||
def obj_fixture(request):
|
||||
return request.param
|
||||
|
||||
|
||||
def _assert_frame_equal_both(a, b, **kwargs):
|
||||
"""
|
||||
Check that two DataFrame equal.
|
||||
|
||||
This check is performed commutatively.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
a : DataFrame
|
||||
The first DataFrame to compare.
|
||||
b : DataFrame
|
||||
The second DataFrame to compare.
|
||||
kwargs : dict
|
||||
The arguments passed to `assert_frame_equal`.
|
||||
"""
|
||||
assert_frame_equal(a, b, **kwargs)
|
||||
assert_frame_equal(b, a, **kwargs)
|
||||
|
||||
|
||||
def _assert_not_frame_equal(a, b, **kwargs):
|
||||
"""
|
||||
Check that two DataFrame are not equal.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
a : DataFrame
|
||||
The first DataFrame to compare.
|
||||
b : DataFrame
|
||||
The second DataFrame to compare.
|
||||
kwargs : dict
|
||||
The arguments passed to `assert_frame_equal`.
|
||||
"""
|
||||
try:
|
||||
assert_frame_equal(a, b, **kwargs)
|
||||
msg = "The two DataFrames were equal when they shouldn't have been"
|
||||
|
||||
pytest.fail(msg=msg)
|
||||
except AssertionError:
|
||||
pass
|
||||
|
||||
|
||||
def _assert_not_frame_equal_both(a, b, **kwargs):
|
||||
"""
|
||||
Check that two DataFrame are not equal.
|
||||
|
||||
This check is performed commutatively.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
a : DataFrame
|
||||
The first DataFrame to compare.
|
||||
b : DataFrame
|
||||
The second DataFrame to compare.
|
||||
kwargs : dict
|
||||
The arguments passed to `assert_frame_equal`.
|
||||
"""
|
||||
_assert_not_frame_equal(a, b, **kwargs)
|
||||
_assert_not_frame_equal(b, a, **kwargs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("check_like", [True, False])
|
||||
def test_frame_equal_row_order_mismatch(check_like, obj_fixture):
|
||||
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["a", "b", "c"])
|
||||
df2 = DataFrame({"A": [3, 2, 1], "B": [6, 5, 4]}, index=["c", "b", "a"])
|
||||
|
||||
if not check_like: # Do not ignore row-column orderings.
|
||||
msg = "{obj}.index are different".format(obj=obj_fixture)
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_frame_equal(df1, df2, check_like=check_like, obj=obj_fixture)
|
||||
else:
|
||||
_assert_frame_equal_both(df1, df2, check_like=check_like, obj=obj_fixture)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"df1,df2",
|
||||
[
|
||||
(DataFrame({"A": [1, 2, 3]}), DataFrame({"A": [1, 2, 3, 4]})),
|
||||
(DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}), DataFrame({"A": [1, 2, 3]})),
|
||||
],
|
||||
)
|
||||
def test_frame_equal_shape_mismatch(df1, df2, obj_fixture):
|
||||
msg = "{obj} are different".format(obj=obj_fixture)
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_frame_equal(df1, df2, obj=obj_fixture)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"df1,df2,msg",
|
||||
[
|
||||
# Index
|
||||
(
|
||||
DataFrame.from_records({"a": [1, 2], "c": ["l1", "l2"]}, index=["a"]),
|
||||
DataFrame.from_records({"a": [1.0, 2.0], "c": ["l1", "l2"]}, index=["a"]),
|
||||
"DataFrame\\.index are different",
|
||||
),
|
||||
# MultiIndex
|
||||
(
|
||||
DataFrame.from_records(
|
||||
{"a": [1, 2], "b": [2.1, 1.5], "c": ["l1", "l2"]}, index=["a", "b"]
|
||||
),
|
||||
DataFrame.from_records(
|
||||
{"a": [1.0, 2.0], "b": [2.1, 1.5], "c": ["l1", "l2"]}, index=["a", "b"]
|
||||
),
|
||||
"MultiIndex level \\[0\\] are different",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_frame_equal_index_dtype_mismatch(df1, df2, msg, check_index_type):
|
||||
kwargs = dict(check_index_type=check_index_type)
|
||||
|
||||
if check_index_type:
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_frame_equal(df1, df2, **kwargs)
|
||||
else:
|
||||
assert_frame_equal(df1, df2, **kwargs)
|
||||
|
||||
|
||||
def test_empty_dtypes(check_dtype):
|
||||
columns = ["col1", "col2"]
|
||||
df1 = DataFrame(columns=columns)
|
||||
df2 = DataFrame(columns=columns)
|
||||
|
||||
kwargs = dict(check_dtype=check_dtype)
|
||||
df1["col1"] = df1["col1"].astype("int64")
|
||||
|
||||
if check_dtype:
|
||||
msg = "Attributes are different"
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_frame_equal(df1, df2, **kwargs)
|
||||
else:
|
||||
assert_frame_equal(df1, df2, **kwargs)
|
||||
|
||||
|
||||
def test_frame_equal_index_mismatch(obj_fixture):
|
||||
msg = """{obj}\\.index are different
|
||||
|
||||
{obj}\\.index values are different \\(33\\.33333 %\\)
|
||||
\\[left\\]: Index\\(\\['a', 'b', 'c'\\], dtype='object'\\)
|
||||
\\[right\\]: Index\\(\\['a', 'b', 'd'\\], dtype='object'\\)""".format(
|
||||
obj=obj_fixture
|
||||
)
|
||||
|
||||
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["a", "b", "c"])
|
||||
df2 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["a", "b", "d"])
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_frame_equal(df1, df2, obj=obj_fixture)
|
||||
|
||||
|
||||
def test_frame_equal_columns_mismatch(obj_fixture):
|
||||
msg = """{obj}\\.columns are different
|
||||
|
||||
{obj}\\.columns values are different \\(50\\.0 %\\)
|
||||
\\[left\\]: Index\\(\\['A', 'B'\\], dtype='object'\\)
|
||||
\\[right\\]: Index\\(\\['A', 'b'\\], dtype='object'\\)""".format(
|
||||
obj=obj_fixture
|
||||
)
|
||||
|
||||
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]}, index=["a", "b", "c"])
|
||||
df2 = DataFrame({"A": [1, 2, 3], "b": [4, 5, 6]}, index=["a", "b", "c"])
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_frame_equal(df1, df2, obj=obj_fixture)
|
||||
|
||||
|
||||
def test_frame_equal_block_mismatch(by_blocks_fixture, obj_fixture):
|
||||
msg = """{obj}\\.iloc\\[:, 1\\] are different
|
||||
|
||||
{obj}\\.iloc\\[:, 1\\] values are different \\(33\\.33333 %\\)
|
||||
\\[left\\]: \\[4, 5, 6\\]
|
||||
\\[right\\]: \\[4, 5, 7\\]""".format(
|
||||
obj=obj_fixture
|
||||
)
|
||||
|
||||
df1 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
|
||||
df2 = DataFrame({"A": [1, 2, 3], "B": [4, 5, 7]})
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_frame_equal(df1, df2, by_blocks=by_blocks_fixture, obj=obj_fixture)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"df1,df2,msg",
|
||||
[
|
||||
(
|
||||
DataFrame({"A": ["á", "à", "ä"], "E": ["é", "è", "ë"]}),
|
||||
DataFrame({"A": ["á", "à", "ä"], "E": ["é", "è", "e̊"]}),
|
||||
"""{obj}\\.iloc\\[:, 1\\] are different
|
||||
|
||||
{obj}\\.iloc\\[:, 1\\] values are different \\(33\\.33333 %\\)
|
||||
\\[left\\]: \\[é, è, ë\\]
|
||||
\\[right\\]: \\[é, è, e̊\\]""",
|
||||
),
|
||||
(
|
||||
DataFrame({"A": ["á", "à", "ä"], "E": ["é", "è", "ë"]}),
|
||||
DataFrame({"A": ["a", "a", "a"], "E": ["e", "e", "e"]}),
|
||||
"""{obj}\\.iloc\\[:, 0\\] are different
|
||||
|
||||
{obj}\\.iloc\\[:, 0\\] values are different \\(100\\.0 %\\)
|
||||
\\[left\\]: \\[á, à, ä\\]
|
||||
\\[right\\]: \\[a, a, a\\]""",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_frame_equal_unicode(df1, df2, msg, by_blocks_fixture, obj_fixture):
|
||||
# see gh-20503
|
||||
#
|
||||
# Test ensures that `assert_frame_equals` raises the right exception
|
||||
# when comparing DataFrames containing differing unicode objects.
|
||||
msg = msg.format(obj=obj_fixture)
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_frame_equal(df1, df2, by_blocks=by_blocks_fixture, obj=obj_fixture)
|
@@ -0,0 +1,173 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import Categorical, Index, MultiIndex, NaT
|
||||
from pandas.util.testing import assert_index_equal
|
||||
|
||||
|
||||
def test_index_equal_levels_mismatch():
|
||||
msg = """Index are different
|
||||
|
||||
Index levels are different
|
||||
\\[left\\]: 1, Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\)
|
||||
\\[right\\]: 2, MultiIndex\\(\\[\\('A', 1\\),
|
||||
\\('A', 2\\),
|
||||
\\('B', 3\\),
|
||||
\\('B', 4\\)\\],
|
||||
\\)"""
|
||||
|
||||
idx1 = Index([1, 2, 3])
|
||||
idx2 = MultiIndex.from_tuples([("A", 1), ("A", 2), ("B", 3), ("B", 4)])
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_index_equal(idx1, idx2, exact=False)
|
||||
|
||||
|
||||
def test_index_equal_values_mismatch(check_exact):
|
||||
msg = """MultiIndex level \\[1\\] are different
|
||||
|
||||
MultiIndex level \\[1\\] values are different \\(25\\.0 %\\)
|
||||
\\[left\\]: Int64Index\\(\\[2, 2, 3, 4\\], dtype='int64'\\)
|
||||
\\[right\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""
|
||||
|
||||
idx1 = MultiIndex.from_tuples([("A", 2), ("A", 2), ("B", 3), ("B", 4)])
|
||||
idx2 = MultiIndex.from_tuples([("A", 1), ("A", 2), ("B", 3), ("B", 4)])
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_index_equal(idx1, idx2, check_exact=check_exact)
|
||||
|
||||
|
||||
def test_index_equal_length_mismatch(check_exact):
|
||||
msg = """Index are different
|
||||
|
||||
Index length are different
|
||||
\\[left\\]: 3, Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\)
|
||||
\\[right\\]: 4, Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""
|
||||
|
||||
idx1 = Index([1, 2, 3])
|
||||
idx2 = Index([1, 2, 3, 4])
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_index_equal(idx1, idx2, check_exact=check_exact)
|
||||
|
||||
|
||||
def test_index_equal_class_mismatch(check_exact):
|
||||
msg = """Index are different
|
||||
|
||||
Index classes are different
|
||||
\\[left\\]: Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\)
|
||||
\\[right\\]: Float64Index\\(\\[1\\.0, 2\\.0, 3\\.0\\], dtype='float64'\\)"""
|
||||
|
||||
idx1 = Index([1, 2, 3])
|
||||
idx2 = Index([1, 2, 3.0])
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_index_equal(idx1, idx2, exact=True, check_exact=check_exact)
|
||||
|
||||
|
||||
def test_index_equal_values_close(check_exact):
|
||||
idx1 = Index([1, 2, 3.0])
|
||||
idx2 = Index([1, 2, 3.0000000001])
|
||||
|
||||
if check_exact:
|
||||
msg = """Index are different
|
||||
|
||||
Index values are different \\(33\\.33333 %\\)
|
||||
\\[left\\]: Float64Index\\(\\[1.0, 2.0, 3.0], dtype='float64'\\)
|
||||
\\[right\\]: Float64Index\\(\\[1.0, 2.0, 3.0000000001\\], dtype='float64'\\)"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_index_equal(idx1, idx2, check_exact=check_exact)
|
||||
else:
|
||||
assert_index_equal(idx1, idx2, check_exact=check_exact)
|
||||
|
||||
|
||||
def test_index_equal_values_less_close(check_exact, check_less_precise):
|
||||
idx1 = Index([1, 2, 3.0])
|
||||
idx2 = Index([1, 2, 3.0001])
|
||||
kwargs = dict(check_exact=check_exact, check_less_precise=check_less_precise)
|
||||
|
||||
if check_exact or not check_less_precise:
|
||||
msg = """Index are different
|
||||
|
||||
Index values are different \\(33\\.33333 %\\)
|
||||
\\[left\\]: Float64Index\\(\\[1.0, 2.0, 3.0], dtype='float64'\\)
|
||||
\\[right\\]: Float64Index\\(\\[1.0, 2.0, 3.0001\\], dtype='float64'\\)"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_index_equal(idx1, idx2, **kwargs)
|
||||
else:
|
||||
assert_index_equal(idx1, idx2, **kwargs)
|
||||
|
||||
|
||||
def test_index_equal_values_too_far(check_exact, check_less_precise):
|
||||
idx1 = Index([1, 2, 3])
|
||||
idx2 = Index([1, 2, 4])
|
||||
kwargs = dict(check_exact=check_exact, check_less_precise=check_less_precise)
|
||||
|
||||
msg = """Index are different
|
||||
|
||||
Index values are different \\(33\\.33333 %\\)
|
||||
\\[left\\]: Int64Index\\(\\[1, 2, 3\\], dtype='int64'\\)
|
||||
\\[right\\]: Int64Index\\(\\[1, 2, 4\\], dtype='int64'\\)"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_index_equal(idx1, idx2, **kwargs)
|
||||
|
||||
|
||||
def test_index_equal_level_values_mismatch(check_exact, check_less_precise):
|
||||
idx1 = MultiIndex.from_tuples([("A", 2), ("A", 2), ("B", 3), ("B", 4)])
|
||||
idx2 = MultiIndex.from_tuples([("A", 1), ("A", 2), ("B", 3), ("B", 4)])
|
||||
kwargs = dict(check_exact=check_exact, check_less_precise=check_less_precise)
|
||||
|
||||
msg = """MultiIndex level \\[1\\] are different
|
||||
|
||||
MultiIndex level \\[1\\] values are different \\(25\\.0 %\\)
|
||||
\\[left\\]: Int64Index\\(\\[2, 2, 3, 4\\], dtype='int64'\\)
|
||||
\\[right\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_index_equal(idx1, idx2, **kwargs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"name1,name2",
|
||||
[(None, "x"), ("x", "x"), (np.nan, np.nan), (NaT, NaT), (np.nan, NaT)],
|
||||
)
|
||||
def test_index_equal_names(name1, name2):
|
||||
msg = """Index are different
|
||||
|
||||
Attribute "names" are different
|
||||
\\[left\\]: \\[{name1}\\]
|
||||
\\[right\\]: \\[{name2}\\]"""
|
||||
|
||||
idx1 = Index([1, 2, 3], name=name1)
|
||||
idx2 = Index([1, 2, 3], name=name2)
|
||||
|
||||
if name1 == name2 or name1 is name2:
|
||||
assert_index_equal(idx1, idx2)
|
||||
else:
|
||||
name1 = "'x'" if name1 == "x" else name1
|
||||
name2 = "'x'" if name2 == "x" else name2
|
||||
msg = msg.format(name1=name1, name2=name2)
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_index_equal(idx1, idx2)
|
||||
|
||||
|
||||
def test_index_equal_category_mismatch(check_categorical):
|
||||
msg = """Index are different
|
||||
|
||||
Attribute "dtype" are different
|
||||
\\[left\\]: CategoricalDtype\\(categories=\\['a', 'b'\\], ordered=False\\)
|
||||
\\[right\\]: CategoricalDtype\\(categories=\\['a', 'b', 'c'\\], \
|
||||
ordered=False\\)"""
|
||||
|
||||
idx1 = Index(Categorical(["a", "b"]))
|
||||
idx2 = Index(Categorical(["a", "b"], categories=["a", "b", "c"]))
|
||||
|
||||
if check_categorical:
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_index_equal(idx1, idx2, check_categorical=check_categorical)
|
||||
else:
|
||||
assert_index_equal(idx1, idx2, check_categorical=check_categorical)
|
@@ -0,0 +1,81 @@
|
||||
import pytest
|
||||
|
||||
from pandas import interval_range
|
||||
from pandas.util.testing import assert_interval_array_equal
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"kwargs",
|
||||
[
|
||||
dict(start=0, periods=4),
|
||||
dict(start=1, periods=5),
|
||||
dict(start=5, end=10, closed="left"),
|
||||
],
|
||||
)
|
||||
def test_interval_array_equal(kwargs):
|
||||
arr = interval_range(**kwargs).values
|
||||
assert_interval_array_equal(arr, arr)
|
||||
|
||||
|
||||
def test_interval_array_equal_closed_mismatch():
|
||||
kwargs = dict(start=0, periods=5)
|
||||
arr1 = interval_range(closed="left", **kwargs).values
|
||||
arr2 = interval_range(closed="right", **kwargs).values
|
||||
|
||||
msg = """\
|
||||
IntervalArray are different
|
||||
|
||||
Attribute "closed" are different
|
||||
\\[left\\]: left
|
||||
\\[right\\]: right"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_interval_array_equal(arr1, arr2)
|
||||
|
||||
|
||||
def test_interval_array_equal_periods_mismatch():
|
||||
kwargs = dict(start=0)
|
||||
arr1 = interval_range(periods=5, **kwargs).values
|
||||
arr2 = interval_range(periods=6, **kwargs).values
|
||||
|
||||
msg = """\
|
||||
IntervalArray.left are different
|
||||
|
||||
IntervalArray.left length are different
|
||||
\\[left\\]: 5, Int64Index\\(\\[0, 1, 2, 3, 4\\], dtype='int64'\\)
|
||||
\\[right\\]: 6, Int64Index\\(\\[0, 1, 2, 3, 4, 5\\], dtype='int64'\\)"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_interval_array_equal(arr1, arr2)
|
||||
|
||||
|
||||
def test_interval_array_equal_end_mismatch():
|
||||
kwargs = dict(start=0, periods=5)
|
||||
arr1 = interval_range(end=10, **kwargs).values
|
||||
arr2 = interval_range(end=20, **kwargs).values
|
||||
|
||||
msg = """\
|
||||
IntervalArray.left are different
|
||||
|
||||
IntervalArray.left values are different \\(80.0 %\\)
|
||||
\\[left\\]: Int64Index\\(\\[0, 2, 4, 6, 8\\], dtype='int64'\\)
|
||||
\\[right\\]: Int64Index\\(\\[0, 4, 8, 12, 16\\], dtype='int64'\\)"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_interval_array_equal(arr1, arr2)
|
||||
|
||||
|
||||
def test_interval_array_equal_start_mismatch():
|
||||
kwargs = dict(periods=4)
|
||||
arr1 = interval_range(start=0, **kwargs).values
|
||||
arr2 = interval_range(start=1, **kwargs).values
|
||||
|
||||
msg = """\
|
||||
IntervalArray.left are different
|
||||
|
||||
IntervalArray.left values are different \\(100.0 %\\)
|
||||
\\[left\\]: Int64Index\\(\\[0, 1, 2, 3\\], dtype='int64'\\)
|
||||
\\[right\\]: Int64Index\\(\\[1, 2, 3, 4\\], dtype='int64'\\)"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_interval_array_equal(arr1, arr2)
|
@@ -0,0 +1,175 @@
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
from pandas import Timestamp
|
||||
from pandas.util.testing import assert_numpy_array_equal
|
||||
|
||||
|
||||
def test_assert_numpy_array_equal_shape_mismatch():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array shapes are different
|
||||
\\[left\\]: \\(2L*,\\)
|
||||
\\[right\\]: \\(3L*,\\)"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(np.array([1, 2]), np.array([3, 4, 5]))
|
||||
|
||||
|
||||
def test_assert_numpy_array_equal_bad_type():
|
||||
expected = "Expected type"
|
||||
|
||||
with pytest.raises(AssertionError, match=expected):
|
||||
assert_numpy_array_equal(1, 2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"a,b,klass1,klass2",
|
||||
[(np.array([1]), 1, "ndarray", "int"), (1, np.array([1]), "int", "ndarray")],
|
||||
)
|
||||
def test_assert_numpy_array_equal_class_mismatch(a, b, klass1, klass2):
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array classes are different
|
||||
\\[left\\]: {klass1}
|
||||
\\[right\\]: {klass2}""".format(
|
||||
klass1=klass1, klass2=klass2
|
||||
)
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(a, b)
|
||||
|
||||
|
||||
def test_assert_numpy_array_equal_value_mismatch1():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(66\\.66667 %\\)
|
||||
\\[left\\]: \\[nan, 2\\.0, 3\\.0\\]
|
||||
\\[right\\]: \\[1\\.0, nan, 3\\.0\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(np.array([np.nan, 2, 3]), np.array([1, np.nan, 3]))
|
||||
|
||||
|
||||
def test_assert_numpy_array_equal_value_mismatch2():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(50\\.0 %\\)
|
||||
\\[left\\]: \\[1, 2\\]
|
||||
\\[right\\]: \\[1, 3\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(np.array([1, 2]), np.array([1, 3]))
|
||||
|
||||
|
||||
def test_assert_numpy_array_equal_value_mismatch3():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(16\\.66667 %\\)
|
||||
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\], \\[5, 6\\]\\]
|
||||
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\], \\[5, 6\\]\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(
|
||||
np.array([[1, 2], [3, 4], [5, 6]]), np.array([[1, 3], [3, 4], [5, 6]])
|
||||
)
|
||||
|
||||
|
||||
def test_assert_numpy_array_equal_value_mismatch4():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(50\\.0 %\\)
|
||||
\\[left\\]: \\[1\\.1, 2\\.000001\\]
|
||||
\\[right\\]: \\[1\\.1, 2.0\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(np.array([1.1, 2.000001]), np.array([1.1, 2.0]))
|
||||
|
||||
|
||||
def test_assert_numpy_array_equal_value_mismatch5():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(16\\.66667 %\\)
|
||||
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\], \\[5, 6\\]\\]
|
||||
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\], \\[5, 6\\]\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(
|
||||
np.array([[1, 2], [3, 4], [5, 6]]), np.array([[1, 3], [3, 4], [5, 6]])
|
||||
)
|
||||
|
||||
|
||||
def test_assert_numpy_array_equal_value_mismatch6():
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(25\\.0 %\\)
|
||||
\\[left\\]: \\[\\[1, 2\\], \\[3, 4\\]\\]
|
||||
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\]\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(np.array([[1, 2], [3, 4]]), np.array([[1, 3], [3, 4]]))
|
||||
|
||||
|
||||
def test_assert_numpy_array_equal_shape_mismatch_override():
|
||||
msg = """Index are different
|
||||
|
||||
Index shapes are different
|
||||
\\[left\\]: \\(2L*,\\)
|
||||
\\[right\\]: \\(3L*,\\)"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(np.array([1, 2]), np.array([3, 4, 5]), obj="Index")
|
||||
|
||||
|
||||
def test_numpy_array_equal_unicode():
|
||||
# see gh-20503
|
||||
#
|
||||
# Test ensures that `assert_numpy_array_equals` raises the right
|
||||
# exception when comparing np.arrays containing differing unicode objects.
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(33\\.33333 %\\)
|
||||
\\[left\\]: \\[á, à, ä\\]
|
||||
\\[right\\]: \\[á, à, å\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(np.array(["á", "à", "ä"]), np.array(["á", "à", "å"]))
|
||||
|
||||
|
||||
def test_numpy_array_equal_object():
|
||||
a = np.array([Timestamp("2011-01-01"), Timestamp("2011-01-01")])
|
||||
b = np.array([Timestamp("2011-01-01"), Timestamp("2011-01-02")])
|
||||
|
||||
msg = """numpy array are different
|
||||
|
||||
numpy array values are different \\(50\\.0 %\\)
|
||||
\\[left\\]: \\[2011-01-01 00:00:00, 2011-01-01 00:00:00\\]
|
||||
\\[right\\]: \\[2011-01-01 00:00:00, 2011-01-02 00:00:00\\]"""
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(a, b)
|
||||
|
||||
|
||||
@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(AssertionError, match=msg):
|
||||
assert_numpy_array_equal(a, other, check_same=check_same)
|
||||
else:
|
||||
assert_numpy_array_equal(a, other, check_same=check_same)
|
@@ -0,0 +1,22 @@
|
||||
import warnings
|
||||
|
||||
import pytest
|
||||
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
def f():
|
||||
warnings.warn("f1", FutureWarning)
|
||||
warnings.warn("f2", RuntimeWarning)
|
||||
|
||||
|
||||
@pytest.mark.filterwarnings("ignore:f1:FutureWarning")
|
||||
def test_assert_produces_warning_honors_filter():
|
||||
# Raise by default.
|
||||
msg = r"Caused unexpected warning\(s\)"
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
with tm.assert_produces_warning(RuntimeWarning):
|
||||
f()
|
||||
|
||||
with tm.assert_produces_warning(RuntimeWarning, raise_on_extra_warnings=False):
|
||||
f()
|
@@ -0,0 +1,196 @@
|
||||
import pytest
|
||||
|
||||
from pandas import Categorical, DataFrame, Series
|
||||
from pandas.util.testing import assert_series_equal
|
||||
|
||||
|
||||
def _assert_series_equal_both(a, b, **kwargs):
|
||||
"""
|
||||
Check that two Series equal.
|
||||
|
||||
This check is performed commutatively.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
a : Series
|
||||
The first Series to compare.
|
||||
b : Series
|
||||
The second Series to compare.
|
||||
kwargs : dict
|
||||
The arguments passed to `assert_series_equal`.
|
||||
"""
|
||||
assert_series_equal(a, b, **kwargs)
|
||||
assert_series_equal(b, a, **kwargs)
|
||||
|
||||
|
||||
def _assert_not_series_equal(a, b, **kwargs):
|
||||
"""
|
||||
Check that two Series are not equal.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
a : Series
|
||||
The first Series to compare.
|
||||
b : Series
|
||||
The second Series to compare.
|
||||
kwargs : dict
|
||||
The arguments passed to `assert_series_equal`.
|
||||
"""
|
||||
try:
|
||||
assert_series_equal(a, b, **kwargs)
|
||||
msg = "The two Series were equal when they shouldn't have been"
|
||||
|
||||
pytest.fail(msg=msg)
|
||||
except AssertionError:
|
||||
pass
|
||||
|
||||
|
||||
def _assert_not_series_equal_both(a, b, **kwargs):
|
||||
"""
|
||||
Check that two Series are not equal.
|
||||
|
||||
This check is performed commutatively.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
a : Series
|
||||
The first Series to compare.
|
||||
b : Series
|
||||
The second Series to compare.
|
||||
kwargs : dict
|
||||
The arguments passed to `assert_series_equal`.
|
||||
"""
|
||||
_assert_not_series_equal(a, b, **kwargs)
|
||||
_assert_not_series_equal(b, a, **kwargs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("data", [range(3), list("abc"), list("áàä")])
|
||||
def test_series_equal(data):
|
||||
_assert_series_equal_both(Series(data), Series(data))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"data1,data2",
|
||||
[
|
||||
(range(3), range(1, 4)),
|
||||
(list("abc"), list("xyz")),
|
||||
(list("áàä"), list("éèë")),
|
||||
(list("áàä"), list(b"aaa")),
|
||||
(range(3), range(4)),
|
||||
],
|
||||
)
|
||||
def test_series_not_equal_value_mismatch(data1, data2):
|
||||
_assert_not_series_equal_both(Series(data1), Series(data2))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"kwargs",
|
||||
[
|
||||
dict(dtype="float64"), # dtype mismatch
|
||||
dict(index=[1, 2, 4]), # index mismatch
|
||||
dict(name="foo"), # name mismatch
|
||||
],
|
||||
)
|
||||
def test_series_not_equal_metadata_mismatch(kwargs):
|
||||
data = range(3)
|
||||
s1 = Series(data)
|
||||
|
||||
s2 = Series(data, **kwargs)
|
||||
_assert_not_series_equal_both(s1, s2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("data1,data2", [(0.12345, 0.12346), (0.1235, 0.1236)])
|
||||
@pytest.mark.parametrize("dtype", ["float32", "float64"])
|
||||
@pytest.mark.parametrize("check_less_precise", [False, True, 0, 1, 2, 3, 10])
|
||||
def test_less_precise(data1, data2, dtype, check_less_precise):
|
||||
s1 = Series([data1], dtype=dtype)
|
||||
s2 = Series([data2], dtype=dtype)
|
||||
|
||||
kwargs = dict(check_less_precise=check_less_precise)
|
||||
|
||||
if (check_less_precise is False or check_less_precise == 10) or (
|
||||
(check_less_precise is True or check_less_precise >= 3)
|
||||
and abs(data1 - data2) >= 0.0001
|
||||
):
|
||||
msg = "Series values are different"
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_series_equal(s1, s2, **kwargs)
|
||||
else:
|
||||
_assert_series_equal_both(s1, s2, **kwargs)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"s1,s2,msg",
|
||||
[
|
||||
# Index
|
||||
(
|
||||
Series(["l1", "l2"], index=[1, 2]),
|
||||
Series(["l1", "l2"], index=[1.0, 2.0]),
|
||||
"Series\\.index are different",
|
||||
),
|
||||
# MultiIndex
|
||||
(
|
||||
DataFrame.from_records(
|
||||
{"a": [1, 2], "b": [2.1, 1.5], "c": ["l1", "l2"]}, index=["a", "b"]
|
||||
).c,
|
||||
DataFrame.from_records(
|
||||
{"a": [1.0, 2.0], "b": [2.1, 1.5], "c": ["l1", "l2"]}, index=["a", "b"]
|
||||
).c,
|
||||
"MultiIndex level \\[0\\] are different",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_series_equal_index_dtype(s1, s2, msg, check_index_type):
|
||||
kwargs = dict(check_index_type=check_index_type)
|
||||
|
||||
if check_index_type:
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_series_equal(s1, s2, **kwargs)
|
||||
else:
|
||||
assert_series_equal(s1, s2, **kwargs)
|
||||
|
||||
|
||||
def test_series_equal_length_mismatch(check_less_precise):
|
||||
msg = """Series are different
|
||||
|
||||
Series length are different
|
||||
\\[left\\]: 3, RangeIndex\\(start=0, stop=3, step=1\\)
|
||||
\\[right\\]: 4, RangeIndex\\(start=0, stop=4, step=1\\)"""
|
||||
|
||||
s1 = Series([1, 2, 3])
|
||||
s2 = Series([1, 2, 3, 4])
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_series_equal(s1, s2, check_less_precise=check_less_precise)
|
||||
|
||||
|
||||
def test_series_equal_values_mismatch(check_less_precise):
|
||||
msg = """Series are different
|
||||
|
||||
Series values are different \\(33\\.33333 %\\)
|
||||
\\[left\\]: \\[1, 2, 3\\]
|
||||
\\[right\\]: \\[1, 2, 4\\]"""
|
||||
|
||||
s1 = Series([1, 2, 3])
|
||||
s2 = Series([1, 2, 4])
|
||||
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_series_equal(s1, s2, check_less_precise=check_less_precise)
|
||||
|
||||
|
||||
def test_series_equal_categorical_mismatch(check_categorical):
|
||||
msg = """Attributes are different
|
||||
|
||||
Attribute "dtype" are different
|
||||
\\[left\\]: CategoricalDtype\\(categories=\\['a', 'b'\\], ordered=False\\)
|
||||
\\[right\\]: CategoricalDtype\\(categories=\\['a', 'b', 'c'\\], \
|
||||
ordered=False\\)"""
|
||||
|
||||
s1 = Series(Categorical(["a", "b"]))
|
||||
s2 = Series(Categorical(["a", "b"], categories=list("abc")))
|
||||
|
||||
if check_categorical:
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
assert_series_equal(s1, s2, check_categorical=check_categorical)
|
||||
else:
|
||||
_assert_series_equal_both(s1, s2, check_categorical=check_categorical)
|
@@ -0,0 +1,64 @@
|
||||
from textwrap import dedent
|
||||
|
||||
import pytest
|
||||
|
||||
from pandas.util._decorators import deprecate
|
||||
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
def new_func():
|
||||
"""
|
||||
This is the summary. The deprecate directive goes next.
|
||||
|
||||
This is the extended summary. The deprecate directive goes before this.
|
||||
"""
|
||||
return "new_func called"
|
||||
|
||||
|
||||
def new_func_no_docstring():
|
||||
return "new_func_no_docstring called"
|
||||
|
||||
|
||||
def new_func_wrong_docstring():
|
||||
"""Summary should be in the next line."""
|
||||
return "new_func_wrong_docstring called"
|
||||
|
||||
|
||||
def new_func_with_deprecation():
|
||||
"""
|
||||
This is the summary. The deprecate directive goes next.
|
||||
|
||||
.. deprecated:: 1.0
|
||||
Use new_func instead.
|
||||
|
||||
This is the extended summary. The deprecate directive goes before this.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
def test_deprecate_ok():
|
||||
depr_func = deprecate("depr_func", new_func, "1.0", msg="Use new_func instead.")
|
||||
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
result = depr_func()
|
||||
|
||||
assert result == "new_func called"
|
||||
assert depr_func.__doc__ == dedent(new_func_with_deprecation.__doc__)
|
||||
|
||||
|
||||
def test_deprecate_no_docstring():
|
||||
depr_func = deprecate(
|
||||
"depr_func", new_func_no_docstring, "1.0", msg="Use new_func instead."
|
||||
)
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
result = depr_func()
|
||||
assert result == "new_func_no_docstring called"
|
||||
|
||||
|
||||
def test_deprecate_wrong_docstring():
|
||||
msg = "deprecate needs a correctly formatted docstring"
|
||||
with pytest.raises(AssertionError, match=msg):
|
||||
deprecate(
|
||||
"depr_func", new_func_wrong_docstring, "1.0", msg="Use new_func instead."
|
||||
)
|
@@ -0,0 +1,90 @@
|
||||
import pytest
|
||||
|
||||
from pandas.util._decorators import deprecate_kwarg
|
||||
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
@deprecate_kwarg("old", "new")
|
||||
def _f1(new=False):
|
||||
return new
|
||||
|
||||
|
||||
_f2_mappings = {"yes": True, "no": False}
|
||||
|
||||
|
||||
@deprecate_kwarg("old", "new", _f2_mappings)
|
||||
def _f2(new=False):
|
||||
return new
|
||||
|
||||
|
||||
def _f3_mapping(x):
|
||||
return x + 1
|
||||
|
||||
|
||||
@deprecate_kwarg("old", "new", _f3_mapping)
|
||||
def _f3(new=0):
|
||||
return new
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key,klass", [("old", FutureWarning), ("new", None)])
|
||||
def test_deprecate_kwarg(key, klass):
|
||||
x = 78
|
||||
|
||||
with tm.assert_produces_warning(klass):
|
||||
assert _f1(**{key: x}) == x
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key", list(_f2_mappings.keys()))
|
||||
def test_dict_deprecate_kwarg(key):
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
assert _f2(old=key) == _f2_mappings[key]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key", ["bogus", 12345, -1.23])
|
||||
def test_missing_deprecate_kwarg(key):
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
assert _f2(old=key) == key
|
||||
|
||||
|
||||
@pytest.mark.parametrize("x", [1, -1.4, 0])
|
||||
def test_callable_deprecate_kwarg(x):
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
assert _f3(old=x) == _f3_mapping(x)
|
||||
|
||||
|
||||
def test_callable_deprecate_kwarg_fail():
|
||||
msg = "((can only|cannot) concatenate)|(must be str)|(Can't convert)"
|
||||
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
_f3(old="hello")
|
||||
|
||||
|
||||
def test_bad_deprecate_kwarg():
|
||||
msg = "mapping from old to new argument values must be dict or callable!"
|
||||
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
|
||||
@deprecate_kwarg("old", "new", 0)
|
||||
def f4(new=None):
|
||||
return new
|
||||
|
||||
|
||||
@deprecate_kwarg("old", None)
|
||||
def _f4(old=True, unchanged=True):
|
||||
return old, unchanged
|
||||
|
||||
|
||||
@pytest.mark.parametrize("key", ["old", "unchanged"])
|
||||
def test_deprecate_keyword(key):
|
||||
x = 9
|
||||
|
||||
if key == "old":
|
||||
klass = FutureWarning
|
||||
expected = (x, True)
|
||||
else:
|
||||
klass = None
|
||||
expected = (True, x)
|
||||
|
||||
with tm.assert_produces_warning(klass):
|
||||
assert _f4(**{key: x}) == expected
|
@@ -0,0 +1,355 @@
|
||||
import datetime
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
|
||||
import pandas as pd
|
||||
from pandas import DataFrame, Index, MultiIndex, Series
|
||||
from pandas.core.util.hashing import _hash_scalar, hash_tuple, hash_tuples
|
||||
from pandas.util import hash_array, hash_pandas_object
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
@pytest.fixture(
|
||||
params=[
|
||||
Series([1, 2, 3] * 3, dtype="int32"),
|
||||
Series([None, 2.5, 3.5] * 3, dtype="float32"),
|
||||
Series(["a", "b", "c"] * 3, dtype="category"),
|
||||
Series(["d", "e", "f"] * 3),
|
||||
Series([True, False, True] * 3),
|
||||
Series(pd.date_range("20130101", periods=9)),
|
||||
Series(pd.date_range("20130101", periods=9, tz="US/Eastern")),
|
||||
Series(pd.timedelta_range("2000", periods=9)),
|
||||
]
|
||||
)
|
||||
def series(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=[True, False])
|
||||
def index(request):
|
||||
return request.param
|
||||
|
||||
|
||||
def _check_equal(obj, **kwargs):
|
||||
"""
|
||||
Check that hashing an objects produces the same value each time.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
obj : object
|
||||
The object to hash.
|
||||
kwargs : kwargs
|
||||
Keyword arguments to pass to the hashing function.
|
||||
"""
|
||||
a = hash_pandas_object(obj, **kwargs)
|
||||
b = hash_pandas_object(obj, **kwargs)
|
||||
tm.assert_series_equal(a, b)
|
||||
|
||||
|
||||
def _check_not_equal_with_index(obj):
|
||||
"""
|
||||
Check the hash of an object with and without its index is not the same.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
obj : object
|
||||
The object to hash.
|
||||
"""
|
||||
if not isinstance(obj, Index):
|
||||
a = hash_pandas_object(obj, index=True)
|
||||
b = hash_pandas_object(obj, index=False)
|
||||
|
||||
if len(obj):
|
||||
assert not (a == b).all()
|
||||
|
||||
|
||||
def test_consistency():
|
||||
# Check that our hash doesn't change because of a mistake
|
||||
# in the actual code; this is the ground truth.
|
||||
result = hash_pandas_object(Index(["foo", "bar", "baz"]))
|
||||
expected = Series(
|
||||
np.array(
|
||||
[3600424527151052760, 1374399572096150070, 477881037637427054],
|
||||
dtype="uint64",
|
||||
),
|
||||
index=["foo", "bar", "baz"],
|
||||
)
|
||||
tm.assert_series_equal(result, expected)
|
||||
|
||||
|
||||
def test_hash_array(series):
|
||||
arr = series.values
|
||||
tm.assert_numpy_array_equal(hash_array(arr), hash_array(arr))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"arr2", [np.array([3, 4, "All"]), np.array([3, 4, "All"], dtype=object)]
|
||||
)
|
||||
def test_hash_array_mixed(arr2):
|
||||
result1 = hash_array(np.array(["3", "4", "All"]))
|
||||
result2 = hash_array(arr2)
|
||||
|
||||
tm.assert_numpy_array_equal(result1, result2)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("val", [5, "foo", pd.Timestamp("20130101")])
|
||||
def test_hash_array_errors(val):
|
||||
msg = "must pass a ndarray-like"
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
hash_array(val)
|
||||
|
||||
|
||||
def test_hash_tuples():
|
||||
tuples = [(1, "one"), (1, "two"), (2, "one")]
|
||||
result = hash_tuples(tuples)
|
||||
|
||||
expected = hash_pandas_object(MultiIndex.from_tuples(tuples)).values
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
result = hash_tuples(tuples[0])
|
||||
assert result == expected[0]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"tup",
|
||||
[(1, "one"), (1, np.nan), (1.0, pd.NaT, "A"), ("A", pd.Timestamp("2012-01-01"))],
|
||||
)
|
||||
def test_hash_tuple(tup):
|
||||
# Test equivalence between
|
||||
# hash_tuples and hash_tuple.
|
||||
result = hash_tuple(tup)
|
||||
expected = hash_tuples([tup])[0]
|
||||
|
||||
assert result == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"val",
|
||||
[
|
||||
1,
|
||||
1.4,
|
||||
"A",
|
||||
b"A",
|
||||
pd.Timestamp("2012-01-01"),
|
||||
pd.Timestamp("2012-01-01", tz="Europe/Brussels"),
|
||||
datetime.datetime(2012, 1, 1),
|
||||
pd.Timestamp("2012-01-01", tz="EST").to_pydatetime(),
|
||||
pd.Timedelta("1 days"),
|
||||
datetime.timedelta(1),
|
||||
pd.Period("2012-01-01", freq="D"),
|
||||
pd.Interval(0, 1),
|
||||
np.nan,
|
||||
pd.NaT,
|
||||
None,
|
||||
],
|
||||
)
|
||||
def test_hash_scalar(val):
|
||||
result = _hash_scalar(val)
|
||||
expected = hash_array(np.array([val], dtype=object), categorize=True)
|
||||
|
||||
assert result[0] == expected[0]
|
||||
|
||||
|
||||
@pytest.mark.parametrize("val", [5, "foo", pd.Timestamp("20130101")])
|
||||
def test_hash_tuples_err(val):
|
||||
msg = "must be convertible to a list-of-tuples"
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
hash_tuples(val)
|
||||
|
||||
|
||||
def test_multiindex_unique():
|
||||
mi = MultiIndex.from_tuples([(118, 472), (236, 118), (51, 204), (102, 51)])
|
||||
assert mi.is_unique is True
|
||||
|
||||
result = hash_pandas_object(mi)
|
||||
assert result.is_unique is True
|
||||
|
||||
|
||||
def test_multiindex_objects():
|
||||
mi = MultiIndex(
|
||||
levels=[["b", "d", "a"], [1, 2, 3]],
|
||||
codes=[[0, 1, 0, 2], [2, 0, 0, 1]],
|
||||
names=["col1", "col2"],
|
||||
)
|
||||
recons = mi._sort_levels_monotonic()
|
||||
|
||||
# These are equal.
|
||||
assert mi.equals(recons)
|
||||
assert Index(mi.values).equals(Index(recons.values))
|
||||
|
||||
# _hashed_values and hash_pandas_object(..., index=False) equivalency.
|
||||
expected = hash_pandas_object(mi, index=False).values
|
||||
result = mi._hashed_values
|
||||
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
expected = hash_pandas_object(recons, index=False).values
|
||||
result = recons._hashed_values
|
||||
|
||||
tm.assert_numpy_array_equal(result, expected)
|
||||
|
||||
expected = mi._hashed_values
|
||||
result = recons._hashed_values
|
||||
|
||||
# Values should match, but in different order.
|
||||
tm.assert_numpy_array_equal(np.sort(result), np.sort(expected))
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"obj",
|
||||
[
|
||||
Series([1, 2, 3]),
|
||||
Series([1.0, 1.5, 3.2]),
|
||||
Series([1.0, 1.5, np.nan]),
|
||||
Series([1.0, 1.5, 3.2], index=[1.5, 1.1, 3.3]),
|
||||
Series(["a", "b", "c"]),
|
||||
Series(["a", np.nan, "c"]),
|
||||
Series(["a", None, "c"]),
|
||||
Series([True, False, True]),
|
||||
Series(),
|
||||
Index([1, 2, 3]),
|
||||
Index([True, False, True]),
|
||||
DataFrame({"x": ["a", "b", "c"], "y": [1, 2, 3]}),
|
||||
DataFrame(),
|
||||
tm.makeMissingDataframe(),
|
||||
tm.makeMixedDataFrame(),
|
||||
tm.makeTimeDataFrame(),
|
||||
tm.makeTimeSeries(),
|
||||
tm.makeTimedeltaIndex(),
|
||||
tm.makePeriodIndex(),
|
||||
Series(tm.makePeriodIndex()),
|
||||
Series(pd.date_range("20130101", periods=3, tz="US/Eastern")),
|
||||
MultiIndex.from_product(
|
||||
[range(5), ["foo", "bar", "baz"], pd.date_range("20130101", periods=2)]
|
||||
),
|
||||
MultiIndex.from_product([pd.CategoricalIndex(list("aabc")), range(3)]),
|
||||
],
|
||||
)
|
||||
def test_hash_pandas_object(obj, index):
|
||||
_check_equal(obj, index=index)
|
||||
_check_not_equal_with_index(obj)
|
||||
|
||||
|
||||
def test_hash_pandas_object2(series, index):
|
||||
_check_equal(series, index=index)
|
||||
_check_not_equal_with_index(series)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"obj", [Series([], dtype="float64"), Series([], dtype="object"), Index([])]
|
||||
)
|
||||
def test_hash_pandas_empty_object(obj, index):
|
||||
# These are by-definition the same with
|
||||
# or without the index as the data is empty.
|
||||
_check_equal(obj, index=index)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"s1",
|
||||
[
|
||||
Series(["a", "b", "c", "d"]),
|
||||
Series([1000, 2000, 3000, 4000]),
|
||||
Series(pd.date_range(0, periods=4)),
|
||||
],
|
||||
)
|
||||
@pytest.mark.parametrize("categorize", [True, False])
|
||||
def test_categorical_consistency(s1, categorize):
|
||||
# see gh-15143
|
||||
#
|
||||
# Check that categoricals hash consistent with their values,
|
||||
# not codes. This should work for categoricals of any dtype.
|
||||
s2 = s1.astype("category").cat.set_categories(s1)
|
||||
s3 = s2.cat.set_categories(list(reversed(s1)))
|
||||
|
||||
# These should all hash identically.
|
||||
h1 = hash_pandas_object(s1, categorize=categorize)
|
||||
h2 = hash_pandas_object(s2, categorize=categorize)
|
||||
h3 = hash_pandas_object(s3, categorize=categorize)
|
||||
|
||||
tm.assert_series_equal(h1, h2)
|
||||
tm.assert_series_equal(h1, h3)
|
||||
|
||||
|
||||
def test_categorical_with_nan_consistency():
|
||||
c = pd.Categorical.from_codes(
|
||||
[-1, 0, 1, 2, 3, 4], categories=pd.date_range("2012-01-01", periods=5, name="B")
|
||||
)
|
||||
expected = hash_array(c, categorize=False)
|
||||
|
||||
c = pd.Categorical.from_codes([-1, 0], categories=[pd.Timestamp("2012-01-01")])
|
||||
result = hash_array(c, categorize=False)
|
||||
|
||||
assert result[0] in expected
|
||||
assert result[1] in expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("obj", [pd.Timestamp("20130101")])
|
||||
def test_pandas_errors(obj):
|
||||
msg = "Unexpected type for hashing"
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
hash_pandas_object(obj)
|
||||
|
||||
|
||||
def test_hash_keys():
|
||||
# Using different hash keys, should have
|
||||
# different hashes for the same data.
|
||||
#
|
||||
# This only matters for object dtypes.
|
||||
obj = Series(list("abc"))
|
||||
|
||||
a = hash_pandas_object(obj, hash_key="9876543210123456")
|
||||
b = hash_pandas_object(obj, hash_key="9876543210123465")
|
||||
|
||||
assert (a != b).all()
|
||||
|
||||
|
||||
def test_invalid_key():
|
||||
# This only matters for object dtypes.
|
||||
msg = "key should be a 16-byte string encoded"
|
||||
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
hash_pandas_object(Series(list("abc")), hash_key="foo")
|
||||
|
||||
|
||||
def test_already_encoded(index):
|
||||
# If already encoded, then ok.
|
||||
obj = Series(list("abc")).str.encode("utf8")
|
||||
_check_equal(obj, index=index)
|
||||
|
||||
|
||||
def test_alternate_encoding(index):
|
||||
obj = Series(list("abc"))
|
||||
_check_equal(obj, index=index, encoding="ascii")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("l_exp", range(8))
|
||||
@pytest.mark.parametrize("l_add", [0, 1])
|
||||
def test_same_len_hash_collisions(l_exp, l_add):
|
||||
length = 2 ** (l_exp + 8) + l_add
|
||||
s = tm.rands_array(length, 2)
|
||||
|
||||
result = hash_array(s, "utf8")
|
||||
assert not result[0] == result[1]
|
||||
|
||||
|
||||
def test_hash_collisions():
|
||||
# Hash collisions are bad.
|
||||
#
|
||||
# https://github.com/pandas-dev/pandas/issues/14711#issuecomment-264885726
|
||||
hashes = [
|
||||
"Ingrid-9Z9fKIZmkO7i7Cn51Li34pJm44fgX6DYGBNj3VPlOH50m7HnBlPxfIwFMrcNJNMP6PSgLmwWnInciMWrCSAlLEvt7JkJl4IxiMrVbXSa8ZQoVaq5xoQPjltuJEfwdNlO6jo8qRRHvD8sBEBMQASrRa6TsdaPTPCBo3nwIBpE7YzzmyH0vMBhjQZLx1aCT7faSEx7PgFxQhHdKFWROcysamgy9iVj8DO2Fmwg1NNl93rIAqC3mdqfrCxrzfvIY8aJdzin2cHVzy3QUJxZgHvtUtOLxoqnUHsYbNTeq0xcLXpTZEZCxD4PGubIuCNf32c33M7HFsnjWSEjE2yVdWKhmSVodyF8hFYVmhYnMCztQnJrt3O8ZvVRXd5IKwlLexiSp4h888w7SzAIcKgc3g5XQJf6MlSMftDXm9lIsE1mJNiJEv6uY6pgvC3fUPhatlR5JPpVAHNSbSEE73MBzJrhCAbOLXQumyOXigZuPoME7QgJcBalliQol7YZ9", # noqa: E501
|
||||
"Tim-b9MddTxOWW2AT1Py6vtVbZwGAmYCjbp89p8mxsiFoVX4FyDOF3wFiAkyQTUgwg9sVqVYOZo09Dh1AzhFHbgij52ylF0SEwgzjzHH8TGY8Lypart4p4onnDoDvVMBa0kdthVGKl6K0BDVGzyOXPXKpmnMF1H6rJzqHJ0HywfwS4XYpVwlAkoeNsiicHkJUFdUAhG229INzvIAiJuAHeJDUoyO4DCBqtoZ5TDend6TK7Y914yHlfH3g1WZu5LksKv68VQHJriWFYusW5e6ZZ6dKaMjTwEGuRgdT66iU5nqWTHRH8WSzpXoCFwGcTOwyuqPSe0fTe21DVtJn1FKj9F9nEnR9xOvJUO7E0piCIF4Ad9yAIDY4DBimpsTfKXCu1vdHpKYerzbndfuFe5AhfMduLYZJi5iAw8qKSwR5h86ttXV0Mc0QmXz8dsRvDgxjXSmupPxBggdlqUlC828hXiTPD7am0yETBV0F3bEtvPiNJfremszcV8NcqAoARMe", # noqa: E501
|
||||
]
|
||||
|
||||
# These should be different.
|
||||
result1 = hash_array(np.asarray(hashes[0:1], dtype=object), "utf8")
|
||||
expected1 = np.array([14963968704024874985], dtype=np.uint64)
|
||||
tm.assert_numpy_array_equal(result1, expected1)
|
||||
|
||||
result2 = hash_array(np.asarray(hashes[1:2], dtype=object), "utf8")
|
||||
expected2 = np.array([16428432627716348016], dtype=np.uint64)
|
||||
tm.assert_numpy_array_equal(result2, expected2)
|
||||
|
||||
result = hash_array(np.asarray(hashes, dtype=object), "utf8")
|
||||
tm.assert_numpy_array_equal(result, np.concatenate([expected1, expected2], axis=0))
|
@@ -0,0 +1,44 @@
|
||||
import pytest
|
||||
|
||||
from pandas.util._move import BadMove, move_into_mutable_buffer, stolenbuf
|
||||
|
||||
|
||||
def test_cannot_create_instance_of_stolen_buffer():
|
||||
# Stolen buffers need to be created through the smart constructor
|
||||
# "move_into_mutable_buffer," which has a bunch of checks in it.
|
||||
|
||||
msg = "cannot create 'pandas.util._move.stolenbuf' instances"
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
stolenbuf()
|
||||
|
||||
|
||||
def test_more_than_one_ref():
|
||||
# Test case for when we try to use "move_into_mutable_buffer"
|
||||
# when the object being moved has other references.
|
||||
|
||||
b = b"testing"
|
||||
|
||||
with pytest.raises(BadMove, match="testing") as e:
|
||||
|
||||
def handle_success(type_, value, tb):
|
||||
assert value.args[0] is b
|
||||
return type(e).handle_success(e, type_, value, tb) # super
|
||||
|
||||
e.handle_success = handle_success
|
||||
move_into_mutable_buffer(b)
|
||||
|
||||
|
||||
def test_exactly_one_ref():
|
||||
# Test case for when the object being moved has exactly one reference.
|
||||
|
||||
b = b"testing"
|
||||
|
||||
# We need to pass an expression on the stack to ensure that there are
|
||||
# not extra references hanging around. We cannot rewrite this test as
|
||||
# buf = b[:-3]
|
||||
# as_stolen_buf = move_into_mutable_buffer(buf)
|
||||
# because then we would have more than one reference to buf.
|
||||
as_stolen_buf = move_into_mutable_buffer(b[:-3])
|
||||
|
||||
# Materialize as byte-array to show that it is mutable.
|
||||
assert bytearray(as_stolen_buf) == b"test"
|
@@ -0,0 +1,39 @@
|
||||
import sys
|
||||
import types
|
||||
|
||||
import pytest
|
||||
|
||||
import pandas.util._test_decorators as td
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["foo", "hello123"])
|
||||
def test_safe_import_non_existent(name):
|
||||
assert not td.safe_import(name)
|
||||
|
||||
|
||||
def test_safe_import_exists():
|
||||
assert td.safe_import("pandas")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("min_version,valid", [("0.0.0", True), ("99.99.99", False)])
|
||||
def test_safe_import_versions(min_version, valid):
|
||||
result = td.safe_import("pandas", min_version=min_version)
|
||||
result = result if valid else not result
|
||||
assert result
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"min_version,valid", [(None, False), ("1.0", True), ("2.0", False)]
|
||||
)
|
||||
def test_safe_import_dummy(monkeypatch, min_version, valid):
|
||||
mod_name = "hello123"
|
||||
|
||||
mod = types.ModuleType(mod_name)
|
||||
mod.__version__ = "1.5"
|
||||
|
||||
if min_version is not None:
|
||||
monkeypatch.setitem(sys.modules, mod_name, mod)
|
||||
|
||||
result = td.safe_import(mod_name, min_version=min_version)
|
||||
result = result if valid else not result
|
||||
assert result
|
107
venv/lib/python3.6/site-packages/pandas/tests/util/test_util.py
Normal file
107
venv/lib/python3.6/site-packages/pandas/tests/util/test_util.py
Normal file
@@ -0,0 +1,107 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
|
||||
import pandas.compat as compat
|
||||
from pandas.compat import raise_with_traceback
|
||||
|
||||
import pandas.util.testing as tm
|
||||
|
||||
|
||||
def test_rands():
|
||||
r = tm.rands(10)
|
||||
assert len(r) == 10
|
||||
|
||||
|
||||
def test_rands_array_1d():
|
||||
arr = tm.rands_array(5, size=10)
|
||||
assert arr.shape == (10,)
|
||||
assert len(arr[0]) == 5
|
||||
|
||||
|
||||
def test_rands_array_2d():
|
||||
arr = tm.rands_array(7, size=(10, 10))
|
||||
assert arr.shape == (10, 10)
|
||||
assert len(arr[1, 1]) == 7
|
||||
|
||||
|
||||
def test_numpy_err_state_is_default():
|
||||
expected = {"over": "warn", "divide": "warn", "invalid": "warn", "under": "ignore"}
|
||||
import numpy as np
|
||||
|
||||
# The error state should be unchanged after that import.
|
||||
assert np.geterr() == expected
|
||||
|
||||
|
||||
def test_raise_with_traceback():
|
||||
with pytest.raises(LookupError, match="error_text"):
|
||||
try:
|
||||
raise ValueError("THIS IS AN ERROR")
|
||||
except ValueError:
|
||||
e = LookupError("error_text")
|
||||
raise_with_traceback(e)
|
||||
|
||||
with pytest.raises(LookupError, match="error_text"):
|
||||
try:
|
||||
raise ValueError("This is another error")
|
||||
except ValueError:
|
||||
e = LookupError("error_text")
|
||||
_, _, traceback = sys.exc_info()
|
||||
raise_with_traceback(e, traceback)
|
||||
|
||||
|
||||
def test_convert_rows_list_to_csv_str():
|
||||
rows_list = ["aaa", "bbb", "ccc"]
|
||||
ret = tm.convert_rows_list_to_csv_str(rows_list)
|
||||
|
||||
if compat.is_platform_windows():
|
||||
expected = "aaa\r\nbbb\r\nccc\r\n"
|
||||
else:
|
||||
expected = "aaa\nbbb\nccc\n"
|
||||
|
||||
assert ret == expected
|
||||
|
||||
|
||||
def test_create_temp_directory():
|
||||
with tm.ensure_clean_dir() as path:
|
||||
assert os.path.exists(path)
|
||||
assert os.path.isdir(path)
|
||||
assert not os.path.exists(path)
|
||||
|
||||
|
||||
def test_assert_raises_regex_deprecated():
|
||||
# see gh-23592
|
||||
|
||||
with tm.assert_produces_warning(FutureWarning):
|
||||
msg = "Not equal!"
|
||||
|
||||
with tm.assert_raises_regex(AssertionError, msg):
|
||||
assert 1 == 2, msg
|
||||
|
||||
|
||||
@pytest.mark.parametrize("strict_data_files", [True, False])
|
||||
def test_datapath_missing(datapath):
|
||||
with pytest.raises(ValueError, match="Could not find file"):
|
||||
datapath("not_a_file")
|
||||
|
||||
|
||||
def test_datapath(datapath):
|
||||
args = ("data", "iris.csv")
|
||||
|
||||
result = datapath(*args)
|
||||
expected = os.path.join(os.path.dirname(os.path.dirname(__file__)), *args)
|
||||
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_rng_context():
|
||||
import numpy as np
|
||||
|
||||
expected0 = 1.764052345967664
|
||||
expected1 = 1.6243453636632417
|
||||
|
||||
with tm.RNGContext(0):
|
||||
with tm.RNGContext(1):
|
||||
assert np.random.randn() == expected1
|
||||
assert np.random.randn() == expected0
|
@@ -0,0 +1,80 @@
|
||||
from collections import OrderedDict
|
||||
|
||||
import pytest
|
||||
|
||||
from pandas.util._validators import validate_args
|
||||
|
||||
_fname = "func"
|
||||
|
||||
|
||||
def test_bad_min_fname_arg_count():
|
||||
msg = "'max_fname_arg_count' must be non-negative"
|
||||
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
validate_args(_fname, (None,), -1, "foo")
|
||||
|
||||
|
||||
def test_bad_arg_length_max_value_single():
|
||||
args = (None, None)
|
||||
compat_args = ("foo",)
|
||||
|
||||
min_fname_arg_count = 0
|
||||
max_length = len(compat_args) + min_fname_arg_count
|
||||
actual_length = len(args) + min_fname_arg_count
|
||||
msg = (
|
||||
r"{fname}\(\) takes at most {max_length} "
|
||||
r"argument \({actual_length} given\)".format(
|
||||
fname=_fname, max_length=max_length, actual_length=actual_length
|
||||
)
|
||||
)
|
||||
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
validate_args(_fname, args, min_fname_arg_count, compat_args)
|
||||
|
||||
|
||||
def test_bad_arg_length_max_value_multiple():
|
||||
args = (None, None)
|
||||
compat_args = dict(foo=None)
|
||||
|
||||
min_fname_arg_count = 2
|
||||
max_length = len(compat_args) + min_fname_arg_count
|
||||
actual_length = len(args) + min_fname_arg_count
|
||||
msg = (
|
||||
r"{fname}\(\) takes at most {max_length} "
|
||||
r"arguments \({actual_length} given\)".format(
|
||||
fname=_fname, max_length=max_length, actual_length=actual_length
|
||||
)
|
||||
)
|
||||
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
validate_args(_fname, args, min_fname_arg_count, compat_args)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("i", range(1, 3))
|
||||
def test_not_all_defaults(i):
|
||||
bad_arg = "foo"
|
||||
msg = (
|
||||
"the '{arg}' parameter is not supported "
|
||||
r"in the pandas implementation of {func}\(\)".format(arg=bad_arg, func=_fname)
|
||||
)
|
||||
|
||||
compat_args = OrderedDict()
|
||||
compat_args["foo"] = 2
|
||||
compat_args["bar"] = -1
|
||||
compat_args["baz"] = 3
|
||||
|
||||
arg_vals = (1, -1, 3)
|
||||
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
validate_args(_fname, arg_vals[:i], 2, compat_args)
|
||||
|
||||
|
||||
def test_validation():
|
||||
# No exceptions should be raised.
|
||||
validate_args(_fname, (None,), 2, dict(out=None))
|
||||
|
||||
compat_args = OrderedDict()
|
||||
compat_args["axis"] = 1
|
||||
compat_args["out"] = None
|
||||
|
||||
validate_args(_fname, (1, None), 2, compat_args)
|
@@ -0,0 +1,98 @@
|
||||
from collections import OrderedDict
|
||||
|
||||
import pytest
|
||||
|
||||
from pandas.util._validators import validate_args_and_kwargs
|
||||
|
||||
_fname = "func"
|
||||
|
||||
|
||||
def test_invalid_total_length_max_length_one():
|
||||
compat_args = ("foo",)
|
||||
kwargs = {"foo": "FOO"}
|
||||
args = ("FoO", "BaZ")
|
||||
|
||||
min_fname_arg_count = 0
|
||||
max_length = len(compat_args) + min_fname_arg_count
|
||||
actual_length = len(kwargs) + len(args) + min_fname_arg_count
|
||||
|
||||
msg = (
|
||||
r"{fname}\(\) takes at most {max_length} "
|
||||
r"argument \({actual_length} given\)".format(
|
||||
fname=_fname, max_length=max_length, actual_length=actual_length
|
||||
)
|
||||
)
|
||||
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count, compat_args)
|
||||
|
||||
|
||||
def test_invalid_total_length_max_length_multiple():
|
||||
compat_args = ("foo", "bar", "baz")
|
||||
kwargs = {"foo": "FOO", "bar": "BAR"}
|
||||
args = ("FoO", "BaZ")
|
||||
|
||||
min_fname_arg_count = 2
|
||||
max_length = len(compat_args) + min_fname_arg_count
|
||||
actual_length = len(kwargs) + len(args) + min_fname_arg_count
|
||||
|
||||
msg = (
|
||||
r"{fname}\(\) takes at most {max_length} "
|
||||
r"arguments \({actual_length} given\)".format(
|
||||
fname=_fname, max_length=max_length, actual_length=actual_length
|
||||
)
|
||||
)
|
||||
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count, compat_args)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("args,kwargs", [((), {"foo": -5, "bar": 2}), ((-5, 2), {})])
|
||||
def test_missing_args_or_kwargs(args, kwargs):
|
||||
bad_arg = "bar"
|
||||
min_fname_arg_count = 2
|
||||
|
||||
compat_args = OrderedDict()
|
||||
compat_args["foo"] = -5
|
||||
compat_args[bad_arg] = 1
|
||||
|
||||
msg = (
|
||||
r"the '{arg}' parameter is not supported "
|
||||
r"in the pandas implementation of {func}\(\)".format(arg=bad_arg, func=_fname)
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count, compat_args)
|
||||
|
||||
|
||||
def test_duplicate_argument():
|
||||
min_fname_arg_count = 2
|
||||
|
||||
compat_args = OrderedDict()
|
||||
compat_args["foo"] = None
|
||||
compat_args["bar"] = None
|
||||
compat_args["baz"] = None
|
||||
|
||||
kwargs = {"foo": None, "bar": None}
|
||||
args = (None,) # duplicate value for "foo"
|
||||
|
||||
msg = r"{fname}\(\) got multiple values for keyword " r"argument '{arg}'".format(
|
||||
fname=_fname, arg="foo"
|
||||
)
|
||||
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count, compat_args)
|
||||
|
||||
|
||||
def test_validation():
|
||||
# No exceptions should be raised.
|
||||
compat_args = OrderedDict()
|
||||
compat_args["foo"] = 1
|
||||
compat_args["bar"] = None
|
||||
compat_args["baz"] = -2
|
||||
kwargs = {"baz": -2}
|
||||
|
||||
args = (1, None)
|
||||
min_fname_arg_count = 2
|
||||
|
||||
validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count, compat_args)
|
@@ -0,0 +1,75 @@
|
||||
from collections import OrderedDict
|
||||
|
||||
import pytest
|
||||
|
||||
from pandas.util._validators import validate_bool_kwarg, validate_kwargs
|
||||
|
||||
_fname = "func"
|
||||
|
||||
|
||||
def test_bad_kwarg():
|
||||
good_arg = "f"
|
||||
bad_arg = good_arg + "o"
|
||||
|
||||
compat_args = OrderedDict()
|
||||
compat_args[good_arg] = "foo"
|
||||
compat_args[bad_arg + "o"] = "bar"
|
||||
kwargs = {good_arg: "foo", bad_arg: "bar"}
|
||||
|
||||
msg = r"{fname}\(\) got an unexpected " r"keyword argument '{arg}'".format(
|
||||
fname=_fname, arg=bad_arg
|
||||
)
|
||||
|
||||
with pytest.raises(TypeError, match=msg):
|
||||
validate_kwargs(_fname, kwargs, compat_args)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("i", range(1, 3))
|
||||
def test_not_all_none(i):
|
||||
bad_arg = "foo"
|
||||
msg = (
|
||||
r"the '{arg}' parameter is not supported "
|
||||
r"in the pandas implementation of {func}\(\)".format(arg=bad_arg, func=_fname)
|
||||
)
|
||||
|
||||
compat_args = OrderedDict()
|
||||
compat_args["foo"] = 1
|
||||
compat_args["bar"] = "s"
|
||||
compat_args["baz"] = None
|
||||
|
||||
kwarg_keys = ("foo", "bar", "baz")
|
||||
kwarg_vals = (2, "s", None)
|
||||
|
||||
kwargs = dict(zip(kwarg_keys[:i], kwarg_vals[:i]))
|
||||
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
validate_kwargs(_fname, kwargs, compat_args)
|
||||
|
||||
|
||||
def test_validation():
|
||||
# No exceptions should be raised.
|
||||
compat_args = OrderedDict()
|
||||
compat_args["f"] = None
|
||||
compat_args["b"] = 1
|
||||
compat_args["ba"] = "s"
|
||||
|
||||
kwargs = dict(f=None, b=1)
|
||||
validate_kwargs(_fname, kwargs, compat_args)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["inplace", "copy"])
|
||||
@pytest.mark.parametrize("value", [1, "True", [1, 2, 3], 5.0])
|
||||
def test_validate_bool_kwarg_fail(name, value):
|
||||
msg = 'For argument "%s" expected type bool, received type %s' % (
|
||||
name,
|
||||
type(value).__name__,
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match=msg):
|
||||
validate_bool_kwarg(value, name)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("name", ["inplace", "copy"])
|
||||
@pytest.mark.parametrize("value", [True, False, None])
|
||||
def test_validate_bool_kwarg(name, value):
|
||||
assert validate_bool_kwarg(value, name) == value
|
Reference in New Issue
Block a user