Skip to content

TST: make tests stricter #32527

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Mar 14, 2020
18 changes: 9 additions & 9 deletions pandas/tests/arithmetic/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,13 +913,13 @@ def test_frame_operators(self, float_frame):

# TODO: taken from tests.series.test_operators; needs cleanup
def test_series_operators(self):
def _check_op(series, other, op, pos_only=False, check_dtype=True):
def _check_op(series, other, op, pos_only=False):
left = np.abs(series) if pos_only else series
right = np.abs(other) if pos_only else other

cython_or_numpy = op(left, right)
python = left.combine(right, op)
tm.assert_series_equal(cython_or_numpy, python, check_dtype=check_dtype)
tm.assert_series_equal(cython_or_numpy, python)

def check(series, other):
simple_ops = ["add", "sub", "mul", "truediv", "floordiv", "mod"]
Expand All @@ -942,15 +942,15 @@ def check(series, other):
check(tser, tser[::2])
check(tser, 5)

def check_comparators(series, other, check_dtype=True):
_check_op(series, other, operator.gt, check_dtype=check_dtype)
_check_op(series, other, operator.ge, check_dtype=check_dtype)
_check_op(series, other, operator.eq, check_dtype=check_dtype)
_check_op(series, other, operator.lt, check_dtype=check_dtype)
_check_op(series, other, operator.le, check_dtype=check_dtype)
def check_comparators(series, other):
_check_op(series, other, operator.gt)
_check_op(series, other, operator.ge)
_check_op(series, other, operator.eq)
_check_op(series, other, operator.lt)
_check_op(series, other, operator.le)

check_comparators(tser, 5)
check_comparators(tser, tser + 1, check_dtype=False)
check_comparators(tser, tser + 1)

# TODO: taken from tests.series.test_operators; needs cleanup
def test_divmod(self):
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/dtypes/cast/test_construct_from_scalar.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,4 @@ def test_cast_1d_array_like_from_scalar_categorical():
expected = Categorical(["a", "a"], categories=cats)

result = construct_1d_arraylike_from_scalar("a", len(expected), cat_type)
tm.assert_categorical_equal(
result, expected, check_category_order=True, check_dtype=True
)
tm.assert_categorical_equal(result, expected)
5 changes: 1 addition & 4 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,10 +916,7 @@ def test_constructor_mrecarray(self):
# from GH3479

assert_fr_equal = functools.partial(
tm.assert_frame_equal,
check_index_type=True,
check_column_type=True,
check_frame_type=True,
tm.assert_frame_equal, check_index_type=True, check_column_type=True,
)
arrays = [
("float", np.array([1.5, 2.0])),
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/excel/test_writers.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def test_float_types(self, np_type, path):
reader = ExcelFile(path)
recons = pd.read_excel(reader, "test1", index_col=0).astype(np_type)

tm.assert_frame_equal(df, recons, check_dtype=False)
tm.assert_frame_equal(df, recons)

@pytest.mark.parametrize("np_type", [np.bool8, np.bool_])
def test_bool_types(self, np_type, path):
Expand Down
1 change: 0 additions & 1 deletion pandas/tests/io/json/test_pandas.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,6 @@ def test_blocks_compat_GH9037(self):
df_roundtrip,
check_index_type=True,
check_column_type=True,
check_frame_type=True,
by_blocks=True,
check_exact=True,
)
Expand Down
35 changes: 31 additions & 4 deletions pandas/tests/io/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ def numpy(request):
return request.param


def get_int32_compat_dtype(numpy, orient):
# See GH#32527
dtype = np.int64
if not ((numpy is None or orient == "index") or (numpy is True and orient is None)):
if compat.is_platform_windows():
dtype = np.int32
else:
dtype = np.intp

return dtype


class TestUltraJSONTests:
@pytest.mark.skipif(
compat.is_platform_32bit(), reason="not compliant on 32-bit, xref #15865"
Expand Down Expand Up @@ -833,13 +845,20 @@ def test_dataframe(self, orient, numpy):
if orient == "records" and numpy:
pytest.skip("Not idiomatic pandas")

dtype = get_int32_compat_dtype(numpy, orient)

df = DataFrame(
[[1, 2, 3], [4, 5, 6]], index=["a", "b"], columns=["x", "y", "z"]
[[1, 2, 3], [4, 5, 6]],
index=["a", "b"],
columns=["x", "y", "z"],
dtype=dtype,
)
encode_kwargs = {} if orient is None else dict(orient=orient)
decode_kwargs = {} if numpy is None else dict(numpy=numpy)
assert (df.dtypes == dtype).all()

output = ujson.decode(ujson.encode(df, **encode_kwargs), **decode_kwargs)
assert (df.dtypes == dtype).all()

# Ensure proper DataFrame initialization.
if orient == "split":
Expand All @@ -857,7 +876,8 @@ def test_dataframe(self, orient, numpy):
elif orient == "index":
df = df.transpose()

tm.assert_frame_equal(output, df, check_dtype=False)
assert (df.dtypes == dtype).all()
tm.assert_frame_equal(output, df)

def test_dataframe_nested(self, orient):
df = DataFrame(
Expand Down Expand Up @@ -897,14 +917,20 @@ def test_dataframe_numpy_labelled(self, orient):
tm.assert_frame_equal(output, df)

def test_series(self, orient, numpy):
dtype = get_int32_compat_dtype(numpy, orient)
s = Series(
[10, 20, 30, 40, 50, 60], name="series", index=[6, 7, 8, 9, 10, 15]
[10, 20, 30, 40, 50, 60],
name="series",
index=[6, 7, 8, 9, 10, 15],
dtype=dtype,
).sort_values()
assert s.dtype == dtype

encode_kwargs = {} if orient is None else dict(orient=orient)
decode_kwargs = {} if numpy is None else dict(numpy=numpy)

output = ujson.decode(ujson.encode(s, **encode_kwargs), **decode_kwargs)
assert s.dtype == dtype

if orient == "split":
dec = _clean_dict(output)
Expand All @@ -920,7 +946,8 @@ def test_series(self, orient, numpy):
s.name = None
s.index = [0, 1, 2, 3, 4, 5]

tm.assert_series_equal(output, s, check_dtype=False)
assert s.dtype == dtype
tm.assert_series_equal(output, s)

def test_series_nested(self, orient):
s = Series(
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/io/pytables/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2298,9 +2298,7 @@ def test_index_types(self, setup_path):
with catch_warnings(record=True):
values = np.random.randn(2)

func = lambda l, r: tm.assert_series_equal(
l, r, check_dtype=True, check_index_type=True
)
func = lambda l, r: tm.assert_series_equal(l, r, check_index_type=True)

with catch_warnings(record=True):
ser = Series(values, [0, "y"])
Expand Down
8 changes: 6 additions & 2 deletions pandas/tests/io/test_clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ def df(request):
)
elif data_type == "mixed":
return DataFrame(
{"a": np.arange(1.0, 6.0) + 0.01, "b": np.arange(1, 6), "c": list("abcde")}
{
"a": np.arange(1.0, 6.0) + 0.01,
"b": np.arange(1, 6).astype(np.int64),
"c": list("abcde"),
}
)
elif data_type == "float":
return tm.makeCustomDataframe(
Expand Down Expand Up @@ -146,7 +150,7 @@ class TestClipboard:
def check_round_trip_frame(self, data, excel=None, sep=None, encoding=None):
data.to_clipboard(excel=excel, sep=sep, encoding=encoding)
result = read_clipboard(sep=sep or "\t", index_col=0, encoding=encoding)
tm.assert_frame_equal(data, result, check_dtype=False)
tm.assert_frame_equal(data, result)

# Test that default arguments copy as tab delimited
def test_round_trip_frame(self, df):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ def test_join_on_inner(self):

expected = df.join(df2, on="key")
expected = expected[expected["value"].notna()]
tm.assert_series_equal(joined["key"], expected["key"], check_dtype=False)
tm.assert_series_equal(joined["key"], expected["key"])
tm.assert_series_equal(joined["value"], expected["value"], check_dtype=False)
tm.assert_index_equal(joined.index, expected.index)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/reshape/test_union_categoricals.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def test_union_categorical(self):
for box in [Categorical, CategoricalIndex, Series]:
result = union_categoricals([box(Categorical(a)), box(Categorical(b))])
expected = Categorical(combined)
tm.assert_categorical_equal(result, expected, check_category_order=True)
tm.assert_categorical_equal(result, expected)

# new categories ordered by appearance
s = Categorical(["x", "y", "z"])
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/methods/test_argsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def test_argsort_stable(self):
mexpected = np.argsort(s.values, kind="mergesort")
qexpected = np.argsort(s.values, kind="quicksort")

tm.assert_series_equal(mindexer, Series(mexpected), check_dtype=False)
tm.assert_series_equal(qindexer, Series(qexpected), check_dtype=False)
tm.assert_series_equal(mindexer.astype(np.intp), Series(mexpected))
tm.assert_series_equal(qindexer.astype(np.intp), Series(qexpected))
msg = (
r"ndarray Expected type <class 'numpy\.ndarray'>, "
r"found <class 'pandas\.core\.series\.Series'> instead"
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/methods/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ def test_replace_categorical(self, categorical, numeric):
s = pd.Series(categorical)
result = s.replace({"A": 1, "B": 2})
expected = pd.Series(numeric)
tm.assert_series_equal(expected, result, check_dtype=False)
tm.assert_series_equal(expected, result)

def test_replace_categorical_single(self):
# GH 26988
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_duplicates.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ def test_unique():

# GH 18051
s = Series(Categorical([]))
tm.assert_categorical_equal(s.unique(), Categorical([]), check_dtype=False)
tm.assert_categorical_equal(s.unique(), Categorical([]))
s = Series(Categorical([np.nan]))
tm.assert_categorical_equal(s.unique(), Categorical([np.nan]), check_dtype=False)
tm.assert_categorical_equal(s.unique(), Categorical([np.nan]))


def test_unique_data_ownership():
Expand Down