diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 61879e71702d09..1c50c94b764207 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -253,15 +253,6 @@ repos: |os\.remove files: ^pandas/tests/ types_or: [python, cython, rst] - - id: unwanted-patterns-in-ea-tests - name: Unwanted patterns in EA tests - language: pygrep - entry: | - (?x) - tm.assert_(series|frame)_equal - files: ^pandas/tests/extension/base/ - exclude: ^pandas/tests/extension/base/base\.py$ - types_or: [python, cython, rst] - id: unwanted-patterns-in-cython name: Unwanted patterns in Cython code language: pygrep diff --git a/pandas/tests/extension/base/__init__.py b/pandas/tests/extension/base/__init__.py index dec41150a451c3..0e9a35b9f07e80 100644 --- a/pandas/tests/extension/base/__init__.py +++ b/pandas/tests/extension/base/__init__.py @@ -33,13 +33,6 @@ class TestMyDtype(BaseDtypeTests): ``BaseDtypeTests``. pytest's fixture discover will supply your ``dtype`` wherever the test requires it. You're free to implement additional tests. -All the tests in these modules use ``self.assert_frame_equal`` or -``self.assert_series_equal`` for dataframe or series comparisons. By default, -they use the usual ``pandas.testing.assert_frame_equal`` and -``pandas.testing.assert_series_equal``. You can override the checks used -by defining the staticmethods ``assert_frame_equal`` and -``assert_series_equal`` on your base test class. - """ from pandas.tests.extension.base.accumulate import BaseAccumulateTests # noqa: F401 from pandas.tests.extension.base.casting import BaseCastingTests # noqa: F401 diff --git a/pandas/tests/extension/base/accumulate.py b/pandas/tests/extension/base/accumulate.py index 868172f9308449..6774fcc27f35ca 100644 --- a/pandas/tests/extension/base/accumulate.py +++ b/pandas/tests/extension/base/accumulate.py @@ -1,6 +1,7 @@ import pytest import pandas as pd +import pandas._testing as tm from pandas.tests.extension.base.base import BaseExtensionTests @@ -20,7 +21,7 @@ def check_accumulate(self, s, op_name, skipna): ) expected = getattr(s.astype("float64"), op_name)(skipna=skipna) - self.assert_series_equal(result, expected, check_dtype=False) + tm.assert_series_equal(result, expected, check_dtype=False) @pytest.mark.parametrize("skipna", [True, False]) def test_accumulate_series_raises(self, data, all_numeric_accumulations, skipna): diff --git a/pandas/tests/extension/base/casting.py b/pandas/tests/extension/base/casting.py index 5a6b0d38e50555..da784ec7f549c0 100644 --- a/pandas/tests/extension/base/casting.py +++ b/pandas/tests/extension/base/casting.py @@ -4,6 +4,7 @@ import pandas.util._test_decorators as td import pandas as pd +import pandas._testing as tm from pandas.core.internals.blocks import NumpyBlock from pandas.tests.extension.base.base import BaseExtensionTests @@ -45,7 +46,7 @@ def test_tolist(self, data): def test_astype_str(self, data): result = pd.Series(data[:5]).astype(str) expected = pd.Series([str(x) for x in data[:5]], dtype=str) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize( "nullable_string_dtype", @@ -61,22 +62,22 @@ def test_astype_string(self, data, nullable_string_dtype): [str(x) if not isinstance(x, bytes) else x.decode() for x in data[:5]], dtype=nullable_string_dtype, ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_to_numpy(self, data): expected = np.asarray(data) result = data.to_numpy() - self.assert_equal(result, expected) + tm.assert_equal(result, expected) result = pd.Series(data).to_numpy() - self.assert_equal(result, expected) + tm.assert_equal(result, expected) def test_astype_empty_dataframe(self, dtype): # https://github.com/pandas-dev/pandas/issues/33113 df = pd.DataFrame() result = df.astype(dtype) - self.assert_frame_equal(result, df) + tm.assert_frame_equal(result, df) @pytest.mark.parametrize("copy", [True, False]) def test_astype_own_type(self, data, copy): @@ -84,4 +85,4 @@ def test_astype_own_type(self, data, copy): # https://github.com/pandas-dev/pandas/issues/28488 result = data.astype(data.dtype, copy=copy) assert (result is data) is (not copy) - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) diff --git a/pandas/tests/extension/base/constructors.py b/pandas/tests/extension/base/constructors.py index 26716922da8fa9..0e276f647d06ce 100644 --- a/pandas/tests/extension/base/constructors.py +++ b/pandas/tests/extension/base/constructors.py @@ -2,6 +2,7 @@ import pytest import pandas as pd +import pandas._testing as tm from pandas.api.extensions import ExtensionArray from pandas.core.internals.blocks import EABackedBlock from pandas.tests.extension.base.base import BaseExtensionTests @@ -10,11 +11,11 @@ class BaseConstructorsTests(BaseExtensionTests): def test_from_sequence_from_cls(self, data): result = type(data)._from_sequence(data, dtype=data.dtype) - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) data = data[:0] result = type(data)._from_sequence(data, dtype=data.dtype) - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) def test_array_from_scalars(self, data): scalars = [data[0], data[1], data[2]] @@ -38,27 +39,27 @@ def test_series_constructor(self, data): def test_series_constructor_no_data_with_index(self, dtype, na_value): result = pd.Series(index=[1, 2, 3], dtype=dtype) expected = pd.Series([na_value] * 3, index=[1, 2, 3], dtype=dtype) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) # GH 33559 - empty index result = pd.Series(index=[], dtype=dtype) expected = pd.Series([], index=pd.Index([], dtype="object"), dtype=dtype) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_series_constructor_scalar_na_with_index(self, dtype, na_value): result = pd.Series(na_value, index=[1, 2, 3], dtype=dtype) expected = pd.Series([na_value] * 3, index=[1, 2, 3], dtype=dtype) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_series_constructor_scalar_with_index(self, data, dtype): scalar = data[0] result = pd.Series(scalar, index=[1, 2, 3], dtype=dtype) expected = pd.Series([scalar] * 3, index=[1, 2, 3], dtype=dtype) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) result = pd.Series(scalar, index=["foo"], dtype=dtype) expected = pd.Series([scalar], index=["foo"], dtype=dtype) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize("from_series", [True, False]) def test_dataframe_constructor_from_dict(self, data, from_series): @@ -90,30 +91,30 @@ def test_from_dtype(self, data): expected = pd.Series(data) result = pd.Series(list(data), dtype=dtype) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) result = pd.Series(list(data), dtype=str(dtype)) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) # gh-30280 expected = pd.DataFrame(data).astype(dtype) result = pd.DataFrame(list(data), dtype=dtype) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) result = pd.DataFrame(list(data), dtype=str(dtype)) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_pandas_array(self, data): # pd.array(extension_array) should be idempotent... result = pd.array(data) - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) def test_pandas_array_dtype(self, data): # ... but specifying dtype will override idempotency result = pd.array(data, dtype=np.dtype(object)) expected = pd.arrays.NumpyExtensionArray(np.asarray(data, dtype=object)) - self.assert_equal(result, expected) + tm.assert_equal(result, expected) def test_construct_empty_dataframe(self, dtype): # GH 33623 @@ -121,7 +122,7 @@ def test_construct_empty_dataframe(self, dtype): expected = pd.DataFrame( {"a": pd.array([], dtype=dtype)}, index=pd.RangeIndex(0) ) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_empty(self, dtype): cls = dtype.construct_array_type() diff --git a/pandas/tests/extension/base/dim2.py b/pandas/tests/extension/base/dim2.py index 9dcce28f47e528..0d91641612c2b8 100644 --- a/pandas/tests/extension/base/dim2.py +++ b/pandas/tests/extension/base/dim2.py @@ -12,6 +12,7 @@ ) import pandas as pd +import pandas._testing as tm from pandas.core.arrays.integer import NUMPY_INT_TO_DTYPE from pandas.tests.extension.base.base import BaseExtensionTests @@ -32,14 +33,14 @@ def test_frame_from_2d_array(self, data): df = pd.DataFrame(arr2d) expected = pd.DataFrame({0: arr2d[:, 0], 1: arr2d[:, 1]}) - self.assert_frame_equal(df, expected) + tm.assert_frame_equal(df, expected) def test_swapaxes(self, data): arr2d = data.repeat(2).reshape(-1, 2) result = arr2d.swapaxes(0, 1) expected = arr2d.T - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_delete_2d(self, data): arr2d = data.repeat(3).reshape(-1, 3) @@ -47,12 +48,12 @@ def test_delete_2d(self, data): # axis = 0 result = arr2d.delete(1, axis=0) expected = data.delete(1).repeat(3).reshape(-1, 3) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) # axis = 1 result = arr2d.delete(1, axis=1) expected = data.repeat(2).reshape(-1, 2) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_take_2d(self, data): arr2d = data.reshape(-1, 1) @@ -60,7 +61,7 @@ def test_take_2d(self, data): result = arr2d.take([0, 0, -1], axis=0) expected = data.take([0, 0, -1]).reshape(-1, 1) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_repr_2d(self, data): # this could fail in a corner case where an element contained the name @@ -88,7 +89,7 @@ def test_getitem_2d(self, data): arr2d = data.reshape(1, -1) result = arr2d[0] - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) with pytest.raises(IndexError): arr2d[1] @@ -97,18 +98,18 @@ def test_getitem_2d(self, data): arr2d[-2] result = arr2d[:] - self.assert_extension_array_equal(result, arr2d) + tm.assert_extension_array_equal(result, arr2d) result = arr2d[:, :] - self.assert_extension_array_equal(result, arr2d) + tm.assert_extension_array_equal(result, arr2d) result = arr2d[:, 0] expected = data[[0]] - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) # dimension-expanding getitem on 1D result = data[:, np.newaxis] - self.assert_extension_array_equal(result, arr2d.T) + tm.assert_extension_array_equal(result, arr2d.T) def test_iter_2d(self, data): arr2d = data.reshape(1, -1) @@ -140,13 +141,13 @@ def test_concat_2d(self, data): # axis=0 result = left._concat_same_type([left, right], axis=0) expected = data._concat_same_type([data] * 4).reshape(-1, 2) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) # axis=1 result = left._concat_same_type([left, right], axis=1) assert result.shape == (len(data), 4) - self.assert_extension_array_equal(result[:, :2], left) - self.assert_extension_array_equal(result[:, 2:], right) + tm.assert_extension_array_equal(result[:, :2], left) + tm.assert_extension_array_equal(result[:, 2:], right) # axis > 1 -> invalid msg = "axis 2 is out of bounds for array of dimension 2" @@ -163,7 +164,7 @@ def test_fillna_2d_method(self, data_missing, method): result = arr.pad_or_backfill(method=method, limit=None) expected = data_missing.pad_or_backfill(method=method).repeat(2).reshape(2, 2) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) # Reverse so that backfill is not a no-op. arr2 = arr[::-1] @@ -175,7 +176,7 @@ def test_fillna_2d_method(self, data_missing, method): expected2 = ( data_missing[::-1].pad_or_backfill(method=method).repeat(2).reshape(2, 2) ) - self.assert_extension_array_equal(result2, expected2) + tm.assert_extension_array_equal(result2, expected2) @pytest.mark.parametrize("method", ["mean", "median", "var", "std", "sum", "prod"]) def test_reductions_2d_axis_none(self, data, method): @@ -251,18 +252,18 @@ def get_reduction_result_dtype(dtype): fill_value = 1 if method == "prod" else 0 expected = expected.fillna(fill_value) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) elif method == "median": # std and var are not dtype-preserving expected = data - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) elif method in ["mean", "std", "var"]: if is_integer_dtype(data) or is_bool_dtype(data): data = data.astype("Float64") if method == "mean": - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) else: - self.assert_extension_array_equal(result, data - data) + tm.assert_extension_array_equal(result, data - data) @pytest.mark.parametrize("method", ["mean", "median", "var", "std", "sum", "prod"]) def test_reductions_2d_axis1(self, data, method): diff --git a/pandas/tests/extension/base/dtype.py b/pandas/tests/extension/base/dtype.py index f940191174849f..8948aad1ce157c 100644 --- a/pandas/tests/extension/base/dtype.py +++ b/pandas/tests/extension/base/dtype.py @@ -2,6 +2,7 @@ import pytest import pandas as pd +import pandas._testing as tm from pandas.api.types import ( infer_dtype, is_object_dtype, @@ -66,11 +67,11 @@ def test_check_dtype(self, data): expected = pd.Series([True, True, False, False], index=list("ABCD")) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) expected = pd.Series([True, True, False, False], index=list("ABCD")) result = df.dtypes.apply(str) == str(dtype) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_hashable(self, dtype): hash(dtype) # no error diff --git a/pandas/tests/extension/base/getitem.py b/pandas/tests/extension/base/getitem.py index 2736d134950bca..73c8afee4083a0 100644 --- a/pandas/tests/extension/base/getitem.py +++ b/pandas/tests/extension/base/getitem.py @@ -13,10 +13,10 @@ def test_iloc_series(self, data): ser = pd.Series(data) result = ser.iloc[:4] expected = pd.Series(data[:4]) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) result = ser.iloc[[0, 1, 2, 3]] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_iloc_frame(self, data): df = pd.DataFrame({"A": data, "B": np.arange(len(data), dtype="int64")}) @@ -24,58 +24,58 @@ def test_iloc_frame(self, data): # slice -> frame result = df.iloc[:4, [0]] - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # sequence -> frame result = df.iloc[[0, 1, 2, 3], [0]] - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) expected = pd.Series(data[:4], name="A") # slice -> series result = df.iloc[:4, 0] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) # sequence -> series result = df.iloc[:4, 0] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) # GH#32959 slice columns with step result = df.iloc[:, ::2] - self.assert_frame_equal(result, df[["A"]]) + tm.assert_frame_equal(result, df[["A"]]) result = df[["B", "A"]].iloc[:, ::2] - self.assert_frame_equal(result, df[["B"]]) + tm.assert_frame_equal(result, df[["B"]]) def test_iloc_frame_single_block(self, data): # GH#32959 null slice along index, slice along columns with single-block df = pd.DataFrame({"A": data}) result = df.iloc[:, :] - self.assert_frame_equal(result, df) + tm.assert_frame_equal(result, df) result = df.iloc[:, :1] - self.assert_frame_equal(result, df) + tm.assert_frame_equal(result, df) result = df.iloc[:, :2] - self.assert_frame_equal(result, df) + tm.assert_frame_equal(result, df) result = df.iloc[:, ::2] - self.assert_frame_equal(result, df) + tm.assert_frame_equal(result, df) result = df.iloc[:, 1:2] - self.assert_frame_equal(result, df.iloc[:, :0]) + tm.assert_frame_equal(result, df.iloc[:, :0]) result = df.iloc[:, -1:] - self.assert_frame_equal(result, df) + tm.assert_frame_equal(result, df) def test_loc_series(self, data): ser = pd.Series(data) result = ser.loc[:3] expected = pd.Series(data[:4]) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) result = ser.loc[[0, 1, 2, 3]] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_loc_frame(self, data): df = pd.DataFrame({"A": data, "B": np.arange(len(data), dtype="int64")}) @@ -83,21 +83,21 @@ def test_loc_frame(self, data): # slice -> frame result = df.loc[:3, ["A"]] - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # sequence -> frame result = df.loc[[0, 1, 2, 3], ["A"]] - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) expected = pd.Series(data[:4], name="A") # slice -> series result = df.loc[:3, "A"] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) # sequence -> series result = df.loc[:3, "A"] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_loc_iloc_frame_single_dtype(self, data): # GH#27110 bug in ExtensionBlock.iget caused df.iloc[n] to incorrectly @@ -106,13 +106,13 @@ def test_loc_iloc_frame_single_dtype(self, data): expected = pd.Series([data[2]], index=["A"], name=2, dtype=data.dtype) result = df.loc[2] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) expected = pd.Series( [data[-1]], index=["A"], name=len(data) - 1, dtype=data.dtype ) result = df.iloc[-1] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_getitem_scalar(self, data): result = data[0] @@ -160,7 +160,7 @@ def test_getitem_empty(self, data): assert isinstance(result, type(data)) expected = data[np.array([], dtype="int64")] - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_getitem_mask(self, data): # Empty mask, raw array @@ -209,11 +209,11 @@ def test_getitem_boolean_array_mask(self, data): mask[:5] = True expected = data.take([0, 1, 2, 3, 4]) result = data[mask] - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) expected = pd.Series(expected) result = pd.Series(data)[mask] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_getitem_boolean_na_treated_as_false(self, data): # https://github.com/pandas-dev/pandas/issues/31503 @@ -224,14 +224,14 @@ def test_getitem_boolean_na_treated_as_false(self, data): result = data[mask] expected = data[mask.fillna(False)] - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) s = pd.Series(data) result = s[mask] expected = s[mask.fillna(False)] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize( "idx", @@ -243,11 +243,11 @@ def test_getitem_integer_array(self, data, idx): assert len(result) == 3 assert isinstance(result, type(data)) expected = data.take([0, 1, 2]) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) expected = pd.Series(expected) result = pd.Series(data)[idx] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize( "idx", @@ -287,22 +287,22 @@ def test_getitem_slice(self, data): def test_getitem_ellipsis_and_slice(self, data): # GH#40353 this is called from slice_block_rows result = data[..., :] - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) result = data[:, ...] - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) result = data[..., :3] - self.assert_extension_array_equal(result, data[:3]) + tm.assert_extension_array_equal(result, data[:3]) result = data[:3, ...] - self.assert_extension_array_equal(result, data[:3]) + tm.assert_extension_array_equal(result, data[:3]) result = data[..., ::2] - self.assert_extension_array_equal(result, data[::2]) + tm.assert_extension_array_equal(result, data[::2]) result = data[::2, ...] - self.assert_extension_array_equal(result, data[::2]) + tm.assert_extension_array_equal(result, data[::2]) def test_get(self, data): # GH 20882 @@ -311,11 +311,11 @@ def test_get(self, data): result = s.get([4, 6]) expected = s.iloc[[2, 3]] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) result = s.get(slice(2)) expected = s.iloc[[0, 1]] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) assert s.get(-1) is None assert s.get(s.index.max() + 1) is None @@ -325,7 +325,7 @@ def test_get(self, data): result = s.get(slice("b", "d")) expected = s.iloc[[1, 2, 3]] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) result = s.get("Z") assert result is None @@ -381,7 +381,7 @@ def test_take_negative(self, data): n = len(data) result = data.take([0, -n, n - 1, -1]) expected = data.take([0, 0, n - 1, n - 1]) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_take_non_na_fill_value(self, data_missing): fill_value = data_missing[1] # valid @@ -392,7 +392,7 @@ def test_take_non_na_fill_value(self, data_missing): ) result = arr.take([-1, 1], fill_value=fill_value, allow_fill=True) expected = arr.take([1, 1]) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_take_pandas_style_negative_raises(self, data, na_value): with pytest.raises(ValueError, match=""): @@ -412,13 +412,13 @@ def test_take_series(self, data): data._from_sequence([data[0], data[len(data) - 1]], dtype=s.dtype), index=[0, len(data) - 1], ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_reindex(self, data, na_value): s = pd.Series(data) result = s.reindex([0, 1, 3]) expected = pd.Series(data.take([0, 1, 3]), index=[0, 1, 3]) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) n = len(data) result = s.reindex([-1, 0, n]) @@ -426,13 +426,13 @@ def test_reindex(self, data, na_value): data._from_sequence([na_value, data[0], na_value], dtype=s.dtype), index=[-1, 0, n], ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) result = s.reindex([n, n + 1]) expected = pd.Series( data._from_sequence([na_value, na_value], dtype=s.dtype), index=[n, n + 1] ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_reindex_non_na_fill_value(self, data_missing): valid = data_missing[1] @@ -445,7 +445,7 @@ def test_reindex_non_na_fill_value(self, data_missing): data_missing._from_sequence([na, valid, valid], dtype=data_missing.dtype) ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_loc_len1(self, data): # see GH-27785 take_nd with indexer of len 1 resulting in wrong ndim @@ -487,4 +487,4 @@ def __getitem__(self, item): # String comparison because there's no native way to compare slices. # Before the fix for GH42430, last_item_arg would get set to the 2D slice # (Ellipsis, slice(None, 1, None)) - self.assert_equal(str(df["col1"].array.last_item_arg), "slice(None, 1, None)") + tm.assert_equal(str(df["col1"].array.last_item_arg), "slice(None, 1, None)") diff --git a/pandas/tests/extension/base/groupby.py b/pandas/tests/extension/base/groupby.py index acabcb600ffcc2..882855f59f28db 100644 --- a/pandas/tests/extension/base/groupby.py +++ b/pandas/tests/extension/base/groupby.py @@ -46,10 +46,10 @@ def test_groupby_extension_agg(self, as_index, data_for_grouping): if as_index: index = pd.Index(uniques, name="B") expected = pd.Series(exp_vals, index=index, name="A") - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) else: expected = pd.DataFrame({"B": uniques, "A": exp_vals}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_groupby_agg_extension(self, data_for_grouping): # GH#38980 groupby agg on extension type fails for non-numeric types @@ -59,13 +59,13 @@ def test_groupby_agg_extension(self, data_for_grouping): expected = expected.set_index("A") result = df.groupby("A").agg({"B": "first"}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) result = df.groupby("A").agg("first") - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) result = df.groupby("A").first() - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_groupby_agg_extension_timedelta_cumsum_with_named_aggregation(self): # GH#41720 @@ -89,7 +89,7 @@ def test_groupby_agg_extension_timedelta_cumsum_with_named_aggregation(self): ) gb = df.groupby("grps") result = gb.agg(td=("td", "cumsum")) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_groupby_extension_no_sort(self, data_for_grouping): df = pd.DataFrame({"A": [1, 1, 2, 2, 3, 3, 1, 4], "B": data_for_grouping}) @@ -108,7 +108,7 @@ def test_groupby_extension_no_sort(self, data_for_grouping): if is_bool: exp_vals = exp_vals[:-1] expected = pd.Series(exp_vals, index=index, name="A") - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_groupby_extension_transform(self, data_for_grouping): is_bool = data_for_grouping.dtype._is_boolean @@ -126,7 +126,7 @@ def test_groupby_extension_transform(self, data_for_grouping): if is_bool: expected = expected[:-1] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_groupby_extension_apply(self, data_for_grouping, groupby_apply_op): df = pd.DataFrame({"A": [1, 1, 2, 2, 3, 3, 1, 4], "B": data_for_grouping}) @@ -148,7 +148,7 @@ def test_groupby_apply_identity(self, data_for_grouping): index=pd.Index([1, 2, 3, 4], name="A"), name="B", ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_in_numeric_groupby(self, data_for_grouping): df = pd.DataFrame( diff --git a/pandas/tests/extension/base/io.py b/pandas/tests/extension/base/io.py index a8c25db3181d04..150b3ba5873330 100644 --- a/pandas/tests/extension/base/io.py +++ b/pandas/tests/extension/base/io.py @@ -4,6 +4,7 @@ import pytest import pandas as pd +import pandas._testing as tm from pandas.tests.extension.base.base import BaseExtensionTests @@ -16,4 +17,4 @@ def test_EA_types(self, engine, data): StringIO(csv_output), dtype={"with_dtype": str(data.dtype)}, engine=engine ) expected = df - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/extension/base/methods.py b/pandas/tests/extension/base/methods.py index 25bc99e8b92704..ac17d836770694 100644 --- a/pandas/tests/extension/base/methods.py +++ b/pandas/tests/extension/base/methods.py @@ -48,7 +48,7 @@ def test_value_counts(self, all_data, dropna): result = pd.Series(all_data).value_counts(dropna=dropna).sort_index() expected = pd.Series(other).value_counts(dropna=dropna).sort_index() - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_value_counts_with_normalize(self, data): # GH 33172 @@ -69,13 +69,13 @@ def test_value_counts_with_normalize(self, data): # TODO(GH#44692): avoid special-casing expected = expected.astype("Float64") - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_count(self, data_missing): df = pd.DataFrame({"A": data_missing}) result = df.count(axis="columns") expected = pd.Series([0, 1]) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_series_count(self, data_missing): # GH#26835 @@ -98,7 +98,7 @@ def test_argsort(self, data_for_sorting): result = pd.Series(data_for_sorting).argsort() # argsort result gets passed to take, so should be np.intp expected = pd.Series(np.array([2, 0, 1], dtype=np.intp)) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_argsort_missing_array(self, data_missing_for_sorting): result = data_missing_for_sorting.argsort() @@ -111,7 +111,7 @@ def test_argsort_missing(self, data_missing_for_sorting): with tm.assert_produces_warning(FutureWarning, match=msg): result = pd.Series(data_missing_for_sorting).argsort() expected = pd.Series(np.array([1, -1, 0], dtype=np.intp)) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_argmin_argmax(self, data_for_sorting, data_missing_for_sorting, na_value): # GH 24382 @@ -216,7 +216,7 @@ def test_sort_values(self, data_for_sorting, ascending, sort_by_key): else: expected = ser.iloc[[1, 0, 2]] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize("ascending", [True, False]) def test_sort_values_missing( @@ -228,7 +228,7 @@ def test_sort_values_missing( expected = ser.iloc[[2, 0, 1]] else: expected = ser.iloc[[0, 2, 1]] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize("ascending", [True, False]) def test_sort_values_frame(self, data_for_sorting, ascending): @@ -237,7 +237,7 @@ def test_sort_values_frame(self, data_for_sorting, ascending): expected = pd.DataFrame( {"A": [1, 1, 2], "B": data_for_sorting.take([2, 0, 1])}, index=[2, 0, 1] ) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("box", [pd.Series, lambda x: x]) @pytest.mark.parametrize("method", [lambda x: x.unique(), pd.unique]) @@ -263,14 +263,14 @@ def test_factorize(self, data_for_grouping): expected_uniques = data_for_grouping.take([0, 4, 7]) tm.assert_numpy_array_equal(codes, expected_codes) - self.assert_extension_array_equal(uniques, expected_uniques) + tm.assert_extension_array_equal(uniques, expected_uniques) def test_factorize_equivalence(self, data_for_grouping): codes_1, uniques_1 = pd.factorize(data_for_grouping, use_na_sentinel=True) codes_2, uniques_2 = data_for_grouping.factorize(use_na_sentinel=True) tm.assert_numpy_array_equal(codes_1, codes_2) - self.assert_extension_array_equal(uniques_1, uniques_2) + tm.assert_extension_array_equal(uniques_1, uniques_2) assert len(uniques_1) == len(pd.unique(uniques_1)) assert uniques_1.dtype == data_for_grouping.dtype @@ -280,7 +280,7 @@ def test_factorize_empty(self, data): expected_uniques = type(data)._from_sequence([], dtype=data[:0].dtype) tm.assert_numpy_array_equal(codes, expected_codes) - self.assert_extension_array_equal(uniques, expected_uniques) + tm.assert_extension_array_equal(uniques, expected_uniques) def test_fillna_copy_frame(self, data_missing): arr = data_missing.take([1, 1]) @@ -292,7 +292,7 @@ def test_fillna_copy_frame(self, data_missing): result.iloc[0, 0] = filled_val - self.assert_frame_equal(df, df_orig) + tm.assert_frame_equal(df, df_orig) def test_fillna_copy_series(self, data_missing): arr = data_missing.take([1, 1]) @@ -303,7 +303,7 @@ def test_fillna_copy_series(self, data_missing): result = ser.fillna(filled_val) result.iloc[0] = filled_val - self.assert_series_equal(ser, ser_orig) + tm.assert_series_equal(ser, ser_orig) def test_fillna_length_mismatch(self, data_missing): msg = "Length of 'value' does not match." @@ -324,7 +324,7 @@ def test_combine_le(self, data_repeated): [a <= b for (a, b) in zip(list(orig_data1), list(orig_data2))], dtype=self._combine_le_expected_dtype, ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) val = s1.iloc[0] result = s1.combine(val, lambda x1, x2: x1 <= x2) @@ -332,7 +332,7 @@ def test_combine_le(self, data_repeated): [a <= val for a in list(orig_data1)], dtype=self._combine_le_expected_dtype, ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_combine_add(self, data_repeated): # GH 20825 @@ -346,14 +346,14 @@ def test_combine_add(self, data_repeated): [a + b for (a, b) in zip(list(orig_data1), list(orig_data2))] ) ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) val = s1.iloc[0] result = s1.combine(val, lambda x1, x2: x1 + x2) expected = pd.Series( orig_data1._from_sequence([a + val for a in list(orig_data1)]) ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_combine_first(self, data): # https://github.com/pandas-dev/pandas/issues/24147 @@ -361,7 +361,7 @@ def test_combine_first(self, data): b = pd.Series(data[2:5], index=[2, 3, 4]) result = a.combine_first(b) expected = pd.Series(data[:5]) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize("frame", [True, False]) @pytest.mark.parametrize( @@ -379,10 +379,10 @@ def test_container_shift(self, data, frame, periods, indices): expected = pd.concat( [expected, pd.Series([1] * 5, name="B").shift(periods)], axis=1 ) - compare = self.assert_frame_equal + compare = tm.assert_frame_equal else: result = data.shift(periods) - compare = self.assert_series_equal + compare = tm.assert_series_equal compare(result, expected) @@ -408,7 +408,7 @@ def test_diff(self, data, periods): s = pd.Series(data) result = s.diff(periods) expected = pd.Series(op(data, data.shift(periods))) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) df = pd.DataFrame({"A": data, "B": [1.0] * 5}) result = df.diff(periods) @@ -417,7 +417,7 @@ def test_diff(self, data, periods): else: b = [0, 0, 0, np.nan, np.nan] expected = pd.DataFrame({"A": expected, "B": b}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "periods, indices", @@ -428,7 +428,7 @@ def test_shift_non_empty_array(self, data, periods, indices): subset = data[:2] result = subset.shift(periods) expected = subset.take(indices, allow_fill=True) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) @pytest.mark.parametrize("periods", [-4, -1, 0, 1, 4]) def test_shift_empty_array(self, data, periods): @@ -436,7 +436,7 @@ def test_shift_empty_array(self, data, periods): empty = data[:0] result = empty.shift(periods) expected = empty - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_shift_zero_copies(self, data): # GH#31502 @@ -451,11 +451,11 @@ def test_shift_fill_value(self, data): fill_value = data[0] result = arr.shift(1, fill_value=fill_value) expected = data.take([0, 0, 1, 2]) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) result = arr.shift(-2, fill_value=fill_value) expected = data.take([2, 3, 0, 0]) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_not_hashable(self, data): # We are in general mutable, so not hashable @@ -469,7 +469,7 @@ def test_hash_pandas_object_works(self, data, as_frame): data = data.to_frame() a = pd.util.hash_pandas_object(data) b = pd.util.hash_pandas_object(data) - self.assert_equal(a, b) + tm.assert_equal(a, b) def test_searchsorted(self, data_for_sorting, as_series): if data_for_sorting.dtype._is_boolean: @@ -544,10 +544,10 @@ def test_where_series(self, data, na_value, as_frame): if as_frame: expected = expected.to_frame(name="a") - self.assert_equal(result, expected) + tm.assert_equal(result, expected) ser.mask(~cond, inplace=True) - self.assert_equal(ser, expected) + tm.assert_equal(ser, expected) # array other ser = orig.copy() @@ -562,10 +562,10 @@ def test_where_series(self, data, na_value, as_frame): expected = pd.Series(cls._from_sequence([a, b, b, b], dtype=data.dtype)) if as_frame: expected = expected.to_frame(name="a") - self.assert_equal(result, expected) + tm.assert_equal(result, expected) ser.mask(~cond, other, inplace=True) - self.assert_equal(ser, expected) + tm.assert_equal(ser, expected) @pytest.mark.parametrize("repeats", [0, 1, 2, [1, 2, 3]]) def test_repeat(self, data, repeats, as_series, use_numpy): @@ -581,7 +581,7 @@ def test_repeat(self, data, repeats, as_series, use_numpy): if as_series: expected = pd.Series(expected, index=arr.index.repeat(repeats)) - self.assert_equal(result, expected) + tm.assert_equal(result, expected) @pytest.mark.parametrize( "repeats, kwargs, error, msg", @@ -602,19 +602,19 @@ def test_repeat_raises(self, data, repeats, kwargs, error, msg, use_numpy): def test_delete(self, data): result = data.delete(0) expected = data[1:] - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) result = data.delete([1, 3]) expected = data._concat_same_type([data[[0]], data[[2]], data[4:]]) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_insert(self, data): # insert at the beginning result = data[1:].insert(0, data[0]) - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) result = data[1:].insert(-len(data[1:]), data[0]) - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) # insert at the middle result = data[:-1].insert(4, data[-1]) @@ -623,7 +623,7 @@ def test_insert(self, data): taker[5:] = taker[4:-1] taker[4] = len(data) - 1 expected = data.take(taker) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_insert_invalid(self, data, invalid_scalar): item = invalid_scalar diff --git a/pandas/tests/extension/base/missing.py b/pandas/tests/extension/base/missing.py index a839a9d327f95d..e076e9d946127f 100644 --- a/pandas/tests/extension/base/missing.py +++ b/pandas/tests/extension/base/missing.py @@ -15,12 +15,12 @@ def test_isna(self, data_missing): result = pd.Series(data_missing).isna() expected = pd.Series(expected) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) # GH 21189 result = pd.Series(data_missing).drop([0, 1]).isna() expected = pd.Series([], dtype=bool) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize("na_func", ["isna", "notna"]) def test_isna_returns_copy(self, data_missing, na_func): @@ -31,18 +31,18 @@ def test_isna_returns_copy(self, data_missing, na_func): mask = np.array(mask) mask[:] = True - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_dropna_array(self, data_missing): result = data_missing.dropna() expected = data_missing[[1]] - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_dropna_series(self, data_missing): ser = pd.Series(data_missing) result = ser.dropna() expected = ser.iloc[[1]] - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_dropna_frame(self, data_missing): df = pd.DataFrame({"A": data_missing}) @@ -50,24 +50,24 @@ def test_dropna_frame(self, data_missing): # defaults result = df.dropna() expected = df.iloc[[1]] - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # axis = 1 result = df.dropna(axis="columns") expected = pd.DataFrame(index=pd.RangeIndex(2), columns=pd.Index([])) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # multiple df = pd.DataFrame({"A": data_missing, "B": [1, np.nan]}) result = df.dropna() expected = df.iloc[:0] - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_fillna_scalar(self, data_missing): valid = data_missing[1] result = data_missing.fillna(valid) expected = data_missing.fillna(valid) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) @pytest.mark.filterwarnings( "ignore:Series.fillna with 'method' is deprecated:FutureWarning" @@ -76,7 +76,7 @@ def test_fillna_limit_pad(self, data_missing): arr = data_missing.take([1, 0, 0, 0, 1]) result = pd.Series(arr).ffill(limit=2) expected = pd.Series(data_missing.take([1, 1, 1, 0, 1])) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.filterwarnings( "ignore:Series.fillna with 'method' is deprecated:FutureWarning" @@ -85,7 +85,7 @@ def test_fillna_limit_backfill(self, data_missing): arr = data_missing.take([1, 0, 0, 0, 1]) result = pd.Series(arr).fillna(method="backfill", limit=2) expected = pd.Series(data_missing.take([1, 0, 1, 1, 1])) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_fillna_no_op_returns_copy(self, data): data = data[~data.isna()] @@ -93,11 +93,11 @@ def test_fillna_no_op_returns_copy(self, data): valid = data[0] result = data.fillna(valid) assert result is not data - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) result = data.pad_or_backfill(method="backfill") assert result is not data - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) def test_fillna_series(self, data_missing): fill_value = data_missing[1] @@ -109,15 +109,15 @@ def test_fillna_series(self, data_missing): [fill_value, fill_value], dtype=data_missing.dtype ) ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) # Fill with a series result = ser.fillna(expected) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) # Fill with a series not affecting the missing values result = ser.fillna(ser) - self.assert_series_equal(result, ser) + tm.assert_series_equal(result, ser) def test_fillna_series_method(self, data_missing, fillna_method): fill_value = data_missing[1] @@ -132,7 +132,7 @@ def test_fillna_series_method(self, data_missing, fillna_method): ) ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_fillna_frame(self, data_missing): fill_value = data_missing[1] @@ -148,14 +148,14 @@ def test_fillna_frame(self, data_missing): } ) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_fillna_fill_other(self, data): result = pd.DataFrame({"A": data, "B": [np.nan] * len(data)}).fillna({"B": 0.0}) expected = pd.DataFrame({"A": data, "B": [0.0] * len(result)}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_use_inf_as_na_no_effect(self, data_missing): ser = pd.Series(data_missing) @@ -164,4 +164,4 @@ def test_use_inf_as_na_no_effect(self, data_missing): with tm.assert_produces_warning(FutureWarning, match=msg): with pd.option_context("mode.use_inf_as_na", True): result = ser.isna() - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) diff --git a/pandas/tests/extension/base/ops.py b/pandas/tests/extension/base/ops.py index 24ccedda31f74d..49598f014fbf60 100644 --- a/pandas/tests/extension/base/ops.py +++ b/pandas/tests/extension/base/ops.py @@ -34,7 +34,7 @@ def _check_op( result = op(ser, other) expected = self._combine(ser, other, op) assert isinstance(result, type(ser)) - self.assert_equal(result, expected) + tm.assert_equal(result, expected) else: with pytest.raises(exc): op(ser, other) @@ -47,8 +47,8 @@ def _check_divmod_op(self, ser: pd.Series, op, other, exc=Exception): expected_div, expected_mod = ser // other, ser % other else: expected_div, expected_mod = other // ser, other % ser - self.assert_series_equal(result_div, expected_div) - self.assert_series_equal(result_mod, expected_mod) + tm.assert_series_equal(result_div, expected_div) + tm.assert_series_equal(result_mod, expected_mod) else: with pytest.raises(exc): divmod(ser, other) @@ -111,7 +111,7 @@ def test_add_series_with_extension_array(self, data): ser = pd.Series(data) result = ser + data expected = pd.Series(data + data) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize("box", [pd.Series, pd.DataFrame]) def test_direct_arith_with_ndframe_returns_not_implemented( @@ -140,7 +140,7 @@ def _compare_other(self, ser: pd.Series, data, op, other): # comparison should match point-wise comparisons result = op(ser, other) expected = ser.combine(other, op) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) else: exc = None @@ -152,7 +152,7 @@ def _compare_other(self, ser: pd.Series, data, op, other): if exc is None: # Didn't error, then should match pointwise behavior expected = ser.combine(other, op) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) else: with pytest.raises(type(exc)): ser.combine(other, op) @@ -192,7 +192,7 @@ def test_invert(self, data): ser = pd.Series(data, name="name") result = ~ser expected = pd.Series(~data, name="name") - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize("ufunc", [np.positive, np.negative, np.abs]) def test_unary_ufunc_dunder_equivalence(self, data, ufunc): @@ -213,4 +213,4 @@ def test_unary_ufunc_dunder_equivalence(self, data, ufunc): ufunc(data) else: alt = ufunc(data) - self.assert_extension_array_equal(result, alt) + tm.assert_extension_array_equal(result, alt) diff --git a/pandas/tests/extension/base/reshaping.py b/pandas/tests/extension/base/reshaping.py index cc970c690529de..9b150cf5054ee8 100644 --- a/pandas/tests/extension/base/reshaping.py +++ b/pandas/tests/extension/base/reshaping.py @@ -4,6 +4,7 @@ import pytest import pandas as pd +import pandas._testing as tm from pandas.api.extensions import ExtensionArray from pandas.core.internals.blocks import EABackedBlock from pandas.tests.extension.base.base import BaseExtensionTests @@ -41,10 +42,10 @@ def test_concat_all_na_block(self, data_missing, in_frame): result = pd.concat([valid_block, na_block]) if in_frame: expected = pd.DataFrame({"a": data_missing.take([1, 1, 0, 0])}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) else: expected = pd.Series(data_missing.take([1, 1, 0, 0])) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_concat_mixed_dtypes(self, data): # https://github.com/pandas-dev/pandas/issues/20762 @@ -56,21 +57,21 @@ def test_concat_mixed_dtypes(self, data): # dataframes result = pd.concat(dfs) expected = pd.concat([x.astype(object) for x in dfs]) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # series result = pd.concat([x["A"] for x in dfs]) expected = pd.concat([x["A"].astype(object) for x in dfs]) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) # simple test for just EA and one other result = pd.concat([df1, df2.astype(object)]) expected = pd.concat([df1.astype("object"), df2.astype("object")]) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) result = pd.concat([df1["A"], df2["A"].astype(object)]) expected = pd.concat([df1["A"].astype("object"), df2["A"].astype("object")]) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_concat_columns(self, data, na_value): df1 = pd.DataFrame({"A": data[:3]}) @@ -78,9 +79,9 @@ def test_concat_columns(self, data, na_value): expected = pd.DataFrame({"A": data[:3], "B": [1, 2, 3]}) result = pd.concat([df1, df2], axis=1) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) result = pd.concat([df1["A"], df2["B"]], axis=1) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # non-aligned df2 = pd.DataFrame({"B": [1, 2, 3]}, index=[1, 2, 3]) @@ -92,9 +93,9 @@ def test_concat_columns(self, data, na_value): ) result = pd.concat([df1, df2], axis=1) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) result = pd.concat([df1["A"], df2["B"]], axis=1) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_concat_extension_arrays_copy_false(self, data, na_value): # GH 20756 @@ -107,7 +108,7 @@ def test_concat_extension_arrays_copy_false(self, data, na_value): } ) result = pd.concat([df1, df2], axis=1, copy=False) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_concat_with_reindex(self, data): # GH-33027 @@ -120,7 +121,7 @@ def test_concat_with_reindex(self, data): "b": data.take(([-1] * 5) + list(range(5)), allow_fill=True), } ) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_align(self, data, na_value): a = data[:3] @@ -130,8 +131,8 @@ def test_align(self, data, na_value): # Assumes that the ctor can take a list of scalars of the type e1 = pd.Series(data._from_sequence(list(a) + [na_value], dtype=data.dtype)) e2 = pd.Series(data._from_sequence([na_value] + list(b), dtype=data.dtype)) - self.assert_series_equal(r1, e1) - self.assert_series_equal(r2, e2) + tm.assert_series_equal(r1, e1) + tm.assert_series_equal(r2, e2) def test_align_frame(self, data, na_value): a = data[:3] @@ -145,8 +146,8 @@ def test_align_frame(self, data, na_value): e2 = pd.DataFrame( {"A": data._from_sequence([na_value] + list(b), dtype=data.dtype)} ) - self.assert_frame_equal(r1, e1) - self.assert_frame_equal(r2, e2) + tm.assert_frame_equal(r1, e1) + tm.assert_frame_equal(r2, e2) def test_align_series_frame(self, data, na_value): # https://github.com/pandas-dev/pandas/issues/20576 @@ -159,20 +160,20 @@ def test_align_series_frame(self, data, na_value): name=ser.name, ) - self.assert_series_equal(r1, e1) - self.assert_frame_equal(r2, df) + tm.assert_series_equal(r1, e1) + tm.assert_frame_equal(r2, df) def test_set_frame_expand_regular_with_extension(self, data): df = pd.DataFrame({"A": [1] * len(data)}) df["B"] = data expected = pd.DataFrame({"A": [1] * len(data), "B": data}) - self.assert_frame_equal(df, expected) + tm.assert_frame_equal(df, expected) def test_set_frame_expand_extension_with_regular(self, data): df = pd.DataFrame({"A": data}) df["B"] = [1] * len(data) expected = pd.DataFrame({"A": data, "B": [1] * len(data)}) - self.assert_frame_equal(df, expected) + tm.assert_frame_equal(df, expected) def test_set_frame_overwrite_object(self, data): # https://github.com/pandas-dev/pandas/issues/20555 @@ -196,7 +197,7 @@ def test_merge(self, data, na_value): ), } ) - self.assert_frame_equal(res, exp[["ext", "int1", "key", "int2"]]) + tm.assert_frame_equal(res, exp[["ext", "int1", "key", "int2"]]) res = pd.merge(df1, df2, how="outer") exp = pd.DataFrame( @@ -209,7 +210,7 @@ def test_merge(self, data, na_value): ), } ) - self.assert_frame_equal(res, exp[["ext", "int1", "key", "int2"]]) + tm.assert_frame_equal(res, exp[["ext", "int1", "key", "int2"]]) def test_merge_on_extension_array(self, data): # GH 23020 @@ -219,12 +220,12 @@ def test_merge_on_extension_array(self, data): df = pd.DataFrame({"key": key, "val": [1, 2]}) result = pd.merge(df, df, on="key") expected = pd.DataFrame({"key": key, "val_x": [1, 2], "val_y": [1, 2]}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # order result = pd.merge(df.iloc[[1, 0]], df, on="key") expected = expected.iloc[[1, 0]].reset_index(drop=True) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_merge_on_extension_array_duplicates(self, data): # GH 23020 @@ -241,7 +242,7 @@ def test_merge_on_extension_array_duplicates(self, data): "val_y": [1, 3, 1, 3, 2], } ) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "columns", @@ -267,7 +268,7 @@ def test_stack(self, data, columns): assert all(result.dtypes == df.iloc[:, 0].dtype) result = result.astype(object) - self.assert_equal(result, expected) + tm.assert_equal(result, expected) @pytest.mark.parametrize( "index", @@ -316,7 +317,7 @@ def test_unstack(self, data, index, obj): df = ser.to_frame() alt = df.unstack(level=level).droplevel(0, axis=1) - self.assert_frame_equal(result, alt) + tm.assert_frame_equal(result, alt) obj_ser = ser.astype(object) @@ -325,7 +326,7 @@ def test_unstack(self, data, index, obj): assert (expected.dtypes == object).all() result = result.astype(object) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_ravel(self, data): # as long as EA is 1D-only, ravel is a no-op @@ -362,6 +363,6 @@ def test_transpose_frame(self, data): }, index=["A", "B"], ) - self.assert_frame_equal(result, expected) - self.assert_frame_equal(np.transpose(np.transpose(df)), df) - self.assert_frame_equal(np.transpose(np.transpose(df[["A"]])), df[["A"]]) + tm.assert_frame_equal(result, expected) + tm.assert_frame_equal(np.transpose(np.transpose(df)), df) + tm.assert_frame_equal(np.transpose(np.transpose(df[["A"]])), df[["A"]]) diff --git a/pandas/tests/extension/base/setitem.py b/pandas/tests/extension/base/setitem.py index 73445a96f4a030..76aa560fd17a21 100644 --- a/pandas/tests/extension/base/setitem.py +++ b/pandas/tests/extension/base/setitem.py @@ -62,18 +62,18 @@ def test_setitem_sequence_mismatched_length_raises(self, data, as_array): with pytest.raises(ValueError, match=xpr.format("list-like")): ser[[0, 1]] = value # Ensure no modifications made before the exception - self.assert_series_equal(ser, original) + tm.assert_series_equal(ser, original) with pytest.raises(ValueError, match=xpr.format("slice")): ser[slice(3)] = value - self.assert_series_equal(ser, original) + tm.assert_series_equal(ser, original) def test_setitem_empty_indexer(self, data, box_in_series): if box_in_series: data = pd.Series(data) original = data.copy() data[np.array([], dtype=int)] = [] - self.assert_equal(data, original) + tm.assert_equal(data, original) def test_setitem_sequence_broadcasts(self, data, box_in_series): if box_in_series: @@ -135,7 +135,7 @@ def test_setitem_mask(self, data, mask, box_in_series): arr = pd.Series(arr) expected = pd.Series(expected) arr[mask] = data[0] - self.assert_equal(expected, arr) + tm.assert_equal(expected, arr) def test_setitem_mask_raises(self, data, box_in_series): # wrong length @@ -177,7 +177,7 @@ def test_setitem_integer_array(self, data, idx, box_in_series): expected = pd.Series(expected) arr[idx] = arr[0] - self.assert_equal(arr, expected) + tm.assert_equal(arr, expected) @pytest.mark.parametrize( "idx, box_in_series", @@ -248,27 +248,27 @@ def test_setitem_expand_columns(self, data): result = df.copy() result["B"] = 1 expected = pd.DataFrame({"A": data, "B": [1] * len(data)}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) result = df.copy() result.loc[:, "B"] = 1 - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) # overwrite with new type result["B"] = data expected = pd.DataFrame({"A": data, "B": data}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_setitem_expand_with_extension(self, data): df = pd.DataFrame({"A": [1] * len(data)}) result = df.copy() result["B"] = data expected = pd.DataFrame({"A": [1] * len(data), "B": data}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) result = df.copy() result.loc[:, "B"] = data - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_setitem_frame_invalid_length(self, data): df = pd.DataFrame({"A": [1] * len(data)}) @@ -283,7 +283,7 @@ def test_setitem_tuple_index(self, data): ser = pd.Series(data[:2], index=[(0, 0), (0, 1)]) expected = pd.Series(data.take([1, 1]), index=ser.index) ser[(0, 0)] = data[1] - self.assert_series_equal(ser, expected) + tm.assert_series_equal(ser, expected) def test_setitem_slice(self, data, box_in_series): arr = data[:5].copy() @@ -293,7 +293,7 @@ def test_setitem_slice(self, data, box_in_series): expected = pd.Series(expected) arr[:3] = data[0] - self.assert_equal(arr, expected) + tm.assert_equal(arr, expected) def test_setitem_loc_iloc_slice(self, data): arr = data[:5].copy() @@ -302,11 +302,11 @@ def test_setitem_loc_iloc_slice(self, data): result = s.copy() result.iloc[:3] = data[0] - self.assert_equal(result, expected) + tm.assert_equal(result, expected) result = s.copy() result.loc[:"c"] = data[0] - self.assert_equal(result, expected) + tm.assert_equal(result, expected) def test_setitem_slice_mismatch_length_raises(self, data): arr = data[:5] @@ -316,7 +316,7 @@ def test_setitem_slice_mismatch_length_raises(self, data): def test_setitem_slice_array(self, data): arr = data[:5].copy() arr[:5] = data[-5:] - self.assert_extension_array_equal(arr, data[-5:]) + tm.assert_extension_array_equal(arr, data[-5:]) def test_setitem_scalar_key_sequence_raise(self, data): arr = data[:5].copy() @@ -340,21 +340,21 @@ def test_setitem_with_expansion_dataframe_column(self, data, full_indexer): key = full_indexer(df) result.loc[key, "data"] = df["data"] - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) def test_setitem_with_expansion_row(self, data, na_value): df = pd.DataFrame({"data": data[:1]}) df.loc[1, "data"] = data[1] expected = pd.DataFrame({"data": data[:2]}) - self.assert_frame_equal(df, expected) + tm.assert_frame_equal(df, expected) # https://github.com/pandas-dev/pandas/issues/47284 df.loc[2, "data"] = na_value expected = pd.DataFrame( {"data": pd.Series([data[0], data[1], na_value], dtype=data.dtype)} ) - self.assert_frame_equal(df, expected) + tm.assert_frame_equal(df, expected) def test_setitem_series(self, data, full_indexer): # https://github.com/pandas-dev/pandas/issues/32395 @@ -369,7 +369,7 @@ def test_setitem_series(self, data, full_indexer): expected = pd.Series( data.astype(object), index=ser.index, name="data", dtype=object ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_setitem_frame_2d_values(self, data): # GH#44514 @@ -385,20 +385,20 @@ def test_setitem_frame_2d_values(self, data): orig = df.copy() df.iloc[:] = df - self.assert_frame_equal(df, orig) + tm.assert_frame_equal(df, orig) df.iloc[:-1] = df.iloc[:-1] - self.assert_frame_equal(df, orig) + tm.assert_frame_equal(df, orig) df.iloc[:] = df.values - self.assert_frame_equal(df, orig) + tm.assert_frame_equal(df, orig) if not using_array_manager and not using_copy_on_write: # GH#33457 Check that this setting occurred in-place # FIXME(ArrayManager): this should work there too assert df._mgr.arrays[0] is blk_data df.iloc[:-1] = df.values[:-1] - self.assert_frame_equal(df, orig) + tm.assert_frame_equal(df, orig) def test_delitem_series(self, data): # GH#40763 @@ -409,7 +409,7 @@ def test_delitem_series(self, data): expected = ser[taker] del ser[1] - self.assert_series_equal(ser, expected) + tm.assert_series_equal(ser, expected) def test_setitem_invalid(self, data, invalid_scalar): msg = "" # messages vary by subclass, so we do not test it diff --git a/pandas/tests/extension/decimal/test_decimal.py b/pandas/tests/extension/decimal/test_decimal.py index 16ce2c9312f7c5..e650e2d20e025c 100644 --- a/pandas/tests/extension/decimal/test_decimal.py +++ b/pandas/tests/extension/decimal/test_decimal.py @@ -89,7 +89,7 @@ def test_take_na_value_other_decimal(self): arr = DecimalArray([decimal.Decimal("1.0"), decimal.Decimal("2.0")]) result = arr.take([0, -1], allow_fill=True, fill_value=decimal.Decimal("-1.0")) expected = DecimalArray([decimal.Decimal("1.0"), decimal.Decimal("-1.0")]) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) class TestIndex(base.BaseIndexTests): diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index 311727b124df16..cec45c984b3590 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -348,7 +348,7 @@ def check_accumulate(self, ser, op_name, skipna): result = result.astype("Float64") expected = getattr(ser.astype("Float64"), op_name)(skipna=skipna) - self.assert_series_equal(result, expected, check_dtype=False) + tm.assert_series_equal(result, expected, check_dtype=False) @pytest.mark.parametrize("skipna", [True, False]) def test_accumulate_series_raises(self, data, all_numeric_accumulations, skipna): @@ -695,11 +695,11 @@ def test_fillna_no_op_returns_copy(self, data): valid = data[0] result = data.fillna(valid) assert result is not data - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) result = data.fillna(method="backfill") assert result is not data - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) class TestBasePrinting(base.BasePrintingTests): @@ -758,7 +758,7 @@ def test_EA_types(self, engine, data, dtype_backend, request): dtype_backend=dtype_backend, ) expected = df - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) class TestBaseUnaryOps(base.BaseUnaryOpsTests): @@ -807,7 +807,7 @@ def test_value_counts_with_normalize(self, data, request): ) expected = expected.astype("double[pyarrow]") - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_argmin_argmax( self, data_for_sorting, data_missing_for_sorting, na_value, request @@ -1229,7 +1229,7 @@ def test_compare_array(self, data, comparison_op, na_value): expected = ser.combine(other, comparison_op) expected[8] = na_value expected[97] = na_value - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) else: exc = None @@ -1241,7 +1241,7 @@ def test_compare_array(self, data, comparison_op, na_value): if exc is None: # Didn't error, then should match point-wise behavior expected = ser.combine(other, comparison_op) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) else: with pytest.raises(type(exc)): ser.combine(other, comparison_op) diff --git a/pandas/tests/extension/test_boolean.py b/pandas/tests/extension/test_boolean.py index c85e0833be0207..24095f807d4ae1 100644 --- a/pandas/tests/extension/test_boolean.py +++ b/pandas/tests/extension/test_boolean.py @@ -156,7 +156,7 @@ def _check_op(self, obj, op, other, op_name, exc=NotImplementedError): if op_name == "__rpow__": # for rpow, combine does not propagate NaN expected[result.isna()] = np.nan - self.assert_equal(result, expected) + tm.assert_equal(result, expected) else: with pytest.raises(exc): op(obj, other) diff --git a/pandas/tests/extension/test_categorical.py b/pandas/tests/extension/test_categorical.py index 91ca358ca0709f..f80869a1927814 100644 --- a/pandas/tests/extension/test_categorical.py +++ b/pandas/tests/extension/test_categorical.py @@ -177,17 +177,17 @@ def test_combine_add(self, data_repeated): expected = pd.Series( [a + b for (a, b) in zip(list(orig_data1), list(orig_data2))] ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) val = s1.iloc[0] result = s1.combine(val, lambda x1, x2: x1 + x2) expected = pd.Series([a + val for a in list(orig_data1)]) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.parametrize("na_action", [None, "ignore"]) def test_map(self, data, na_action): result = data.map(lambda x: x, na_action=na_action) - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) class TestCasting(base.BaseCastingTests): diff --git a/pandas/tests/extension/test_datetime.py b/pandas/tests/extension/test_datetime.py index a93271a454459b..d8adc4c8c91a5e 100644 --- a/pandas/tests/extension/test_datetime.py +++ b/pandas/tests/extension/test_datetime.py @@ -19,6 +19,7 @@ from pandas.core.dtypes.dtypes import DatetimeTZDtype import pandas as pd +import pandas._testing as tm from pandas.core.arrays import DatetimeArray from pandas.tests.extension import base @@ -119,7 +120,7 @@ def test_combine_add(self, data_repeated): @pytest.mark.parametrize("na_action", [None, "ignore"]) def test_map(self, data, na_action): result = data.map(lambda x: x, na_action=na_action) - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) class TestInterface(BaseDatetimeTests, base.BaseInterfaceTests): diff --git a/pandas/tests/extension/test_masked_numeric.py b/pandas/tests/extension/test_masked_numeric.py index 6462ad1b31c932..321321e0760d5c 100644 --- a/pandas/tests/extension/test_masked_numeric.py +++ b/pandas/tests/extension/test_masked_numeric.py @@ -181,7 +181,7 @@ def _check_op(self, s, op, other, op_name, exc=NotImplementedError): # combine method result in 'biggest' (float64) dtype expected = expected.astype(sdtype) - self.assert_equal(result, expected) + tm.assert_equal(result, expected) else: with pytest.raises(exc): op(s, other) @@ -202,7 +202,7 @@ def _check_op( result = op(ser, other) # Override to do the astype to boolean expected = ser.combine(other, op).astype("boolean") - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) else: with pytest.raises(exc): op(ser, other) diff --git a/pandas/tests/extension/test_numpy.py b/pandas/tests/extension/test_numpy.py index 993a08d7bd3696..f4ff423ad485ba 100644 --- a/pandas/tests/extension/test_numpy.py +++ b/pandas/tests/extension/test_numpy.py @@ -424,7 +424,7 @@ def test_setitem_with_expansion_dataframe_column(self, data, full_indexer): if data.dtype.numpy_dtype != object: if not isinstance(key, slice) or key != slice(None): expected = pd.DataFrame({"data": data.to_numpy()}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) @skip_nested diff --git a/pandas/tests/extension/test_period.py b/pandas/tests/extension/test_period.py index 6ddd1dff92f01b..dc8e822fc69120 100644 --- a/pandas/tests/extension/test_period.py +++ b/pandas/tests/extension/test_period.py @@ -108,7 +108,7 @@ def test_diff(self, data, periods): @pytest.mark.parametrize("na_action", [None, "ignore"]) def test_map(self, data, na_action): result = data.map(lambda x: x, na_action=na_action) - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) class TestInterface(BasePeriodTests, base.BaseInterfaceTests): diff --git a/pandas/tests/extension/test_sparse.py b/pandas/tests/extension/test_sparse.py index c22e55952e40d4..92faebd8ac3a17 100644 --- a/pandas/tests/extension/test_sparse.py +++ b/pandas/tests/extension/test_sparse.py @@ -145,7 +145,7 @@ def test_concat_mixed_dtypes(self, data): expected = pd.concat( [x.apply(lambda s: np.asarray(s).astype(object)) for x in dfs] ) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( "columns", @@ -221,7 +221,7 @@ def test_isna(self, data_missing): sarr = sarr.fillna(0) expected_dtype = SparseDtype(bool, pd.isna(data_missing.dtype.fill_value)) expected = SparseArray([False, False], fill_value=False, dtype=expected_dtype) - self.assert_equal(sarr.isna(), expected) + tm.assert_equal(sarr.isna(), expected) def test_fillna_limit_backfill(self, data_missing): warns = (PerformanceWarning, FutureWarning) @@ -258,7 +258,7 @@ def test_fillna_frame(self, data_missing): } ) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) class TestMethods(BaseSparseTests, base.BaseMethodsTests): @@ -310,13 +310,13 @@ def test_where_series(self, data, na_value): expected = pd.Series( cls._from_sequence([a, a, na_value, na_value], dtype=new_dtype) ) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) other = cls._from_sequence([a, b, a, b], dtype=data.dtype) cond = np.array([True, False, True, True]) result = ser.where(cond, other) expected = pd.Series(cls._from_sequence([a, b, b, b], dtype=data.dtype)) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_combine_first(self, data, request): super().test_combine_first(data) @@ -356,7 +356,7 @@ def test_map(self, func, na_action, expected): # GH52096 data = SparseArray([1, np.nan]) result = data.map(func, na_action=na_action) - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) @pytest.mark.parametrize("na_action", [None, "ignore"]) def test_map_raises(self, data, na_action): @@ -372,7 +372,7 @@ def test_astype_str(self, data): # for a non-sparse dtype. result = pd.Series(data[:5]).astype(str) expected = pd.Series([str(x) for x in data[:5]], dtype=object) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) @pytest.mark.xfail(raises=TypeError, reason="no sparse StringDtype") def test_astype_string(self, data): diff --git a/pandas/tests/extension/test_string.py b/pandas/tests/extension/test_string.py index eb166691d33141..cb8fef7734cf9a 100644 --- a/pandas/tests/extension/test_string.py +++ b/pandas/tests/extension/test_string.py @@ -19,6 +19,7 @@ import pytest import pandas as pd +import pandas._testing as tm from pandas.api.types import is_string_dtype from pandas.core.arrays import ArrowStringArray from pandas.core.arrays.string_ import StringDtype @@ -152,7 +153,7 @@ class TestMissing(base.BaseMissingTests): def test_dropna_array(self, data_missing): result = data_missing.dropna() expected = data_missing[[1]] - self.assert_extension_array_equal(result, expected) + tm.assert_extension_array_equal(result, expected) def test_fillna_no_op_returns_copy(self, data): data = data[~data.isna()] @@ -160,11 +161,11 @@ def test_fillna_no_op_returns_copy(self, data): valid = data[0] result = data.fillna(valid) assert result is not data - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) result = data.fillna(method="backfill") assert result is not data - self.assert_extension_array_equal(result, data) + tm.assert_extension_array_equal(result, data) class TestNoReduce(base.BaseNoReduceTests): @@ -196,7 +197,7 @@ def test_value_counts_with_normalize(self, data): else: expected = expected.astype("Float64") - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) class TestCasting(base.BaseCastingTests): @@ -209,7 +210,7 @@ def _compare_other(self, ser, data, op, other): result = getattr(ser, op_name)(other) dtype = "boolean[pyarrow]" if ser.dtype.storage == "pyarrow" else "boolean" expected = getattr(ser.astype(object), op_name)(other).astype(dtype) - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) def test_compare_scalar(self, data, comparison_op): ser = pd.Series(data) @@ -234,10 +235,10 @@ def test_groupby_extension_agg(self, as_index, data_for_grouping): if as_index: index = pd.Index(uniques, name="B") expected = pd.Series([3.0, 1.0, 4.0], index=index, name="A") - self.assert_series_equal(result, expected) + tm.assert_series_equal(result, expected) else: expected = pd.DataFrame({"B": uniques, "A": [3.0, 1.0, 4.0]}) - self.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) @pytest.mark.filterwarnings("ignore:Falling back:pandas.errors.PerformanceWarning") def test_groupby_extension_apply(self, data_for_grouping, groupby_apply_op):