From 1dc983e299f27c20b5deb5fa008c82f6d2d53f7e Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Thu, 4 Jan 2024 14:49:34 -0800 Subject: [PATCH 1/4] TST/CLN: Tests parametrizations --- pandas/tests/plotting/frame/test_frame.py | 8 +-- .../tests/plotting/frame/test_frame_color.py | 5 +- pandas/tests/reductions/test_reductions.py | 40 +++++-------- pandas/tests/reshape/concat/test_series.py | 7 +-- pandas/tests/reshape/merge/test_merge.py | 24 ++++---- pandas/tests/reshape/merge/test_merge_asof.py | 2 +- .../tests/reshape/merge/test_merge_ordered.py | 57 +++++++++---------- pandas/tests/reshape/test_from_dummies.py | 26 +++++---- pandas/tests/reshape/test_get_dummies.py | 9 +-- pandas/tests/reshape/test_melt.py | 32 +++++------ pandas/tests/reshape/test_pivot.py | 13 +---- pandas/tests/reshape/test_qcut.py | 10 ++-- pandas/tests/scalar/period/test_arithmetic.py | 7 ++- .../scalar/timedelta/methods/test_round.py | 28 ++++----- .../tests/scalar/timedelta/test_arithmetic.py | 5 +- .../scalar/timedelta/test_constructors.py | 15 +++-- .../scalar/timestamp/methods/test_round.py | 8 --- .../scalar/timestamp/test_constructors.py | 11 ++-- .../tests/scalar/timestamp/test_timestamp.py | 8 +-- .../series/accessors/test_dt_accessor.py | 11 ++-- pandas/tests/series/indexing/test_getitem.py | 9 +-- pandas/tests/series/indexing/test_indexing.py | 10 ++-- pandas/tests/series/indexing/test_setitem.py | 10 +--- pandas/tests/series/indexing/test_where.py | 10 +--- pandas/tests/series/methods/test_astype.py | 17 +++--- pandas/tests/series/methods/test_diff.py | 11 ++-- pandas/tests/series/methods/test_drop.py | 12 ++-- .../series/methods/test_drop_duplicates.py | 8 +-- .../tests/series/methods/test_duplicated.py | 14 +++-- pandas/tests/series/methods/test_explode.py | 9 +-- pandas/tests/series/methods/test_fillna.py | 14 ++--- pandas/tests/series/methods/test_info.py | 16 +++--- pandas/tests/series/methods/test_isin.py | 11 +--- pandas/tests/series/methods/test_replace.py | 8 +-- pandas/tests/series/methods/test_update.py | 12 ++-- .../tests/series/methods/test_value_counts.py | 26 ++------- pandas/tests/series/test_arithmetic.py | 18 ++---- pandas/tests/series/test_constructors.py | 21 +++---- pandas/tests/series/test_cumulative.py | 5 +- pandas/tests/series/test_logical_ops.py | 7 ++- 40 files changed, 246 insertions(+), 328 deletions(-) diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index 45dc612148f40..7e0b8dc7282e4 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -1199,12 +1199,10 @@ def test_hist_df_orientation(self): axes = df.plot.hist(rot=50, fontsize=8, orientation="horizontal") _check_ticks_props(axes, xrot=0, yrot=50, ylabelsize=8) - @pytest.mark.parametrize( - "weights", [0.1 * np.ones(shape=(100,)), 0.1 * np.ones(shape=(100, 2))] - ) - def test_hist_weights(self, weights): + @pytest.mark.parametrize("weight_shape", [(100,), (100, 2)]) + def test_hist_weights(self, weight_shape): # GH 33173 - + weights = 0.1 * np.ones(shape=weight_shape) df = DataFrame( dict(zip(["A", "B"], np.random.default_rng(2).standard_normal((2, 100)))) ) diff --git a/pandas/tests/plotting/frame/test_frame_color.py b/pandas/tests/plotting/frame/test_frame_color.py index ff1edd323ef28..4f14f1e43cf29 100644 --- a/pandas/tests/plotting/frame/test_frame_color.py +++ b/pandas/tests/plotting/frame/test_frame_color.py @@ -30,11 +30,10 @@ def _check_colors_box(bp, box_c, whiskers_c, medians_c, caps_c="k", fliers_c=Non class TestDataFrameColor: - @pytest.mark.parametrize( - "color", ["C0", "C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9"] - ) + @pytest.mark.parametrize("color", list(range(10))) def test_mpl2_color_cycle_str(self, color): # GH 15516 + color = f"C{color}" df = DataFrame( np.random.default_rng(2).standard_normal((10, 3)), columns=["a", "b", "c"] ) diff --git a/pandas/tests/reductions/test_reductions.py b/pandas/tests/reductions/test_reductions.py index 4cdd50d70d078..5e447fbf39b0e 100644 --- a/pandas/tests/reductions/test_reductions.py +++ b/pandas/tests/reductions/test_reductions.py @@ -1144,18 +1144,7 @@ def test_timedelta64_analytics(self): expected = Timedelta("1 days") assert result == expected - @pytest.mark.parametrize( - "test_input,error_type", - [ - (Series([], dtype="float64"), ValueError), - # For strings, or any Series with dtype 'O' - (Series(["foo", "bar", "baz"]), TypeError), - (Series([(1,), (2,)]), TypeError), - # For mixed data types - (Series(["foo", "foo", "bar", "bar", None, np.nan, "baz"]), TypeError), - ], - ) - def test_assert_idxminmax_empty_raises(self, test_input, error_type): + def test_assert_idxminmax_empty_raises(self): """ Cases where ``Series.argmax`` and related should raise an exception """ @@ -1294,13 +1283,14 @@ def test_minmax_nat_series(self, nat_ser): @pytest.mark.parametrize( "nat_df", [ - DataFrame([NaT, NaT]), - DataFrame([NaT, Timedelta("nat")]), - DataFrame([Timedelta("nat"), Timedelta("nat")]), + [NaT, NaT], + [NaT, Timedelta("nat")], + [Timedelta("nat"), Timedelta("nat")], ], ) def test_minmax_nat_dataframe(self, nat_df): # GH#23282 + nat_df = DataFrame(nat_df) assert nat_df.min()[0] is NaT assert nat_df.max()[0] is NaT assert nat_df.min(skipna=False)[0] is NaT @@ -1399,14 +1389,10 @@ class TestSeriesMode: # were moved from a series-specific test file, _not_ that these tests are # intended long-term to be series-specific - @pytest.mark.parametrize( - "dropna, expected", - [(True, Series([], dtype=np.float64)), (False, Series([], dtype=np.float64))], - ) - def test_mode_empty(self, dropna, expected): + def test_mode_empty(self, dropna): s = Series([], dtype=np.float64) result = s.mode(dropna) - tm.assert_series_equal(result, expected) + tm.assert_series_equal(result, s) @pytest.mark.parametrize( "dropna, data, expected", @@ -1619,23 +1605,24 @@ def test_mode_boolean_with_na(self): [ ( [0, 1j, 1, 1, 1 + 1j, 1 + 2j], - Series([1], dtype=np.complex128), + [1], np.complex128, ), ( [0, 1j, 1, 1, 1 + 1j, 1 + 2j], - Series([1], dtype=np.complex64), + [1], np.complex64, ), ( [1 + 1j, 2j, 1 + 1j], - Series([1 + 1j], dtype=np.complex128), + [1 + 1j], np.complex128, ), ], ) def test_single_mode_value_complex(self, array, expected, dtype): result = Series(array, dtype=dtype).mode() + expected = Series(expected, dtype=dtype) tm.assert_series_equal(result, expected) @pytest.mark.parametrize( @@ -1644,12 +1631,12 @@ def test_single_mode_value_complex(self, array, expected, dtype): ( # no modes [0, 1j, 1, 1 + 1j, 1 + 2j], - Series([0j, 1j, 1 + 0j, 1 + 1j, 1 + 2j], dtype=np.complex128), + [0j, 1j, 1 + 0j, 1 + 1j, 1 + 2j], np.complex128, ), ( [1 + 1j, 2j, 1 + 1j, 2j, 3], - Series([2j, 1 + 1j], dtype=np.complex64), + [2j, 1 + 1j], np.complex64, ), ], @@ -1659,4 +1646,5 @@ def test_multimode_complex(self, array, expected, dtype): # mode tries to sort multimodal series. # Complex numbers are sorted by their magnitude result = Series(array, dtype=dtype).mode() + expected = Series(expected, dtype=dtype) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/reshape/concat/test_series.py b/pandas/tests/reshape/concat/test_series.py index c12b835cb61e1..bdc64fe826cc9 100644 --- a/pandas/tests/reshape/concat/test_series.py +++ b/pandas/tests/reshape/concat/test_series.py @@ -128,11 +128,10 @@ def test_concat_series_axis1_same_names_ignore_index(self): tm.assert_index_equal(result.columns, expected, exact=True) - @pytest.mark.parametrize( - "s1name,s2name", [(np.int64(190), (43, 0)), (190, (43, 0))] - ) - def test_concat_series_name_npscalar_tuple(self, s1name, s2name): + @pytest.mark.parametrize("s1name", [np.int64(190), 190]) + def test_concat_series_name_npscalar_tuple(self, s1name): # GH21015 + s2name = (43, 0) s1 = Series({"a": 1, "b": 2}, name=s1name) s2 = Series({"c": 5, "d": 6}, name=s2name) result = concat([s1, s2]) diff --git a/pandas/tests/reshape/merge/test_merge.py b/pandas/tests/reshape/merge/test_merge.py index 3b37ffa7baa82..ff83cbc55f7c3 100644 --- a/pandas/tests/reshape/merge/test_merge.py +++ b/pandas/tests/reshape/merge/test_merge.py @@ -1466,10 +1466,8 @@ def _check_merge(x, y): class TestMergeDtypes: - @pytest.mark.parametrize( - "right_vals", [["foo", "bar"], Series(["foo", "bar"]).astype("category")] - ) - def test_different(self, right_vals): + @pytest.mark.parametrize("dtype", [object, "category"]) + def test_different(self, dtype): left = DataFrame( { "A": ["foo", "bar"], @@ -1480,6 +1478,7 @@ def test_different(self, right_vals): "F": Series([1, 2], dtype="int32"), } ) + right_vals = Series(["foo", "bar"], dtype=dtype) right = DataFrame({"A": right_vals}) # GH 9780 @@ -2312,19 +2311,15 @@ def test_merge_suffix(col1, col2, kwargs, expected_cols): [ ( "right", - DataFrame( - {"A": [100, 200, 300], "B1": [60, 70, np.nan], "B2": [600, 700, 800]} - ), + {"A": [100, 200, 300], "B1": [60, 70, np.nan], "B2": [600, 700, 800]}, ), ( "outer", - DataFrame( - { - "A": [1, 100, 200, 300], - "B1": [80, 60, 70, np.nan], - "B2": [np.nan, 600, 700, 800], - } - ), + { + "A": [1, 100, 200, 300], + "B1": [80, 60, 70, np.nan], + "B2": [np.nan, 600, 700, 800], + }, ), ], ) @@ -2332,6 +2327,7 @@ def test_merge_duplicate_suffix(how, expected): left_df = DataFrame({"A": [100, 200, 1], "B": [60, 70, 80]}) right_df = DataFrame({"A": [100, 200, 300], "B": [600, 700, 800]}) result = merge(left_df, right_df, on="A", how=how, suffixes=("_x", "_x")) + expected = DataFrame(expected) expected.columns = ["A", "B_x", "B_x"] tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/reshape/merge/test_merge_asof.py b/pandas/tests/reshape/merge/test_merge_asof.py index 00df8064d5190..b0efbc253c04e 100644 --- a/pandas/tests/reshape/merge/test_merge_asof.py +++ b/pandas/tests/reshape/merge/test_merge_asof.py @@ -3099,7 +3099,7 @@ def test_merge_groupby_multiple_column_with_categorical_column(self): tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( - "func", [lambda x: x, lambda x: to_datetime(x)], ids=["numeric", "datetime"] + "func", [lambda x: x, to_datetime], ids=["numeric", "datetime"] ) @pytest.mark.parametrize("side", ["left", "right"]) def test_merge_on_nans(self, func, side): diff --git a/pandas/tests/reshape/merge/test_merge_ordered.py b/pandas/tests/reshape/merge/test_merge_ordered.py index abd61026b4e37..db71a732b44e3 100644 --- a/pandas/tests/reshape/merge/test_merge_ordered.py +++ b/pandas/tests/reshape/merge/test_merge_ordered.py @@ -135,54 +135,50 @@ def test_doc_example(self): "left, right, on, left_by, right_by, expected", [ ( - DataFrame({"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}), - DataFrame({"T": [2], "E": [1]}), + {"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}, + {"T": [2], "E": [1]}, ["T"], ["G", "H"], None, - DataFrame( - { - "G": ["g"] * 3, - "H": ["h"] * 3, - "T": [1, 2, 3], - "E": [np.nan, 1.0, np.nan], - } - ), + { + "G": ["g"] * 3, + "H": ["h"] * 3, + "T": [1, 2, 3], + "E": [np.nan, 1.0, np.nan], + }, ), ( - DataFrame({"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}), - DataFrame({"T": [2], "E": [1]}), + {"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}, + {"T": [2], "E": [1]}, "T", ["G", "H"], None, - DataFrame( - { - "G": ["g"] * 3, - "H": ["h"] * 3, - "T": [1, 2, 3], - "E": [np.nan, 1.0, np.nan], - } - ), + { + "G": ["g"] * 3, + "H": ["h"] * 3, + "T": [1, 2, 3], + "E": [np.nan, 1.0, np.nan], + }, ), ( - DataFrame({"T": [2], "E": [1]}), - DataFrame({"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}), + {"T": [2], "E": [1]}, + {"G": ["g", "g"], "H": ["h", "h"], "T": [1, 3]}, ["T"], None, ["G", "H"], - DataFrame( - { - "T": [1, 2, 3], - "E": [np.nan, 1.0, np.nan], - "G": ["g"] * 3, - "H": ["h"] * 3, - } - ), + { + "T": [1, 2, 3], + "E": [np.nan, 1.0, np.nan], + "G": ["g"] * 3, + "H": ["h"] * 3, + }, ), ], ) def test_list_type_by(self, left, right, on, left_by, right_by, expected): # GH 35269 + left = DataFrame(left) + right = DataFrame(right) result = merge_ordered( left=left, right=right, @@ -190,6 +186,7 @@ def test_list_type_by(self, left, right, on, left_by, right_by, expected): left_by=left_by, right_by=right_by, ) + expected = DataFrame(expected) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/reshape/test_from_dummies.py b/pandas/tests/reshape/test_from_dummies.py index f9a03222c8057..ba71bb24e8a16 100644 --- a/pandas/tests/reshape/test_from_dummies.py +++ b/pandas/tests/reshape/test_from_dummies.py @@ -298,32 +298,32 @@ def test_no_prefix_string_cats_contains_get_dummies_NaN_column(): [ pytest.param( "c", - DataFrame({"": ["a", "b", "c"]}), + {"": ["a", "b", "c"]}, id="default_category is a str", ), pytest.param( 1, - DataFrame({"": ["a", "b", 1]}), + {"": ["a", "b", 1]}, id="default_category is a int", ), pytest.param( 1.25, - DataFrame({"": ["a", "b", 1.25]}), + {"": ["a", "b", 1.25]}, id="default_category is a float", ), pytest.param( 0, - DataFrame({"": ["a", "b", 0]}), + {"": ["a", "b", 0]}, id="default_category is a 0", ), pytest.param( False, - DataFrame({"": ["a", "b", False]}), + {"": ["a", "b", False]}, id="default_category is a bool", ), pytest.param( (1, 2), - DataFrame({"": ["a", "b", (1, 2)]}), + {"": ["a", "b", (1, 2)]}, id="default_category is a tuple", ), ], @@ -333,6 +333,7 @@ def test_no_prefix_string_cats_default_category( ): dummies = DataFrame({"a": [1, 0, 0], "b": [0, 1, 0]}) result = from_dummies(dummies, default_category=default_category) + expected = DataFrame(expected) if using_infer_string: expected[""] = expected[""].astype("string[pyarrow_numpy]") tm.assert_frame_equal(result, expected) @@ -366,32 +367,32 @@ def test_with_prefix_contains_get_dummies_NaN_column(): [ pytest.param( "x", - DataFrame({"col1": ["a", "b", "x"], "col2": ["x", "a", "c"]}), + {"col1": ["a", "b", "x"], "col2": ["x", "a", "c"]}, id="default_category is a str", ), pytest.param( 0, - DataFrame({"col1": ["a", "b", 0], "col2": [0, "a", "c"]}), + {"col1": ["a", "b", 0], "col2": [0, "a", "c"]}, id="default_category is a 0", ), pytest.param( False, - DataFrame({"col1": ["a", "b", False], "col2": [False, "a", "c"]}), + {"col1": ["a", "b", False], "col2": [False, "a", "c"]}, id="default_category is a False", ), pytest.param( {"col2": 1, "col1": 2.5}, - DataFrame({"col1": ["a", "b", 2.5], "col2": [1, "a", "c"]}), + {"col1": ["a", "b", 2.5], "col2": [1, "a", "c"]}, id="default_category is a dict with int and float values", ), pytest.param( {"col2": None, "col1": False}, - DataFrame({"col1": ["a", "b", False], "col2": [None, "a", "c"]}), + {"col1": ["a", "b", False], "col2": [None, "a", "c"]}, id="default_category is a dict with bool and None values", ), pytest.param( {"col2": (1, 2), "col1": [1.25, False]}, - DataFrame({"col1": ["a", "b", [1.25, False]], "col2": [(1, 2), "a", "c"]}), + {"col1": ["a", "b", [1.25, False]], "col2": [(1, 2), "a", "c"]}, id="default_category is a dict with list and tuple values", ), ], @@ -402,6 +403,7 @@ def test_with_prefix_default_category( result = from_dummies( dummies_with_unassigned, sep="_", default_category=default_category ) + expected = DataFrame(expected) tm.assert_frame_equal(result, expected) diff --git a/pandas/tests/reshape/test_get_dummies.py b/pandas/tests/reshape/test_get_dummies.py index 31260e4dcb7d2..082d5f0ee81ab 100644 --- a/pandas/tests/reshape/test_get_dummies.py +++ b/pandas/tests/reshape/test_get_dummies.py @@ -453,19 +453,19 @@ def test_dataframe_dummies_with_categorical(self, df, sparse, dtype): [ ( {"data": DataFrame({"ä": ["a"]})}, - DataFrame({"ä_a": [True]}), + "ä_a", ), ( {"data": DataFrame({"x": ["ä"]})}, - DataFrame({"x_ä": [True]}), + "x_ä", ), ( {"data": DataFrame({"x": ["a"]}), "prefix": "ä"}, - DataFrame({"ä_a": [True]}), + "ä_a", ), ( {"data": DataFrame({"x": ["a"]}), "prefix_sep": "ä"}, - DataFrame({"xäa": [True]}), + "xäa", ), ], ) @@ -473,6 +473,7 @@ def test_dataframe_dummies_unicode(self, get_dummies_kwargs, expected): # GH22084 get_dummies incorrectly encodes unicode characters # in dataframe column names result = get_dummies(**get_dummies_kwargs) + expected = DataFrame({expected: [True]}) tm.assert_frame_equal(result, expected) def test_get_dummies_basic_drop_first(self, sparse): diff --git a/pandas/tests/reshape/test_melt.py b/pandas/tests/reshape/test_melt.py index ff9f927597956..63367aee77f83 100644 --- a/pandas/tests/reshape/test_melt.py +++ b/pandas/tests/reshape/test_melt.py @@ -133,25 +133,21 @@ def test_vars_work_with_multiindex(self, df1): ["A"], ["B"], 0, - DataFrame( - { - "A": {0: 1.067683, 1: -1.321405, 2: -0.807333}, - "CAP": {0: "B", 1: "B", 2: "B"}, - "value": {0: -1.110463, 1: 0.368915, 2: 0.08298}, - } - ), + { + "A": {0: 1.067683, 1: -1.321405, 2: -0.807333}, + "CAP": {0: "B", 1: "B", 2: "B"}, + "value": {0: -1.110463, 1: 0.368915, 2: 0.08298}, + }, ), ( ["a"], ["b"], 1, - DataFrame( - { - "a": {0: 1.067683, 1: -1.321405, 2: -0.807333}, - "low": {0: "b", 1: "b", 2: "b"}, - "value": {0: -1.110463, 1: 0.368915, 2: 0.08298}, - } - ), + { + "a": {0: 1.067683, 1: -1.321405, 2: -0.807333}, + "low": {0: "b", 1: "b", 2: "b"}, + "value": {0: -1.110463, 1: 0.368915, 2: 0.08298}, + }, ), ], ) @@ -159,6 +155,7 @@ def test_single_vars_work_with_multiindex( self, id_vars, value_vars, col_level, expected, df1 ): result = df1.melt(id_vars, value_vars, col_level=col_level) + expected = DataFrame(expected) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( @@ -287,13 +284,14 @@ def test_multiindex(self, df1): @pytest.mark.parametrize( "col", [ - pd.Series(date_range("2010", periods=5, tz="US/Pacific")), - pd.Series(["a", "b", "c", "a", "d"], dtype="category"), - pd.Series([0, 1, 0, 0, 0]), + date_range("2010", periods=5, tz="US/Pacific"), + pd.Categorical(["a", "b", "c", "a", "d"]), + [0, 1, 0, 0, 0], ], ) def test_pandas_dtypes(self, col): # GH 15785 + col = pd.Series(col) df = DataFrame( {"klass": range(5), "col": col, "attr1": [1, 0, 0, 0, 0], "attr2": col} ) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index af156a1da87f2..bbce33b57dfc4 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -744,18 +744,11 @@ def test_pivot_periods_with_margins(self): result = df.pivot_table(index="a", columns="b", values="x", margins=True) tm.assert_frame_equal(expected, result) - @pytest.mark.parametrize( - "values", - [ - ["baz", "zoo"], - np.array(["baz", "zoo"]), - Series(["baz", "zoo"]), - Index(["baz", "zoo"]), - ], - ) + @pytest.mark.parametrize("values", [list, np.array, Series, Index]) @pytest.mark.parametrize("method", [True, False]) - def test_pivot_with_list_like_values(self, values, method): + def test_pivot_with_list_like_values(self, box, method): # issue #17160 + values = box(["baz", "zoo"]) df = DataFrame( { "foo": ["one", "one", "one", "two", "two", "two"], diff --git a/pandas/tests/reshape/test_qcut.py b/pandas/tests/reshape/test_qcut.py index b5b19eef1106f..53af673e0f7b0 100644 --- a/pandas/tests/reshape/test_qcut.py +++ b/pandas/tests/reshape/test_qcut.py @@ -154,14 +154,15 @@ def test_qcut_wrong_length_labels(labels): @pytest.mark.parametrize( "labels, expected", [ - (["a", "b", "c"], Categorical(["a", "b", "c"], ordered=True)), - (list(range(3)), Categorical([0, 1, 2], ordered=True)), + (["a", "b", "c"], ["a", "b", "c"]), + (list(range(3)), [0, 1, 2]), ], ) def test_qcut_list_like_labels(labels, expected): # GH 13318 values = range(3) result = qcut(values, 3, labels=labels) + expected = Categorical(expected, ordered=True) tm.assert_categorical_equal(result, expected) @@ -209,13 +210,14 @@ def test_single_quantile(data, start, end, length, labels): @pytest.mark.parametrize( "ser", [ - Series(DatetimeIndex(["20180101", NaT, "20180103"])), - Series(TimedeltaIndex(["0 days", NaT, "2 days"])), + DatetimeIndex(["20180101", NaT, "20180103"]), + TimedeltaIndex(["0 days", NaT, "2 days"]), ], ids=lambda x: str(x.dtype), ) def test_qcut_nat(ser, unit): # see gh-19768 + ser = Series(ser) ser = ser.dt.as_unit(unit) td = Timedelta(1, unit=unit).as_unit(unit) diff --git a/pandas/tests/scalar/period/test_arithmetic.py b/pandas/tests/scalar/period/test_arithmetic.py index 5dc0858de466c..97e486f9af060 100644 --- a/pandas/tests/scalar/period/test_arithmetic.py +++ b/pandas/tests/scalar/period/test_arithmetic.py @@ -476,10 +476,11 @@ def test_period_comparison_nat(self): assert not left >= right @pytest.mark.parametrize( - "zerodim_arr, expected", - ((np.array(0), False), (np.array(Period("2000-01", "M")), True)), + "scalar, expected", + ((0, False), (Period("2000-01", "M"), True)), ) - def test_period_comparison_numpy_zerodim_arr(self, zerodim_arr, expected): + def test_period_comparison_numpy_zerodim_arr(self, scalar, expected): + zerodim_arr = np.array(scalar) per = Period("2000-01", "M") assert (per == zerodim_arr) is expected diff --git a/pandas/tests/scalar/timedelta/methods/test_round.py b/pandas/tests/scalar/timedelta/methods/test_round.py index 676b44a4d54f4..082c36999e06f 100644 --- a/pandas/tests/scalar/timedelta/methods/test_round.py +++ b/pandas/tests/scalar/timedelta/methods/test_round.py @@ -19,29 +19,31 @@ class TestTimedeltaRound: # This first case has s1, s2 being the same as t1,t2 below ( "ns", - Timedelta("1 days 02:34:56.789123456"), - Timedelta("-1 days 02:34:56.789123456"), + "1 days 02:34:56.789123456", + "-1 days 02:34:56.789123456", ), ( "us", - Timedelta("1 days 02:34:56.789123000"), - Timedelta("-1 days 02:34:56.789123000"), + "1 days 02:34:56.789123000", + "-1 days 02:34:56.789123000", ), ( "ms", - Timedelta("1 days 02:34:56.789000000"), - Timedelta("-1 days 02:34:56.789000000"), + "1 days 02:34:56.789000000", + "-1 days 02:34:56.789000000", ), - ("s", Timedelta("1 days 02:34:57"), Timedelta("-1 days 02:34:57")), - ("2s", Timedelta("1 days 02:34:56"), Timedelta("-1 days 02:34:56")), - ("5s", Timedelta("1 days 02:34:55"), Timedelta("-1 days 02:34:55")), - ("min", Timedelta("1 days 02:35:00"), Timedelta("-1 days 02:35:00")), - ("12min", Timedelta("1 days 02:36:00"), Timedelta("-1 days 02:36:00")), - ("h", Timedelta("1 days 03:00:00"), Timedelta("-1 days 03:00:00")), - ("d", Timedelta("1 days"), Timedelta("-1 days")), + ("s", "1 days 02:34:57", "-1 days 02:34:57"), + ("2s", "1 days 02:34:56", "-1 days 02:34:56"), + ("5s", "1 days 02:34:55", "-1 days 02:34:55"), + ("min", "1 days 02:35:00", "-1 days 02:35:00"), + ("12min", "1 days 02:36:00", "-1 days 02:36:00"), + ("h", "1 days 03:00:00", "-1 days 03:00:00"), + ("d", "1 days", "-1 days"), ], ) def test_round(self, freq, s1, s2): + s1 = Timedelta(s1) + s2 = Timedelta(s2) t1 = Timedelta("1 days 02:34:56.789123456") t2 = Timedelta("-1 days 02:34:56.789123456") diff --git a/pandas/tests/scalar/timedelta/test_arithmetic.py b/pandas/tests/scalar/timedelta/test_arithmetic.py index d2fa0f722ca6f..f3edaffdb315d 100644 --- a/pandas/tests/scalar/timedelta/test_arithmetic.py +++ b/pandas/tests/scalar/timedelta/test_arithmetic.py @@ -955,11 +955,12 @@ def test_rdivmod_invalid(self): @pytest.mark.parametrize( "arr", [ - np.array([Timestamp("20130101 9:01"), Timestamp("20121230 9:02")]), - np.array([Timestamp("2021-11-09 09:54:00"), Timedelta("1D")]), + [Timestamp("20130101 9:01"), Timestamp("20121230 9:02")], + [Timestamp("2021-11-09 09:54:00"), Timedelta("1D")], ], ) def test_td_op_timedelta_timedeltalike_array(self, op, arr): + arr = np.array(arr) msg = "unsupported operand type|cannot use operands with types" with pytest.raises(TypeError, match=msg): op(arr, Timedelta("1D")) diff --git a/pandas/tests/scalar/timedelta/test_constructors.py b/pandas/tests/scalar/timedelta/test_constructors.py index 4663f8cb71961..e680ca737b546 100644 --- a/pandas/tests/scalar/timedelta/test_constructors.py +++ b/pandas/tests/scalar/timedelta/test_constructors.py @@ -631,17 +631,16 @@ def test_timedelta_pass_td_and_kwargs_raises(): @pytest.mark.parametrize( - "constructor, value, unit, expectation", + "constructor, value, unit", [ - (Timedelta, "10s", "ms", (ValueError, "unit must not be specified")), - (to_timedelta, "10s", "ms", (ValueError, "unit must not be specified")), - (to_timedelta, ["1", 2, 3], "s", (ValueError, "unit must not be specified")), + (Timedelta, "10s", "ms"), + (to_timedelta, "10s", "ms"), + (to_timedelta, ["1", 2, 3], "s"), ], ) -def test_string_with_unit(constructor, value, unit, expectation): - exp, match = expectation - with pytest.raises(exp, match=match): - _ = constructor(value, unit=unit) +def test_string_with_unit(constructor, value, unit): + with pytest.raises(ValueError, match="unit must not be specified"): + constructor(value, unit=unit) @pytest.mark.parametrize( diff --git a/pandas/tests/scalar/timestamp/methods/test_round.py b/pandas/tests/scalar/timestamp/methods/test_round.py index 59c0fe8bbebfb..2fb0e1a8d3397 100644 --- a/pandas/tests/scalar/timestamp/methods/test_round.py +++ b/pandas/tests/scalar/timestamp/methods/test_round.py @@ -162,10 +162,6 @@ def test_floor(self, unit): assert result._creso == dt._creso @pytest.mark.parametrize("method", ["ceil", "round", "floor"]) - @pytest.mark.parametrize( - "unit", - ["ns", "us", "ms", "s"], - ) def test_round_dst_border_ambiguous(self, method, unit): # GH 18946 round near "fall back" DST ts = Timestamp("2017-10-29 00:00:00", tz="UTC").tz_convert("Europe/Madrid") @@ -197,10 +193,6 @@ def test_round_dst_border_ambiguous(self, method, unit): ["floor", "2018-03-11 03:01:00-0500", "2h"], ], ) - @pytest.mark.parametrize( - "unit", - ["ns", "us", "ms", "s"], - ) def test_round_dst_border_nonexistent(self, method, ts_str, freq, unit): # GH 23324 round near "spring forward" DST ts = Timestamp(ts_str, tz="America/Chicago").as_unit(unit) diff --git a/pandas/tests/scalar/timestamp/test_constructors.py b/pandas/tests/scalar/timestamp/test_constructors.py index 3975f3c46aaa1..94c56f4901d20 100644 --- a/pandas/tests/scalar/timestamp/test_constructors.py +++ b/pandas/tests/scalar/timestamp/test_constructors.py @@ -190,18 +190,17 @@ def test_timestamp_constructor_infer_fold_from_value(self, tz, ts_input, fold_ou @pytest.mark.parametrize("tz", ["dateutil/Europe/London"]) @pytest.mark.parametrize( - "ts_input,fold,value_out", + "fold,value_out", [ - (datetime(2019, 10, 27, 1, 30, 0, 0), 0, 1572136200000000), - (datetime(2019, 10, 27, 1, 30, 0, 0), 1, 1572139800000000), + (0, 1572136200000000), + (1, 1572139800000000), ], ) - def test_timestamp_constructor_adjust_value_for_fold( - self, tz, ts_input, fold, value_out - ): + def test_timestamp_constructor_adjust_value_for_fold(self, tz, fold, value_out): # Test for GH#25057 # Check that we adjust value for fold correctly # based on timestamps since utc + ts_input = datetime(2019, 10, 27, 1, 30) ts = Timestamp(ts_input, tz=tz, fold=fold) result = ts._value expected = value_out diff --git a/pandas/tests/scalar/timestamp/test_timestamp.py b/pandas/tests/scalar/timestamp/test_timestamp.py index cacbe0a1d6095..86ab405ab99e0 100644 --- a/pandas/tests/scalar/timestamp/test_timestamp.py +++ b/pandas/tests/scalar/timestamp/test_timestamp.py @@ -118,18 +118,16 @@ def test_is_end(self, end, tz): assert getattr(ts, end) # GH 12806 - @pytest.mark.parametrize( - "data", - [Timestamp("2017-08-28 23:00:00"), Timestamp("2017-08-28 23:00:00", tz="EST")], - ) + @pytest.mark.parametrize("tz", [None, "EST"]) # error: Unsupported operand types for + ("List[None]" and "List[str]") @pytest.mark.parametrize( "time_locale", [None] + tm.get_locales(), # type: ignore[operator] ) - def test_names(self, data, time_locale): + def test_names(self, tz, time_locale): # GH 17354 # Test .day_name(), .month_name + data = Timestamp("2017-08-28 23:00:00", tz=tz) if time_locale is None: expected_day = "Monday" expected_month = "August" diff --git a/pandas/tests/series/accessors/test_dt_accessor.py b/pandas/tests/series/accessors/test_dt_accessor.py index 911f5d7b28e3f..2365ff62b1680 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -697,15 +697,16 @@ def test_dt_accessor_api(self): assert isinstance(ser.dt, DatetimeProperties) @pytest.mark.parametrize( - "ser", + "data", [ - Series(np.arange(5)), - Series(list("abcde")), - Series(np.random.default_rng(2).standard_normal(5)), + np.arange(5), + list("abcde"), + np.random.default_rng(2).standard_normal(5), ], ) - def test_dt_accessor_invalid(self, ser): + def test_dt_accessor_invalid(self, data): # GH#9322 check that series with incorrect dtypes don't have attr + ser = Series(data) with pytest.raises(AttributeError, match="only use .dt accessor"): ser.dt assert not hasattr(ser, "dt") diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index 596a225c288b8..c5b79b88084af 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -563,12 +563,13 @@ def test_getitem_generator(string_series): @pytest.mark.parametrize( "series", [ - Series([0, 1]), - Series(date_range("2012-01-01", periods=2)), - Series(date_range("2012-01-01", periods=2, tz="CET")), + [0, 1], + date_range("2012-01-01", periods=2), + date_range("2012-01-01", periods=2, tz="CET"), ], ) -def test_getitem_ndim_deprecated(series): +def test_getitem_ndim_deprecated(data): + series = Series(data) with pytest.raises(ValueError, match="Multi-dimensional indexing"): series[:, None] diff --git a/pandas/tests/series/indexing/test_indexing.py b/pandas/tests/series/indexing/test_indexing.py index c52e47a812183..449b73ecf32fe 100644 --- a/pandas/tests/series/indexing/test_indexing.py +++ b/pandas/tests/series/indexing/test_indexing.py @@ -117,19 +117,21 @@ def test_getitem_setitem_ellipsis(using_copy_on_write, warn_copy_on_write): "result_1, duplicate_item, expected_1", [ [ - Series({1: 12, 2: [1, 2, 2, 3]}), - Series({1: 313}), + {1: 12, 2: [1, 2, 2, 3]}, + {1: 313}, Series({1: 12}, dtype=object), ], [ - Series({1: [1, 2, 3], 2: [1, 2, 2, 3]}), - Series({1: [1, 2, 3]}), + {1: [1, 2, 3], 2: [1, 2, 2, 3]}, + {1: [1, 2, 3]}, Series({1: [1, 2, 3]}), ], ], ) def test_getitem_with_duplicates_indices(result_1, duplicate_item, expected_1): # GH 17610 + result_1 = Series(result_1) + duplicate_item = Series(duplicate_item) result = result_1._append(duplicate_item) expected = expected_1._append(duplicate_item) tm.assert_series_equal(result[1], expected) diff --git a/pandas/tests/series/indexing/test_setitem.py b/pandas/tests/series/indexing/test_setitem.py index 23137f0975fb1..85ffb0f8fe647 100644 --- a/pandas/tests/series/indexing/test_setitem.py +++ b/pandas/tests/series/indexing/test_setitem.py @@ -1799,11 +1799,7 @@ def test_setitem_with_bool_indexer(): @pytest.mark.parametrize( "item", [2.0, np.nan, np.finfo(float).max, np.finfo(float).min] ) -# Test numpy arrays, lists and tuples as the input to be -# broadcast -@pytest.mark.parametrize( - "box", [lambda x: np.array([x]), lambda x: [x], lambda x: (x,)] -) +@pytest.mark.parametrize("box", [np.array, list, tuple]) def test_setitem_bool_indexer_dont_broadcast_length1_values(size, mask, item, box): # GH#44265 # see also tests.series.indexing.test_where.test_broadcast @@ -1821,11 +1817,11 @@ def test_setitem_bool_indexer_dont_broadcast_length1_values(size, mask, item, bo ) with pytest.raises(ValueError, match=msg): # GH#44265 - ser[selection] = box(item) + ser[selection] = box([item]) else: # In this corner case setting is equivalent to setting with the unboxed # item - ser[selection] = box(item) + ser[selection] = box([item]) expected = Series(np.arange(size, dtype=float)) expected[selection] = item diff --git a/pandas/tests/series/indexing/test_where.py b/pandas/tests/series/indexing/test_where.py index c978481ca9988..e1139ea75f48b 100644 --- a/pandas/tests/series/indexing/test_where.py +++ b/pandas/tests/series/indexing/test_where.py @@ -297,11 +297,7 @@ def test_where_setitem_invalid(): @pytest.mark.parametrize( "item", [2.0, np.nan, np.finfo(float).max, np.finfo(float).min] ) -# Test numpy arrays, lists and tuples as the input to be -# broadcast -@pytest.mark.parametrize( - "box", [lambda x: np.array([x]), lambda x: [x], lambda x: (x,)] -) +@pytest.mark.parametrize("box", [np.array, list, tuple]) def test_broadcast(size, mask, item, box): # GH#8801, GH#4195 selection = np.resize(mask, size) @@ -320,11 +316,11 @@ def test_broadcast(size, mask, item, box): tm.assert_series_equal(s, expected) s = Series(data) - result = s.where(~selection, box(item)) + result = s.where(~selection, box([item])) tm.assert_series_equal(result, expected) s = Series(data) - result = s.mask(selection, box(item)) + result = s.mask(selection, box([item])) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/series/methods/test_astype.py b/pandas/tests/series/methods/test_astype.py index 4d2cd2ba963fd..2588f195aab7e 100644 --- a/pandas/tests/series/methods/test_astype.py +++ b/pandas/tests/series/methods/test_astype.py @@ -164,14 +164,15 @@ def test_astype_empty_constructor_equality(self, dtype): @pytest.mark.parametrize("dtype", [str, np.str_]) @pytest.mark.parametrize( - "series", + "data", [ - Series([string.digits * 10, rand_str(63), rand_str(64), rand_str(1000)]), - Series([string.digits * 10, rand_str(63), rand_str(64), np.nan, 1.0]), + [string.digits * 10, rand_str(63), rand_str(64), rand_str(1000)], + [string.digits * 10, rand_str(63), rand_str(64), np.nan, 1.0], ], ) - def test_astype_str_map(self, dtype, series, using_infer_string): + def test_astype_str_map(self, dtype, data, using_infer_string): # see GH#4405 + series = Series(data) result = series.astype(dtype) expected = series.map(str) if using_infer_string: @@ -459,13 +460,9 @@ def test_astype_nan_to_bool(self): expected = Series(True, dtype="bool") tm.assert_series_equal(result, expected) - @pytest.mark.parametrize( - "dtype", - tm.ALL_INT_EA_DTYPES + tm.FLOAT_EA_DTYPES, - ) - def test_astype_ea_to_datetimetzdtype(self, dtype): + def test_astype_ea_to_datetimetzdtype(self, any_numeric_ea_dtype): # GH37553 - ser = Series([4, 0, 9], dtype=dtype) + ser = Series([4, 0, 9], dtype=any_numeric_ea_dtype) result = ser.astype(DatetimeTZDtype(tz="US/Pacific")) expected = Series( diff --git a/pandas/tests/series/methods/test_diff.py b/pandas/tests/series/methods/test_diff.py index 18de81a927c3a..81949a64aafae 100644 --- a/pandas/tests/series/methods/test_diff.py +++ b/pandas/tests/series/methods/test_diff.py @@ -1,5 +1,4 @@ import numpy as np -import pytest from pandas import ( Series, @@ -69,13 +68,11 @@ def test_diff_dt64tz(self): expected = Series(TimedeltaIndex(["NaT"] + ["1 days"] * 4), name="foo") tm.assert_series_equal(result, expected) - @pytest.mark.parametrize( - "input,output,diff", - [([False, True, True, False, False], [np.nan, True, False, True, False], 1)], - ) - def test_diff_bool(self, input, output, diff): + def test_diff_bool(self): # boolean series (test for fixing #17294) - ser = Series(input) + data = [False, True, True, False, False] + output = [np.nan, True, False, True, False] + ser = Series(data) result = ser.diff() expected = Series(output) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/series/methods/test_drop.py b/pandas/tests/series/methods/test_drop.py index 5d9a469915cfb..d2a5a3324e886 100644 --- a/pandas/tests/series/methods/test_drop.py +++ b/pandas/tests/series/methods/test_drop.py @@ -31,17 +31,17 @@ def test_drop_unique_and_non_unique_index( @pytest.mark.parametrize( - "data, index, drop_labels, axis, error_type, error_desc", + "drop_labels, axis, error_type, error_desc", [ # single string/tuple-like - (range(3), list("abc"), "bc", 0, KeyError, "not found in axis"), + ("bc", 0, KeyError, "not found in axis"), # bad axis - (range(3), list("abc"), ("a",), 0, KeyError, "not found in axis"), - (range(3), list("abc"), "one", "columns", ValueError, "No axis named columns"), + (("a",), 0, KeyError, "not found in axis"), + ("one", "columns", ValueError, "No axis named columns"), ], ) -def test_drop_exception_raised(data, index, drop_labels, axis, error_type, error_desc): - ser = Series(data, index=index) +def test_drop_exception_raised(drop_labels, axis, error_type, error_desc): + ser = Series(range(3), index=list("abc")) with pytest.raises(error_type, match=error_desc): ser.drop(drop_labels, axis=axis) diff --git a/pandas/tests/series/methods/test_drop_duplicates.py b/pandas/tests/series/methods/test_drop_duplicates.py index 10b2e98586365..31ef8ff896bcc 100644 --- a/pandas/tests/series/methods/test_drop_duplicates.py +++ b/pandas/tests/series/methods/test_drop_duplicates.py @@ -34,14 +34,14 @@ def test_drop_duplicates(any_numpy_dtype, keep, expected): @pytest.mark.parametrize( "keep, expected", [ - ("first", Series([False, False, True, True])), - ("last", Series([True, True, False, False])), - (False, Series([True, True, True, True])), + ("first", [False, False, True, True]), + ("last", [True, True, False, False]), + (False, [True, True, True, True]), ], ) def test_drop_duplicates_bool(keep, expected): tc = Series([True, False, True, False]) - + expected = Series(expected) tm.assert_series_equal(tc.duplicated(keep=keep), expected) tm.assert_series_equal(tc.drop_duplicates(keep=keep), tc[~expected]) sc = tc.copy() diff --git a/pandas/tests/series/methods/test_duplicated.py b/pandas/tests/series/methods/test_duplicated.py index e177b5275d855..f5a387a7f302e 100644 --- a/pandas/tests/series/methods/test_duplicated.py +++ b/pandas/tests/series/methods/test_duplicated.py @@ -12,30 +12,32 @@ @pytest.mark.parametrize( "keep, expected", [ - ("first", Series([False, False, True, False, True], name="name")), - ("last", Series([True, True, False, False, False], name="name")), - (False, Series([True, True, True, False, True], name="name")), + ("first", [False, False, True, False, True]), + ("last", [True, True, False, False, False]), + (False, [True, True, True, False, True]), ], ) def test_duplicated_keep(keep, expected): ser = Series(["a", "b", "b", "c", "a"], name="name") result = ser.duplicated(keep=keep) + expected = Series(expected, name="name") tm.assert_series_equal(result, expected) @pytest.mark.parametrize( "keep, expected", [ - ("first", Series([False, False, True, False, True])), - ("last", Series([True, True, False, False, False])), - (False, Series([True, True, True, False, True])), + ("first", [False, False, True, False, True]), + ("last", [True, True, False, False, False]), + (False, [True, True, True, False, True]), ], ) def test_duplicated_nan_none(keep, expected): ser = Series([np.nan, 3, 3, None, np.nan], dtype=object) result = ser.duplicated(keep=keep) + expected = Series(expected) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/series/methods/test_explode.py b/pandas/tests/series/methods/test_explode.py index 5a0188585ef30..15d615fc35081 100644 --- a/pandas/tests/series/methods/test_explode.py +++ b/pandas/tests/series/methods/test_explode.py @@ -74,11 +74,12 @@ def test_invert_array(): @pytest.mark.parametrize( - "s", [pd.Series([1, 2, 3]), pd.Series(pd.date_range("2019", periods=3, tz="UTC"))] + "data", [[1, 2, 3], pd.date_range("2019", periods=3, tz="UTC")] ) -def test_non_object_dtype(s): - result = s.explode() - tm.assert_series_equal(result, s) +def test_non_object_dtype(data): + ser = pd.Series(data) + result = ser.explode() + tm.assert_series_equal(result, ser) def test_typical_usecase(): diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index f38e4a622cffa..308d25b88b3eb 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -786,15 +786,12 @@ def test_fillna_categorical(self, fill_value, expected_output): @pytest.mark.parametrize( "fill_value, expected_output", [ - (Series(["a", "b", "c", "d", "e"]), ["a", "b", "b", "d", "e"]), - (Series(["b", "d", "a", "d", "a"]), ["a", "d", "b", "d", "a"]), + (["a", "b", "c", "d", "e"], ["a", "b", "b", "d", "e"]), + (["b", "d", "a", "d", "a"], ["a", "d", "b", "d", "a"]), ( - Series( - Categorical( - ["b", "d", "a", "d", "a"], categories=["b", "c", "d", "e", "a"] - ) - ), - ["a", "d", "b", "d", "a"], + Categorical( + ["b", "d", "a", "d", "a"], categories=["b", "c", "d", "e", "a"] + )["a", "d", "b", "d", "a"], ), ], ) @@ -803,6 +800,7 @@ def test_fillna_categorical_with_new_categories(self, fill_value, expected_outpu data = ["a", np.nan, "b", np.nan, np.nan] ser = Series(Categorical(data, categories=["a", "b", "c", "d", "e"])) exp = Series(Categorical(expected_output, categories=["a", "b", "c", "d", "e"])) + fill_value = Series(fill_value) result = ser.fillna(fill_value) tm.assert_series_equal(result, exp) diff --git a/pandas/tests/series/methods/test_info.py b/pandas/tests/series/methods/test_info.py index 29dd704f6efa9..bd1bc1781958c 100644 --- a/pandas/tests/series/methods/test_info.py +++ b/pandas/tests/series/methods/test_info.py @@ -141,19 +141,17 @@ def test_info_memory_usage_deep_pypy(): @pytest.mark.parametrize( - "series, plus", + "index, plus", [ - (Series(1, index=[1, 2, 3]), False), - (Series(1, index=list("ABC")), True), - (Series(1, index=MultiIndex.from_product([range(3), range(3)])), False), - ( - Series(1, index=MultiIndex.from_product([range(3), ["foo", "bar"]])), - True, - ), + ([1, 2, 3], False), + (list("ABC"), True), + (MultiIndex.from_product([range(3), range(3)]), False), + (MultiIndex.from_product([range(3), ["foo", "bar"]]), True), ], ) -def test_info_memory_usage_qualified(series, plus): +def test_info_memory_usage_qualified(index, plus): buf = StringIO() + series = Series(1, index=index) series.info(buf=buf) if plus: assert "+" in buf.getvalue() diff --git a/pandas/tests/series/methods/test_isin.py b/pandas/tests/series/methods/test_isin.py index f94f67b8cc40a..08b34669f1aa4 100644 --- a/pandas/tests/series/methods/test_isin.py +++ b/pandas/tests/series/methods/test_isin.py @@ -211,18 +211,11 @@ def test_isin_large_series_mixed_dtypes_and_nan(monkeypatch): tm.assert_series_equal(result, expected) -@pytest.mark.parametrize( - "array,expected", - [ - ( - [0, 1j, 1j, 1, 1 + 1j, 1 + 2j, 1 + 1j], - Series([False, True, True, False, True, True, True], dtype=bool), - ) - ], -) def test_isin_complex_numbers(array, expected): # GH 17927 + array = [0, 1j, 1j, 1, 1 + 1j, 1 + 2j, 1 + 1j] result = Series(array).isin([1j, 1 + 1j, 1 + 2j]) + expected = Series([False, True, True, False, True, True, True], dtype=bool) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index 4330153c186ca..da5d1fcbc2ad9 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -395,13 +395,13 @@ def test_replace_mixed_types_with_string(self): @pytest.mark.parametrize( "categorical, numeric", [ - (pd.Categorical(["A"], categories=["A", "B"]), [1]), - (pd.Categorical(["A", "B"], categories=["A", "B"]), [1, 2]), + (["A"], [1]), + (["A", "B"], [1, 2]), ], ) - def test_replace_categorical(self, categorical, numeric): + def test_replace_categorical(self, values, numeric): # GH 24971, GH#23305 - ser = pd.Series(categorical) + ser = pd.Series(pd.Categorical(values, categories=["A", "B"])) msg = "Downcasting behavior in `replace`" msg = "with CategoricalDtype is deprecated" with tm.assert_produces_warning(FutureWarning, match=msg): diff --git a/pandas/tests/series/methods/test_update.py b/pandas/tests/series/methods/test_update.py index 3f18ae6c13880..191aa36ad5d41 100644 --- a/pandas/tests/series/methods/test_update.py +++ b/pandas/tests/series/methods/test_update.py @@ -76,21 +76,23 @@ def test_update_dtypes(self, other, dtype, expected, warn): tm.assert_series_equal(ser, expected) @pytest.mark.parametrize( - "series, other, expected", + "values, other, expected", [ # update by key ( - Series({"a": 1, "b": 2, "c": 3, "d": 4}), + {"a": 1, "b": 2, "c": 3, "d": 4}, {"b": 5, "c": np.nan}, - Series({"a": 1, "b": 5, "c": 3, "d": 4}), + {"a": 1, "b": 5, "c": 3, "d": 4}, ), # update by position - (Series([1, 2, 3, 4]), [np.nan, 5, 1], Series([1, 5, 1, 4])), + ([1, 2, 3, 4], [np.nan, 5, 1], [1, 5, 1, 4]), ], ) - def test_update_from_non_series(self, series, other, expected): + def test_update_from_non_series(self, values, other, expected): # GH 33215 + series = Series(values) series.update(other) + expected = Series(expected) tm.assert_series_equal(series, expected) @pytest.mark.parametrize( diff --git a/pandas/tests/series/methods/test_value_counts.py b/pandas/tests/series/methods/test_value_counts.py index 859010d9c79c6..f79ebf99f09d6 100644 --- a/pandas/tests/series/methods/test_value_counts.py +++ b/pandas/tests/series/methods/test_value_counts.py @@ -225,30 +225,12 @@ def test_value_counts_bool_with_nan(self, ser, dropna, exp): out = ser.value_counts(dropna=dropna) tm.assert_series_equal(out, exp) - @pytest.mark.parametrize( - "input_array,expected", - [ - ( - [1 + 1j, 1 + 1j, 1, 3j, 3j, 3j], - Series( - [3, 2, 1], - index=Index([3j, 1 + 1j, 1], dtype=np.complex128), - name="count", - ), - ), - ( - np.array([1 + 1j, 1 + 1j, 1, 3j, 3j, 3j], dtype=np.complex64), - Series( - [3, 2, 1], - index=Index([3j, 1 + 1j, 1], dtype=np.complex64), - name="count", - ), - ), - ], - ) - def test_value_counts_complex_numbers(self, input_array, expected): + @pytest.mark.parametrize("dtype", [np.complex128, np.complex64]) + def test_value_counts_complex_numbers(self, dtype): # GH 17927 + input_array = np.array([1 + 1j, 1 + 1j, 1, 3j, 3j, 3j], dtype=dtype) result = Series(input_array).value_counts() + expected = Series([3, 2, 1], dtype=dtype, name="count") tm.assert_series_equal(result, expected) def test_value_counts_masked(self): diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index b40e2e99dae2e..5d18bfa793aa1 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -674,22 +674,12 @@ def test_ne(self): tm.assert_numpy_array_equal(ts.index != 5, expected) tm.assert_numpy_array_equal(~(ts.index == 5), expected) - @pytest.mark.parametrize( - "left, right", - [ - ( - Series([1, 2, 3], index=list("ABC"), name="x"), - Series([2, 2, 2], index=list("ABD"), name="x"), - ), - ( - Series([1, 2, 3], index=list("ABC"), name="x"), - Series([2, 2, 2, 2], index=list("ABCD"), name="x"), - ), - ], - ) - def test_comp_ops_df_compat(self, left, right, frame_or_series): + @pytest.mark.parametrize("right_data", [[2, 2, 2], [2, 2, 2, 2]]) + def test_comp_ops_df_compat(self, right_data, frame_or_series): # GH 1134 # GH 50083 to clarify that index and columns must be identically labeled + left = Series([1, 2, 3], index=list("ABC"), name="x") + right = Series(right_data, index=list("ABD"), name="x") if frame_or_series is not Series: msg = ( rf"Can only compare identically-labeled \(both index and columns\) " diff --git a/pandas/tests/series/test_constructors.py b/pandas/tests/series/test_constructors.py index b802e92e4fcca..55ca1f98f6d6c 100644 --- a/pandas/tests/series/test_constructors.py +++ b/pandas/tests/series/test_constructors.py @@ -335,11 +335,11 @@ def test_constructor_index_dtype(self, dtype): @pytest.mark.parametrize( "input_vals", [ - ([1, 2]), - (["1", "2"]), - (list(date_range("1/1/2011", periods=2, freq="h"))), - (list(date_range("1/1/2011", periods=2, freq="h", tz="US/Eastern"))), - ([Interval(left=0, right=5)]), + [1, 2], + ["1", "2"], + list(date_range("1/1/2011", periods=2, freq="h")), + list(date_range("1/1/2011", periods=2, freq="h", tz="US/Eastern")), + [Interval(left=0, right=5)], ], ) def test_constructor_list_str(self, input_vals, string_dtype): @@ -1806,15 +1806,10 @@ def test_constructor_datetimelike_scalar_to_string_dtype( expected = Series(["M", "M", "M"], index=[1, 2, 3], dtype=nullable_string_dtype) tm.assert_series_equal(result, expected) - @pytest.mark.parametrize( - "values", - [ - [np.datetime64("2012-01-01"), np.datetime64("2013-01-01")], - ["2012-01-01", "2013-01-01"], - ], - ) - def test_constructor_sparse_datetime64(self, values): + @pytest.mark.parametrize("box", [lambda x: x, np.datetime64]) + def test_constructor_sparse_datetime64(self, box): # https://github.com/pandas-dev/pandas/issues/35762 + values = [box("2012-01-01"), box("2013-01-01")] dtype = pd.SparseDtype("datetime64[ns]") result = Series(values, dtype=dtype) arr = pd.arrays.SparseArray(values, dtype=dtype) diff --git a/pandas/tests/series/test_cumulative.py b/pandas/tests/series/test_cumulative.py index e6f7b2a5e69e0..68d7fd8b90df2 100644 --- a/pandas/tests/series/test_cumulative.py +++ b/pandas/tests/series/test_cumulative.py @@ -94,8 +94,8 @@ def test_cummin_cummax_datetimelike(self, ts, method, skipna, exp_tdi): @pytest.mark.parametrize( "func, exp", [ - ("cummin", pd.Period("2012-1-1", freq="D")), - ("cummax", pd.Period("2012-1-2", freq="D")), + ("cummin", "2012-1-1"), + ("cummax", "2012-1-2"), ], ) def test_cummin_cummax_period(self, func, exp): @@ -108,6 +108,7 @@ def test_cummin_cummax_period(self, func, exp): tm.assert_series_equal(result, expected) result = getattr(ser, func)(skipna=True) + exp = pd.Period(exp, freq="D") expected = pd.Series([pd.Period("2012-1-1", freq="D"), pd.NaT, exp]) tm.assert_series_equal(result, expected) diff --git a/pandas/tests/series/test_logical_ops.py b/pandas/tests/series/test_logical_ops.py index d9c94e871bd4b..755d26623cf98 100644 --- a/pandas/tests/series/test_logical_ops.py +++ b/pandas/tests/series/test_logical_ops.py @@ -345,9 +345,9 @@ def test_reversed_logical_op_with_index_returns_series(self, op): @pytest.mark.parametrize( "op, expected", [ - (ops.rand_, Series([False, False])), - (ops.ror_, Series([True, True])), - (ops.rxor, Series([True, True])), + (ops.rand_, [False, False]), + (ops.ror_, [True, True]), + (ops.rxor, [True, True]), ], ) def test_reverse_ops_with_index(self, op, expected): @@ -358,6 +358,7 @@ def test_reverse_ops_with_index(self, op, expected): idx = Index([False, True]) result = op(ser, idx) + expected = Series(expected) tm.assert_series_equal(result, expected) def test_logical_ops_label_based(self, using_infer_string): From 9dfd01b1d71d9111b3ccf427a980915436d9fa54 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 5 Jan 2024 10:55:07 -0800 Subject: [PATCH 2/4] Fix typos --- pandas/tests/reshape/test_pivot.py | 2 +- pandas/tests/series/indexing/test_getitem.py | 2 +- pandas/tests/series/methods/test_fillna.py | 3 ++- pandas/tests/series/methods/test_replace.py | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/tests/reshape/test_pivot.py b/pandas/tests/reshape/test_pivot.py index bbce33b57dfc4..fbc3d2b8a7c35 100644 --- a/pandas/tests/reshape/test_pivot.py +++ b/pandas/tests/reshape/test_pivot.py @@ -744,7 +744,7 @@ def test_pivot_periods_with_margins(self): result = df.pivot_table(index="a", columns="b", values="x", margins=True) tm.assert_frame_equal(expected, result) - @pytest.mark.parametrize("values", [list, np.array, Series, Index]) + @pytest.mark.parametrize("box", [list, np.array, Series, Index]) @pytest.mark.parametrize("method", [True, False]) def test_pivot_with_list_like_values(self, box, method): # issue #17160 diff --git a/pandas/tests/series/indexing/test_getitem.py b/pandas/tests/series/indexing/test_getitem.py index c5b79b88084af..01c775e492888 100644 --- a/pandas/tests/series/indexing/test_getitem.py +++ b/pandas/tests/series/indexing/test_getitem.py @@ -561,7 +561,7 @@ def test_getitem_generator(string_series): @pytest.mark.parametrize( - "series", + "data", [ [0, 1], date_range("2012-01-01", periods=2), diff --git a/pandas/tests/series/methods/test_fillna.py b/pandas/tests/series/methods/test_fillna.py index 308d25b88b3eb..a70d9f39ff488 100644 --- a/pandas/tests/series/methods/test_fillna.py +++ b/pandas/tests/series/methods/test_fillna.py @@ -791,7 +791,8 @@ def test_fillna_categorical(self, fill_value, expected_output): ( Categorical( ["b", "d", "a", "d", "a"], categories=["b", "c", "d", "e", "a"] - )["a", "d", "b", "d", "a"], + ), + ["a", "d", "b", "d", "a"], ), ], ) diff --git a/pandas/tests/series/methods/test_replace.py b/pandas/tests/series/methods/test_replace.py index da5d1fcbc2ad9..abbdfb426272b 100644 --- a/pandas/tests/series/methods/test_replace.py +++ b/pandas/tests/series/methods/test_replace.py @@ -399,9 +399,9 @@ def test_replace_mixed_types_with_string(self): (["A", "B"], [1, 2]), ], ) - def test_replace_categorical(self, values, numeric): + def test_replace_categorical(self, categorical, numeric): # GH 24971, GH#23305 - ser = pd.Series(pd.Categorical(values, categories=["A", "B"])) + ser = pd.Series(pd.Categorical(categorical, categories=["A", "B"])) msg = "Downcasting behavior in `replace`" msg = "with CategoricalDtype is deprecated" with tm.assert_produces_warning(FutureWarning, match=msg): From 27debcf92fb0d119ea28cc4c7888593c072680eb Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Fri, 5 Jan 2024 14:55:18 -0800 Subject: [PATCH 3/4] fix typos --- pandas/tests/series/methods/test_isin.py | 2 +- pandas/tests/series/methods/test_value_counts.py | 4 +++- pandas/tests/series/test_arithmetic.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/pandas/tests/series/methods/test_isin.py b/pandas/tests/series/methods/test_isin.py index 08b34669f1aa4..937b85a547bcd 100644 --- a/pandas/tests/series/methods/test_isin.py +++ b/pandas/tests/series/methods/test_isin.py @@ -211,7 +211,7 @@ def test_isin_large_series_mixed_dtypes_and_nan(monkeypatch): tm.assert_series_equal(result, expected) -def test_isin_complex_numbers(array, expected): +def test_isin_complex_numbers(): # GH 17927 array = [0, 1j, 1j, 1, 1 + 1j, 1 + 2j, 1 + 1j] result = Series(array).isin([1j, 1 + 1j, 1 + 2j]) diff --git a/pandas/tests/series/methods/test_value_counts.py b/pandas/tests/series/methods/test_value_counts.py index f79ebf99f09d6..7f882fa348b7e 100644 --- a/pandas/tests/series/methods/test_value_counts.py +++ b/pandas/tests/series/methods/test_value_counts.py @@ -230,7 +230,9 @@ def test_value_counts_complex_numbers(self, dtype): # GH 17927 input_array = np.array([1 + 1j, 1 + 1j, 1, 3j, 3j, 3j], dtype=dtype) result = Series(input_array).value_counts() - expected = Series([3, 2, 1], dtype=dtype, name="count") + expected = Series( + [3, 2, 1], index=Index([3j, 1 + 1j, 1], dtype=dtype), name="count" + ) tm.assert_series_equal(result, expected) def test_value_counts_masked(self): diff --git a/pandas/tests/series/test_arithmetic.py b/pandas/tests/series/test_arithmetic.py index 5d18bfa793aa1..d71a515c85bd0 100644 --- a/pandas/tests/series/test_arithmetic.py +++ b/pandas/tests/series/test_arithmetic.py @@ -679,7 +679,7 @@ def test_comp_ops_df_compat(self, right_data, frame_or_series): # GH 1134 # GH 50083 to clarify that index and columns must be identically labeled left = Series([1, 2, 3], index=list("ABC"), name="x") - right = Series(right_data, index=list("ABD"), name="x") + right = Series(right_data, index=list("ABDC")[: len(right_data)], name="x") if frame_or_series is not Series: msg = ( rf"Can only compare identically-labeled \(both index and columns\) " From 2282ea1535e98f81aeac838737449ad7da1d0dca Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Mon, 8 Jan 2024 13:59:25 -0800 Subject: [PATCH 4/4] Add pytest --- pandas/tests/series/methods/test_diff.py | 1 + 1 file changed, 1 insertion(+) diff --git a/pandas/tests/series/methods/test_diff.py b/pandas/tests/series/methods/test_diff.py index 50ef99f97942b..89c34aeb64206 100644 --- a/pandas/tests/series/methods/test_diff.py +++ b/pandas/tests/series/methods/test_diff.py @@ -1,4 +1,5 @@ import numpy as np +import pytest from pandas import ( Series,