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
4 changes: 1 addition & 3 deletions pandas/tests/generic/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ def test_to_xarray_index_types(self, index):
assert isinstance(result, DataArray)

# idempotency
tm.assert_series_equal(
result.to_series(), s, check_index_type=False, check_categorical=True
)
tm.assert_series_equal(result.to_series(), s, check_index_type=False)

@td.skip_if_no("xarray", min_version="0.7.0")
def test_to_xarray(self):
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
31 changes: 27 additions & 4 deletions pandas/tests/io/json/test_ujson.py
Original file line number Diff line number Diff line change
Expand Up @@ -833,13 +833,24 @@ def test_dataframe(self, orient, numpy):
if orient == "records" and numpy:
pytest.skip("Not idiomatic pandas")

dtype = np.intp if not compat.is_platform_windows() else np.int32
if numpy is None or orient == "index":
dtype = np.int64
if numpy is True and orient is None:
dtype = np.int64

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 +868,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 +909,24 @@ def test_dataframe_numpy_labelled(self, orient):
tm.assert_frame_equal(output, df)

def test_series(self, orient, numpy):
dtype = np.intp if not compat.is_platform_windows() else np.int32
if numpy is None or orient == "index":
dtype = np.int64
if numpy is True and orient is None:
dtype = np.int64
Copy link
Member Author

Choose a reason for hiding this comment

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

@WillAyd do these conditional dtypes make sense? i was surprised that the windows special-casing was needed

Copy link
Member

Choose a reason for hiding this comment

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

I think this is related to #28560 which I closed since we deprecated the numpy argument. Are you seeing this issue generally on Windows?

Copy link
Member Author

Choose a reason for hiding this comment

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

this was necessary to get the CI passing. i havent run any other windows builds

Copy link
Member Author

Choose a reason for hiding this comment

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

are you OK with these being here like this? maybe a comment about them being made unnecessary at some point?

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 refactor this to start with np.int64 and only special case the 32 bit needs? I think that would make it more logical to follow / clean up whenever that argument is removed

Copy link
Contributor

Choose a reason for hiding this comment

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

you can make this a fixture as well

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 +942,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
14 changes: 2 additions & 12 deletions pandas/tests/io/pytables/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
from pandas.compat import is_platform_little_endian, is_platform_windows
import pandas.util._test_decorators as td

from pandas.core.dtypes.common import is_categorical_dtype

import pandas as pd
from pandas import (
Categorical,
Expand Down Expand Up @@ -1057,13 +1055,7 @@ def test_latin_encoding(self, setup_path, dtype, val):

s_nan = ser.replace(nan_rep, np.nan)

if is_categorical_dtype(s_nan):
assert is_categorical_dtype(retr)
tm.assert_series_equal(
s_nan, retr, check_dtype=False, check_categorical=False
)
else:
tm.assert_series_equal(s_nan, retr)
tm.assert_series_equal(s_nan, retr)

# FIXME: don't leave commented-out
# fails:
Expand Down Expand Up @@ -2311,9 +2303,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
2 changes: 1 addition & 1 deletion pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def test_constructor_categorical_dtype(self):
expected = Series(
["a", "a"], index=[0, 1], dtype=CategoricalDtype(["a", "b"], ordered=True)
)
tm.assert_series_equal(result, expected, check_categorical=True)
tm.assert_series_equal(result, expected)

def test_constructor_categorical_string(self):
# GH 26336: the string 'category' maintains existing CategoricalDtype
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