Skip to content

CLN: tests/extension/json/test_json.py typefix #28994

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

Closed
wants to merge 19 commits into from
Closed
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
10 changes: 6 additions & 4 deletions pandas/tests/extension/base/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@


class BaseExtensionTests:
@staticmethod
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you get rid of these methods altogether and just import / use as function within tests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, since some tests need to override these

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm OK - so was this change required? Looks like using tm.assert_* in the tests now?

def assert_series_equal(left, right, **kwargs):
tm.assert_series_equal(left, right, **kwargs)

assert_equal = staticmethod(tm.assert_equal)
assert_series_equal = staticmethod(tm.assert_series_equal)
assert_frame_equal = staticmethod(tm.assert_frame_equal)
assert_extension_array_equal = staticmethod(tm.assert_extension_array_equal)
@staticmethod
def assert_frame_equal(left, right, **kwargs):
tm.assert_frame_equal(left, right, **kwargs)
9 changes: 5 additions & 4 deletions pandas/tests/extension/base/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

import pandas as pd
from pandas.core.internals import ExtensionBlock
import pandas.util.testing as tm

from .base import BaseExtensionTests


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 Down Expand Up @@ -67,10 +68,10 @@ def test_from_dtype(self, data):
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.PandasArray(np.asarray(data, dtype=object))
self.assert_equal(result, expected)
tm.assert_equal(result, expected)
5 changes: 3 additions & 2 deletions pandas/tests/extension/base/getitem.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.util.testing as tm

from .base import BaseExtensionTests

Expand Down Expand Up @@ -200,7 +201,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
Expand All @@ -209,7 +210,7 @@ def test_take_non_na_fill_value(self, data_missing):
array = data_missing._from_sequence([na, fill_value, na])
result = array.take([-1, 1], fill_value=fill_value, allow_fill=True)
expected = array.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):
Expand Down
22 changes: 11 additions & 11 deletions pandas/tests/extension/base/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,23 @@ def test_factorize(self, data_for_grouping, na_sentinel):
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)

@pytest.mark.parametrize("na_sentinel", [-1, -2])
def test_factorize_equivalence(self, data_for_grouping, na_sentinel):
codes_1, uniques_1 = pd.factorize(data_for_grouping, na_sentinel=na_sentinel)
codes_2, uniques_2 = data_for_grouping.factorize(na_sentinel=na_sentinel)

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)

def test_factorize_empty(self, data):
codes, uniques = pd.factorize(data[:0])
expected_codes = np.array([], dtype=np.intp)
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])
Expand Down Expand Up @@ -240,26 +240,26 @@ 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):
# https://github.com/pandas-dev/pandas/issues/23911
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_fill_value(self, data):
arr = data[:4]
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_hash_pandas_object_works(self, data, as_frame):
# https://github.com/pandas-dev/pandas/issues/23066
Expand All @@ -268,7 +268,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):
b, c, a = data_for_sorting
Expand Down Expand Up @@ -313,7 +313,7 @@ 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)

# array other
cond = np.array([True, False, True, True])
Expand All @@ -325,7 +325,7 @@ 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)

@pytest.mark.parametrize("repeats", [0, 1, 2, [1, 2, 3]])
def test_repeat(self, data, repeats, as_series, use_numpy):
Expand All @@ -341,7 +341,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",
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/extension/base/missing.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_isna(self, data_missing):
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)
Expand Down Expand Up @@ -56,7 +56,7 @@ 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)

def test_fillna_limit_pad(self, data_missing):
arr = data_missing.take([1, 0, 0, 0, 1])
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/extension/base/reshaping.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import pandas as pd
from pandas.core.internals import ExtensionBlock
import pandas.util.testing as tm

from .base import BaseExtensionTests

Expand Down Expand Up @@ -239,7 +240,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",
Expand Down
5 changes: 3 additions & 2 deletions pandas/tests/extension/base/setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest

import pandas as pd
import pandas.util.testing as tm

from .base import BaseExtensionTests

Expand Down Expand Up @@ -46,7 +47,7 @@ def test_setitem_empty_indxer(self, data, 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:
Expand Down Expand Up @@ -180,7 +181,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()
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/decimal/test_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,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 TestMissing(BaseDecimal, base.BaseMissingTests):
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/extension/json/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def assert_series_equal(self, left, right, **kwargs):
)
tm.assert_series_equal(left, right, **kwargs)

def assert_frame_equal(self, left, right, *args, **kwargs):
def assert_frame_equal(self, left, right, **kwargs):
tm.assert_index_equal(
left.columns,
right.columns,
Expand All @@ -106,11 +106,11 @@ def assert_frame_equal(self, left, right, *args, **kwargs):
jsons = (left.dtypes == "json").index

for col in jsons:
self.assert_series_equal(left[col], right[col], *args, **kwargs)
self.assert_series_equal(left[col], right[col], **kwargs)

left = left.drop(columns=jsons)
right = right.drop(columns=jsons)
tm.assert_frame_equal(left, right, *args, **kwargs)
tm.assert_frame_equal(left, right, **kwargs)


class TestDtype(BaseJSON, base.BaseDtypeTests):
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/extension/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pandas as pd
from pandas.core.arrays import DatetimeArray
from pandas.tests.extension import base
import pandas.util.testing as tm


@pytest.fixture(params=["US/Central"])
Expand Down Expand Up @@ -199,7 +200,7 @@ def test_unstack(self, obj):
expected.columns.names = [None, "a"]

result = ser.unstack(0)
self.assert_equal(result, expected)
tm.assert_equal(result, expected)


class TestSetitem(BaseDatetimeTests, base.BaseSetitemTests):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def test_isna(self, data_missing):
expected = SparseArray([True, False], dtype=expected_dtype)

result = pd.isna(data_missing)
self.assert_equal(result, expected)
tm.assert_equal(result, expected)

result = pd.Series(data_missing).isna()
expected = pd.Series(expected)
Expand Down