From ddcbf200544de407e90e88a42eb18482d68cf469 Mon Sep 17 00:00:00 2001 From: Daniel Saxton Date: Thu, 23 Apr 2020 14:42:21 -0500 Subject: [PATCH] CLN: Parametrize dtype inference tests --- pandas/tests/dtypes/test_inference.py | 106 +++++++++++++------------- 1 file changed, 52 insertions(+), 54 deletions(-) diff --git a/pandas/tests/dtypes/test_inference.py b/pandas/tests/dtypes/test_inference.py index 8c0580b7cf047..8b20f9ada8ff7 100644 --- a/pandas/tests/dtypes/test_inference.py +++ b/pandas/tests/dtypes/test_inference.py @@ -354,71 +354,69 @@ def test_is_recompilable_fails(ll): class TestInference: - def test_infer_dtype_bytes(self): - compare = "bytes" - - # string array of bytes - arr = np.array(list("abc"), dtype="S1") - assert lib.infer_dtype(arr, skipna=True) == compare + @pytest.mark.parametrize( + "arr", + [ + np.array(list("abc"), dtype="S1"), + np.array(list("abc"), dtype="S1").astype(object), + [b"a", np.nan, b"c"], + ], + ) + def test_infer_dtype_bytes(self, arr): + result = lib.infer_dtype(arr, skipna=True) + assert result == "bytes" - # object array of bytes - arr = arr.astype(object) - assert lib.infer_dtype(arr, skipna=True) == compare + @pytest.mark.parametrize( + "value, expected", + [ + (float("inf"), True), + (np.inf, True), + (-np.inf, False), + (1, False), + ("a", False), + ], + ) + def test_isposinf_scalar(self, value, expected): + # GH 11352 + result = libmissing.isposinf_scalar(value) + assert result is expected - # object array of bytes with missing values - assert lib.infer_dtype([b"a", np.nan, b"c"], skipna=True) == compare + @pytest.mark.parametrize( + "value, expected", + [ + (float("-inf"), True), + (-np.inf, True), + (np.inf, False), + (1, False), + ("a", False), + ], + ) + def test_isneginf_scalar(self, value, expected): + result = libmissing.isneginf_scalar(value) + assert result is expected - def test_isinf_scalar(self): - # GH 11352 - assert libmissing.isposinf_scalar(float("inf")) - assert libmissing.isposinf_scalar(np.inf) - assert not libmissing.isposinf_scalar(-np.inf) - assert not libmissing.isposinf_scalar(1) - assert not libmissing.isposinf_scalar("a") - - assert libmissing.isneginf_scalar(float("-inf")) - assert libmissing.isneginf_scalar(-np.inf) - assert not libmissing.isneginf_scalar(np.inf) - assert not libmissing.isneginf_scalar(1) - assert not libmissing.isneginf_scalar("a") - - @pytest.mark.parametrize("maybe_int", [True, False]) + @pytest.mark.parametrize("coerce_numeric", [True, False]) @pytest.mark.parametrize( "infinity", ["inf", "inF", "iNf", "Inf", "iNF", "InF", "INf", "INF"] ) - def test_maybe_convert_numeric_infinities(self, infinity, maybe_int): + @pytest.mark.parametrize("prefix", ["", "-", "+"]) + def test_maybe_convert_numeric_infinities(self, coerce_numeric, infinity, prefix): # see gh-13274 - na_values = {"", "NULL", "nan"} - - pos = np.array(["inf"], dtype=np.float64) - neg = np.array(["-inf"], dtype=np.float64) - - msg = "Unable to parse string" - - out = lib.maybe_convert_numeric( - np.array([infinity], dtype=object), na_values, maybe_int - ) - tm.assert_numpy_array_equal(out, pos) - - out = lib.maybe_convert_numeric( - np.array(["-" + infinity], dtype=object), na_values, maybe_int - ) - tm.assert_numpy_array_equal(out, neg) - - out = lib.maybe_convert_numeric( - np.array([infinity], dtype=object), na_values, maybe_int - ) - tm.assert_numpy_array_equal(out, pos) - - out = lib.maybe_convert_numeric( - np.array(["+" + infinity], dtype=object), na_values, maybe_int + result = lib.maybe_convert_numeric( + np.array([prefix + infinity], dtype=object), + na_values={"", "NULL", "nan"}, + coerce_numeric=coerce_numeric, ) - tm.assert_numpy_array_equal(out, pos) + expected = np.array([np.inf if prefix in ["", "+"] else -np.inf]) + tm.assert_numpy_array_equal(result, expected) - # too many characters + def test_maybe_convert_numeric_infinities_raises(self): + msg = "Unable to parse string" with pytest.raises(ValueError, match=msg): lib.maybe_convert_numeric( - np.array(["foo_" + infinity], dtype=object), na_values, maybe_int + np.array(["foo_inf"], dtype=object), + na_values={"", "NULL", "nan"}, + coerce_numeric=False, ) def test_maybe_convert_numeric_post_floatify_nan(self, coerce):