From 894784278a64127c34fa953c5e7d3d31022d41cd Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Thu, 7 Apr 2022 15:53:42 -0700 Subject: [PATCH 1/4] TST: Parameterize --- .../reshape/concat/test_append_common.py | 13 +++++++++--- pandas/tests/reshape/concat/test_empty.py | 20 +++++++++---------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/pandas/tests/reshape/concat/test_append_common.py b/pandas/tests/reshape/concat/test_append_common.py index 0a330fd12d76d..527ec43437469 100644 --- a/pandas/tests/reshape/concat/test_append_common.py +++ b/pandas/tests/reshape/concat/test_append_common.py @@ -70,11 +70,18 @@ def _check_expected_dtype(self, obj, label): else: raise ValueError - def test_dtypes(self, item): + @pytest.mark.parametrize("box", [Index, Series]) + def test_dtypes(self, item, box): # to confirm test case covers intended dtypes typ, vals = item - self._check_expected_dtype(Index(vals), typ) - self._check_expected_dtype(Series(vals), typ) + obj = box(vals) + if isinstance(obj, Index): + assert obj.dtype == typ + elif isinstance(obj, Series): + if typ.startswith("period"): + assert obj.dtype == "Period[M]" + else: + assert obj.dtype == typ def test_concatlike_same_dtypes(self, item): # GH 13660 diff --git a/pandas/tests/reshape/concat/test_empty.py b/pandas/tests/reshape/concat/test_empty.py index 82d2a8a2b1fd2..69073b3b3f507 100644 --- a/pandas/tests/reshape/concat/test_empty.py +++ b/pandas/tests/reshape/concat/test_empty.py @@ -129,10 +129,13 @@ def test_concat_empty_series_dtypes_match_roundtrips(self, dtype): result = concat([Series(dtype=dtype), Series(dtype=dtype)]) assert result.dtype == dtype - def test_concat_empty_series_dtypes_roundtrips(self): + @pytest.mark.parametrize("dtype", map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"])) + @pytest.mark.parametrize("dtype2", map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"])) + def test_concat_empty_series_dtypes_roundtrips(self, dtype, dtype2): # round-tripping with self & like self - dtypes = map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"]) + if dtype == dtype2: + return def int_result_type(dtype, dtype2): typs = {dtype.kind, dtype2.kind} @@ -163,14 +166,11 @@ def get_result_type(dtype, dtype2): return result return "O" - for dtype in dtypes: - for dtype2 in dtypes: - if dtype == dtype2: - continue - - expected = get_result_type(dtype, dtype2) - result = concat([Series(dtype=dtype), Series(dtype=dtype2)]).dtype - assert result.kind == expected + dtype = np.dtype(dtype) + dtype2 = np.dtype(dtype2) + expected = get_result_type(dtype, dtype2) + result = concat([Series(dtype=dtype), Series(dtype=dtype2)]).dtype + assert result.kind == expected def test_concat_empty_series_dtypes_triple(self): From 74d1b89d1621c26ff88f2973f6a94aa013e4a71f Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Thu, 7 Apr 2022 16:22:35 -0700 Subject: [PATCH 2/4] Param test --- .../tests/reshape/merge/test_merge_ordered.py | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/pandas/tests/reshape/merge/test_merge_ordered.py b/pandas/tests/reshape/merge/test_merge_ordered.py index 4d3dc05571d1d..a7f914c326551 100644 --- a/pandas/tests/reshape/merge/test_merge_ordered.py +++ b/pandas/tests/reshape/merge/test_merge_ordered.py @@ -76,24 +76,21 @@ def _constructor(self): assert isinstance(result, NotADataFrame) - def test_empty_sequence_concat(self): + @pytest.mark.parametrize("df_seq, pattern", [ + ((), "[Nn]o objects"), + ([], "[Nn]o objects"), + ({}, "[Nn]o objects"), + ([None], "objects.*None"), + ([None, None], "objects.*None"), + ]) + def test_empty_sequence_concat(self, df_seq, pattern): # GH 9157 - empty_pat = "[Nn]o objects" - none_pat = "objects.*None" - test_cases = [ - ((), empty_pat), - ([], empty_pat), - ({}, empty_pat), - ([None], none_pat), - ([None, None], none_pat), - ] - for df_seq, pattern in test_cases: - with pytest.raises(ValueError, match=pattern): - pd.concat(df_seq) - - pd.concat([DataFrame()]) - pd.concat([None, DataFrame()]) - pd.concat([DataFrame(), None]) + with pytest.raises(ValueError, match=pattern): + pd.concat(df_seq) + + @pytest.mark.parametrize("arg", [[DataFrame()], [None, DataFrame()], [DataFrame(), None]]) + def test_empty_sequence_concat_ok(self, arg): + pd.concat(arg) def test_doc_example(self): left = DataFrame( From addc865a91b24101184a34ae902488637e8fed23 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 8 Apr 2022 20:44:15 -0700 Subject: [PATCH 3/4] format --- pandas/tests/reshape/concat/test_empty.py | 9 +++++++-- pandas/tests/reshape/merge/test_merge_ordered.py | 11 ++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pandas/tests/reshape/concat/test_empty.py b/pandas/tests/reshape/concat/test_empty.py index 69073b3b3f507..962110b609140 100644 --- a/pandas/tests/reshape/concat/test_empty.py +++ b/pandas/tests/reshape/concat/test_empty.py @@ -129,8 +129,13 @@ def test_concat_empty_series_dtypes_match_roundtrips(self, dtype): result = concat([Series(dtype=dtype), Series(dtype=dtype)]) assert result.dtype == dtype - @pytest.mark.parametrize("dtype", map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"])) - @pytest.mark.parametrize("dtype2", map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"])) + @pytest.mark.parametrize( + "dtype", map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"]) + ) + @pytest.mark.parametrize( + "dtype2", + map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"]), + ) def test_concat_empty_series_dtypes_roundtrips(self, dtype, dtype2): # round-tripping with self & like self diff --git a/pandas/tests/reshape/merge/test_merge_ordered.py b/pandas/tests/reshape/merge/test_merge_ordered.py index a7f914c326551..cdff4737a3078 100644 --- a/pandas/tests/reshape/merge/test_merge_ordered.py +++ b/pandas/tests/reshape/merge/test_merge_ordered.py @@ -76,19 +76,24 @@ def _constructor(self): assert isinstance(result, NotADataFrame) - @pytest.mark.parametrize("df_seq, pattern", [ + @pytest.mark.parametrize( + "df_seq, pattern", + [ ((), "[Nn]o objects"), ([], "[Nn]o objects"), ({}, "[Nn]o objects"), ([None], "objects.*None"), ([None, None], "objects.*None"), - ]) + ], + ) def test_empty_sequence_concat(self, df_seq, pattern): # GH 9157 with pytest.raises(ValueError, match=pattern): pd.concat(df_seq) - @pytest.mark.parametrize("arg", [[DataFrame()], [None, DataFrame()], [DataFrame(), None]]) + @pytest.mark.parametrize( + "arg", [[DataFrame()], [None, DataFrame()], [DataFrame(), None]] + ) def test_empty_sequence_concat_ok(self, arg): pd.concat(arg) From 079975699859ab034a8ec131a36390cca57e9912 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sat, 9 Apr 2022 10:22:51 -0700 Subject: [PATCH 4/4] Fix test --- pandas/tests/reshape/concat/test_empty.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/tests/reshape/concat/test_empty.py b/pandas/tests/reshape/concat/test_empty.py index 962110b609140..d78337131bb97 100644 --- a/pandas/tests/reshape/concat/test_empty.py +++ b/pandas/tests/reshape/concat/test_empty.py @@ -129,12 +129,10 @@ def test_concat_empty_series_dtypes_match_roundtrips(self, dtype): result = concat([Series(dtype=dtype), Series(dtype=dtype)]) assert result.dtype == dtype - @pytest.mark.parametrize( - "dtype", map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"]) - ) + @pytest.mark.parametrize("dtype", ["float64", "int8", "uint8", "m8[ns]", "M8[ns]"]) @pytest.mark.parametrize( "dtype2", - map(np.dtype, ["float64", "int8", "uint8", "bool", "m8[ns]", "M8[ns]"]), + ["float64", "int8", "uint8", "m8[ns]", "M8[ns]"], ) def test_concat_empty_series_dtypes_roundtrips(self, dtype, dtype2):