Skip to content

Commit

Permalink
REF/TST: remove overriding tm.assert_foo pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrockmendel committed Aug 1, 2023
1 parent ae6a335 commit 04474de
Show file tree
Hide file tree
Showing 25 changed files with 258 additions and 265 deletions.
9 changes: 0 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 0 additions & 7 deletions pandas/tests/extension/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/extension/base/accumulate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest

import pandas as pd
import pandas._testing as tm
from pandas.tests.extension.base.base import BaseExtensionTests


Expand All @@ -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):
Expand Down
13 changes: 7 additions & 6 deletions pandas/tests/extension/base/casting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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",
Expand All @@ -61,27 +62,27 @@ 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):
# ensure that astype returns the original object for equal dtype and copy=False
# 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)
29 changes: 15 additions & 14 deletions pandas/tests/extension/base/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]]
Expand All @@ -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):
Expand Down Expand Up @@ -90,38 +91,38 @@ 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
result = pd.DataFrame(columns=["a"], dtype=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()
Expand Down
39 changes: 20 additions & 19 deletions pandas/tests/extension/base/dim2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -32,35 +33,35 @@ 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)

# 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)

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
Expand Down Expand Up @@ -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]
Expand All @@ -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)
Expand Down Expand Up @@ -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"
Expand All @@ -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]
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/extension/base/dtype.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit 04474de

Please sign in to comment.