Skip to content

CLN: Consistent pandas.util.testing imports in pandas/tests/util #29278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions pandas/tests/util/test_assert_almost_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

from pandas import DataFrame, Index, Series, Timestamp
from pandas.util.testing import assert_almost_equal
import pandas.util.testing as tm


def _assert_almost_equal_both(a, b, **kwargs):
Expand All @@ -18,10 +18,10 @@ def _assert_almost_equal_both(a, b, **kwargs):
b : object
The second object to compare.
kwargs : dict
The arguments passed to `assert_almost_equal`.
The arguments passed to `tm.assert_almost_equal`.
"""
assert_almost_equal(a, b, **kwargs)
assert_almost_equal(b, a, **kwargs)
tm.assert_almost_equal(a, b, **kwargs)
tm.assert_almost_equal(b, a, **kwargs)


def _assert_not_almost_equal(a, b, **kwargs):
Expand All @@ -35,10 +35,10 @@ def _assert_not_almost_equal(a, b, **kwargs):
b : object
The second object to compare.
kwargs : dict
The arguments passed to `assert_almost_equal`.
The arguments passed to `tm.assert_almost_equal`.
"""
try:
assert_almost_equal(a, b, **kwargs)
tm.assert_almost_equal(a, b, **kwargs)
msg = (
"{a} and {b} were approximately equal when they shouldn't have been"
).format(a=a, b=b)
Expand Down Expand Up @@ -240,7 +240,7 @@ 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)
tm.assert_almost_equal(1, 2)


@pytest.mark.parametrize(
Expand All @@ -257,7 +257,7 @@ def test_assert_almost_equal_class_mismatch(a, b, klass1, klass2):
)

with pytest.raises(AssertionError, match=msg):
assert_almost_equal(a, b)
tm.assert_almost_equal(a, b)


def test_assert_almost_equal_value_mismatch1():
Expand All @@ -268,7 +268,7 @@ def test_assert_almost_equal_value_mismatch1():
\\[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]))
tm.assert_almost_equal(np.array([np.nan, 2, 3]), np.array([1, np.nan, 3]))


def test_assert_almost_equal_value_mismatch2():
Expand All @@ -279,7 +279,7 @@ def test_assert_almost_equal_value_mismatch2():
\\[right\\]: \\[1, 3\\]"""

with pytest.raises(AssertionError, match=msg):
assert_almost_equal(np.array([1, 2]), np.array([1, 3]))
tm.assert_almost_equal(np.array([1, 2]), np.array([1, 3]))


def test_assert_almost_equal_value_mismatch3():
Expand All @@ -290,7 +290,7 @@ def test_assert_almost_equal_value_mismatch3():
\\[right\\]: \\[\\[1, 3\\], \\[3, 4\\], \\[5, 6\\]\\]"""

with pytest.raises(AssertionError, match=msg):
assert_almost_equal(
tm.assert_almost_equal(
np.array([[1, 2], [3, 4], [5, 6]]), np.array([[1, 3], [3, 4], [5, 6]])
)

Expand All @@ -303,7 +303,7 @@ def test_assert_almost_equal_value_mismatch4():
\\[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]]))
tm.assert_almost_equal(np.array([[1, 2], [3, 4]]), np.array([[1, 3], [3, 4]]))


def test_assert_almost_equal_shape_mismatch_override():
Expand All @@ -313,7 +313,7 @@ def test_assert_almost_equal_shape_mismatch_override():
\\[left\\]: \\(2L*,\\)
\\[right\\]: \\(3L*,\\)"""
with pytest.raises(AssertionError, match=msg):
assert_almost_equal(np.array([1, 2]), np.array([3, 4, 5]), obj="Index")
tm.assert_almost_equal(np.array([1, 2]), np.array([3, 4, 5]), obj="Index")


def test_assert_almost_equal_unicode():
Expand All @@ -325,7 +325,7 @@ def test_assert_almost_equal_unicode():
\\[right\\]: \\[á, à, å\\]"""

with pytest.raises(AssertionError, match=msg):
assert_almost_equal(np.array(["á", "à", "ä"]), np.array(["á", "à", "å"]))
tm.assert_almost_equal(np.array(["á", "à", "ä"]), np.array(["á", "à", "å"]))


def test_assert_almost_equal_timestamp():
Expand All @@ -339,7 +339,7 @@ def test_assert_almost_equal_timestamp():
\\[right\\]: \\[2011-01-01 00:00:00, 2011-01-02 00:00:00\\]"""

with pytest.raises(AssertionError, match=msg):
assert_almost_equal(a, b)
tm.assert_almost_equal(a, b)


def test_assert_almost_equal_iterable_length_mismatch():
Expand All @@ -350,7 +350,7 @@ def test_assert_almost_equal_iterable_length_mismatch():
\\[right\\]: 3"""

with pytest.raises(AssertionError, match=msg):
assert_almost_equal([1, 2], [3, 4, 5])
tm.assert_almost_equal([1, 2], [3, 4, 5])


def test_assert_almost_equal_iterable_values_mismatch():
Expand All @@ -361,4 +361,4 @@ def test_assert_almost_equal_iterable_values_mismatch():
\\[right\\]: \\[1, 3\\]"""

with pytest.raises(AssertionError, match=msg):
assert_almost_equal([1, 2], [1, 3])
tm.assert_almost_equal([1, 2], [1, 3])
16 changes: 8 additions & 8 deletions pandas/tests/util/test_assert_categorical_equal.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import pytest

from pandas import Categorical
from pandas.util.testing import assert_categorical_equal
import pandas.util.testing as tm


@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)
tm.assert_categorical_equal(c, c)


@pytest.mark.parametrize("check_category_order", [True, False])
Expand All @@ -25,9 +25,9 @@ def test_categorical_equal_order_mismatch(check_category_order):
\\[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)
tm.assert_categorical_equal(c1, c2, **kwargs)
else:
assert_categorical_equal(c1, c2, **kwargs)
tm.assert_categorical_equal(c1, c2, **kwargs)


def test_categorical_equal_categories_mismatch():
Expand All @@ -41,7 +41,7 @@ def test_categorical_equal_categories_mismatch():
c2 = Categorical([1, 2, 3, 5])

with pytest.raises(AssertionError, match=msg):
assert_categorical_equal(c1, c2)
tm.assert_categorical_equal(c1, c2)


def test_categorical_equal_codes_mismatch():
Expand All @@ -56,7 +56,7 @@ def test_categorical_equal_codes_mismatch():
c2 = Categorical([1, 2, 3, 4], categories=categories)

with pytest.raises(AssertionError, match=msg):
assert_categorical_equal(c1, c2)
tm.assert_categorical_equal(c1, c2)


def test_categorical_equal_ordered_mismatch():
Expand All @@ -71,7 +71,7 @@ def test_categorical_equal_ordered_mismatch():
c2 = Categorical(data, ordered=True)

with pytest.raises(AssertionError, match=msg):
assert_categorical_equal(c1, c2)
tm.assert_categorical_equal(c1, c2)


@pytest.mark.parametrize("obj", ["index", "foo", "pandas"])
Expand All @@ -89,4 +89,4 @@ def test_categorical_equal_object_override(obj):
c2 = Categorical(data, ordered=True)

with pytest.raises(AssertionError, match=msg):
assert_categorical_equal(c1, c2, obj=obj)
tm.assert_categorical_equal(c1, c2, obj=obj)
18 changes: 9 additions & 9 deletions pandas/tests/util/test_assert_extension_array_equal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import pytest

from pandas.core.arrays.sparse import SparseArray
from pandas.util.testing import assert_extension_array_equal
import pandas.util.testing as tm


@pytest.mark.parametrize(
Expand All @@ -27,9 +27,9 @@ def test_assert_extension_array_equal_not_exact(kwargs):
\\[right\\]: \\[-0\\.17387645482.*, 0\\.341414801642.*\\]"""

with pytest.raises(AssertionError, match=msg):
assert_extension_array_equal(arr1, arr2, **kwargs)
tm.assert_extension_array_equal(arr1, arr2, **kwargs)
else:
assert_extension_array_equal(arr1, arr2, **kwargs)
tm.assert_extension_array_equal(arr1, arr2, **kwargs)


@pytest.mark.parametrize(
Expand All @@ -50,9 +50,9 @@ def test_assert_extension_array_equal_less_precise(check_less_precise):
\\[right\\]: \\[0\\.5, 0\\.123457\\]"""

with pytest.raises(AssertionError, match=msg):
assert_extension_array_equal(arr1, arr2, **kwargs)
tm.assert_extension_array_equal(arr1, arr2, **kwargs)
else:
assert_extension_array_equal(arr1, arr2, **kwargs)
tm.assert_extension_array_equal(arr1, arr2, **kwargs)


def test_assert_extension_array_equal_dtype_mismatch(check_dtype):
Expand All @@ -71,9 +71,9 @@ def test_assert_extension_array_equal_dtype_mismatch(check_dtype):
\\[right\\]: Sparse\\[int32, 0\\]"""

with pytest.raises(AssertionError, match=msg):
assert_extension_array_equal(arr1, arr2, **kwargs)
tm.assert_extension_array_equal(arr1, arr2, **kwargs)
else:
assert_extension_array_equal(arr1, arr2, **kwargs)
tm.assert_extension_array_equal(arr1, arr2, **kwargs)


def test_assert_extension_array_equal_missing_values():
Expand All @@ -88,7 +88,7 @@ def test_assert_extension_array_equal_missing_values():
\\[right\\]: \\[True, False, False, False\\]"""

with pytest.raises(AssertionError, match=msg):
assert_extension_array_equal(arr1, arr2)
tm.assert_extension_array_equal(arr1, arr2)


@pytest.mark.parametrize("side", ["left", "right"])
Expand All @@ -104,4 +104,4 @@ def test_assert_extension_array_equal_non_extension_array(side):
)

with pytest.raises(AssertionError, match=msg):
assert_extension_array_equal(*args)
tm.assert_extension_array_equal(*args)
36 changes: 18 additions & 18 deletions pandas/tests/util/test_assert_frame_equal.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest

from pandas import DataFrame
from pandas.util.testing import assert_frame_equal
import pandas.util.testing as tm


@pytest.fixture(params=[True, False])
Expand All @@ -27,10 +27,10 @@ def _assert_frame_equal_both(a, b, **kwargs):
b : DataFrame
The second DataFrame to compare.
kwargs : dict
The arguments passed to `assert_frame_equal`.
The arguments passed to `tm.assert_frame_equal`.
"""
assert_frame_equal(a, b, **kwargs)
assert_frame_equal(b, a, **kwargs)
tm.assert_frame_equal(a, b, **kwargs)
tm.assert_frame_equal(b, a, **kwargs)


def _assert_not_frame_equal(a, b, **kwargs):
Expand All @@ -44,10 +44,10 @@ def _assert_not_frame_equal(a, b, **kwargs):
b : DataFrame
The second DataFrame to compare.
kwargs : dict
The arguments passed to `assert_frame_equal`.
The arguments passed to `tm.assert_frame_equal`.
"""
try:
assert_frame_equal(a, b, **kwargs)
tm.assert_frame_equal(a, b, **kwargs)
msg = "The two DataFrames were equal when they shouldn't have been"

pytest.fail(msg=msg)
Expand All @@ -68,7 +68,7 @@ def _assert_not_frame_equal_both(a, b, **kwargs):
b : DataFrame
The second DataFrame to compare.
kwargs : dict
The arguments passed to `assert_frame_equal`.
The arguments passed to `tm.assert_frame_equal`.
"""
_assert_not_frame_equal(a, b, **kwargs)
_assert_not_frame_equal(b, a, **kwargs)
Expand All @@ -82,7 +82,7 @@ def test_frame_equal_row_order_mismatch(check_like, obj_fixture):
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)
tm.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)

Expand All @@ -98,7 +98,7 @@ 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)
tm.assert_frame_equal(df1, df2, obj=obj_fixture)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -127,9 +127,9 @@ def test_frame_equal_index_dtype_mismatch(df1, df2, msg, check_index_type):

if check_index_type:
with pytest.raises(AssertionError, match=msg):
assert_frame_equal(df1, df2, **kwargs)
tm.assert_frame_equal(df1, df2, **kwargs)
else:
assert_frame_equal(df1, df2, **kwargs)
tm.assert_frame_equal(df1, df2, **kwargs)


def test_empty_dtypes(check_dtype):
Expand All @@ -143,9 +143,9 @@ def test_empty_dtypes(check_dtype):
if check_dtype:
msg = r"Attributes of DataFrame\..* are different"
with pytest.raises(AssertionError, match=msg):
assert_frame_equal(df1, df2, **kwargs)
tm.assert_frame_equal(df1, df2, **kwargs)
else:
assert_frame_equal(df1, df2, **kwargs)
tm.assert_frame_equal(df1, df2, **kwargs)


def test_frame_equal_index_mismatch(obj_fixture):
Expand All @@ -161,7 +161,7 @@ def test_frame_equal_index_mismatch(obj_fixture):
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)
tm.assert_frame_equal(df1, df2, obj=obj_fixture)


def test_frame_equal_columns_mismatch(obj_fixture):
Expand All @@ -177,7 +177,7 @@ def test_frame_equal_columns_mismatch(obj_fixture):
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)
tm.assert_frame_equal(df1, df2, obj=obj_fixture)


def test_frame_equal_block_mismatch(by_blocks_fixture, obj_fixture):
Expand All @@ -193,7 +193,7 @@ def test_frame_equal_block_mismatch(by_blocks_fixture, obj_fixture):
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)
tm.assert_frame_equal(df1, df2, by_blocks=by_blocks_fixture, obj=obj_fixture)


@pytest.mark.parametrize(
Expand Down Expand Up @@ -222,8 +222,8 @@ def test_frame_equal_block_mismatch(by_blocks_fixture, obj_fixture):
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
# Test ensures that `tm.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)
tm.assert_frame_equal(df1, df2, by_blocks=by_blocks_fixture, obj=obj_fixture)
Loading