diff --git a/doc/source/development/contributing_codebase.rst b/doc/source/development/contributing_codebase.rst index df5b69c471b09..c06c0f8703d11 100644 --- a/doc/source/development/contributing_codebase.rst +++ b/doc/source/development/contributing_codebase.rst @@ -576,16 +576,6 @@ ignore the error. def test_thing(self): pass -If you need finer-grained control, you can use Python's -`warnings module `__ -to control whether a warning is ignored or raised at different places within -a single test. - -.. code-block:: python - - with warnings.catch_warnings(): - warnings.simplefilter("ignore", FutureWarning) - Testing an exception ^^^^^^^^^^^^^^^^^^^^ diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index 120cd1cabdc45..ba052c6936dd9 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -293,6 +293,7 @@ def test_apply_mixed_dtype_corner_indexing(): tm.assert_series_equal(result, expected) +@pytest.mark.filterwarnings("ignore::RuntimeWarning") @pytest.mark.parametrize("ax", ["index", "columns"]) @pytest.mark.parametrize( "func", [lambda x: x, lambda x: x.mean()], ids=["identity", "mean"] @@ -303,9 +304,7 @@ def test_apply_empty_infer_type(ax, func, raw, axis): df = DataFrame(**{ax: ["a", "b", "c"]}) with np.errstate(all="ignore"): - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", RuntimeWarning) - test_res = func(np.array([], dtype="f8")) + test_res = func(np.array([], dtype="f8")) is_reduction = not isinstance(test_res, np.ndarray) result = df.apply(func, axis=axis, raw=raw) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index 5e6026a9348c4..a3d9de5e78afb 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -8,7 +8,6 @@ from itertools import chain import re -import warnings import numpy as np import pytest @@ -298,6 +297,7 @@ def test_transform_and_agg_err_agg(axis, float_frame): float_frame.agg(["max", "sqrt"], axis=axis) +@pytest.mark.filterwarnings("ignore::FutureWarning") # GH53325 @pytest.mark.parametrize( "func, msg", [ @@ -312,10 +312,7 @@ def test_transform_and_agg_err_series(string_series, func, msg): # we are trying to transform with an aggregator with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): - # GH53325 - with warnings.catch_warnings(): - warnings.simplefilter("ignore", FutureWarning) - string_series.agg(func) + string_series.agg(func) @pytest.mark.parametrize("func", [["max", "min"], ["max", "sqrt"]]) diff --git a/pandas/tests/arithmetic/test_datetime64.py b/pandas/tests/arithmetic/test_datetime64.py index e6c743c76a2c1..01217f87b6359 100644 --- a/pandas/tests/arithmetic/test_datetime64.py +++ b/pandas/tests/arithmetic/test_datetime64.py @@ -11,7 +11,6 @@ starmap, ) import operator -import warnings import numpy as np import pytest @@ -1166,6 +1165,7 @@ def test_dt64arr_add_sub_parr( ) assert_invalid_addsub_type(dtarr, parr, msg) + @pytest.mark.filterwarnings("ignore::pandas.errors.PerformanceWarning") def test_dt64arr_addsub_time_objects_raises(self, box_with_array, tz_naive_fixture): # https://github.com/pandas-dev/pandas/issues/10329 @@ -1183,14 +1183,10 @@ def test_dt64arr_addsub_time_objects_raises(self, box_with_array, tz_naive_fixtu "cannot subtract DatetimeArray from ndarray", ] ) - - with warnings.catch_warnings(record=True): - # pandas.errors.PerformanceWarning: Non-vectorized DateOffset being - # applied to Series or DatetimeIndex - # we aren't testing that here, so ignore. - warnings.simplefilter("ignore", PerformanceWarning) - - assert_invalid_addsub_type(obj1, obj2, msg=msg) + # pandas.errors.PerformanceWarning: Non-vectorized DateOffset being + # applied to Series or DatetimeIndex + # we aren't testing that here, so ignore. + assert_invalid_addsub_type(obj1, obj2, msg=msg) # ------------------------------------------------------------- # Other invalid operations @@ -1370,6 +1366,7 @@ def test_dt64arr_add_sub_relativedelta_offsets(self, box_with_array): # TODO: redundant with test_dt64arr_add_sub_DateOffset? that includes # tz-aware cases which this does not + @pytest.mark.filterwarnings("ignore::pandas.errors.PerformanceWarning") @pytest.mark.parametrize( "cls_and_kwargs", [ @@ -1458,28 +1455,26 @@ def test_dt64arr_add_sub_DateOffsets( offset_cls = getattr(pd.offsets, cls_name) - with warnings.catch_warnings(record=True): - # pandas.errors.PerformanceWarning: Non-vectorized DateOffset being - # applied to Series or DatetimeIndex - # we aren't testing that here, so ignore. - warnings.simplefilter("ignore", PerformanceWarning) + # pandas.errors.PerformanceWarning: Non-vectorized DateOffset being + # applied to Series or DatetimeIndex + # we aren't testing that here, so ignore. - offset = offset_cls(n, normalize=normalize, **kwargs) + offset = offset_cls(n, normalize=normalize, **kwargs) - expected = DatetimeIndex([x + offset for x in vec_items]) - expected = tm.box_expected(expected, box_with_array) - tm.assert_equal(expected, vec + offset) + expected = DatetimeIndex([x + offset for x in vec_items]) + expected = tm.box_expected(expected, box_with_array) + tm.assert_equal(expected, vec + offset) - expected = DatetimeIndex([x - offset for x in vec_items]) - expected = tm.box_expected(expected, box_with_array) - tm.assert_equal(expected, vec - offset) + expected = DatetimeIndex([x - offset for x in vec_items]) + expected = tm.box_expected(expected, box_with_array) + tm.assert_equal(expected, vec - offset) - expected = DatetimeIndex([offset + x for x in vec_items]) - expected = tm.box_expected(expected, box_with_array) - tm.assert_equal(expected, offset + vec) - msg = "(bad|unsupported) operand type for unary" - with pytest.raises(TypeError, match=msg): - offset - vec + expected = DatetimeIndex([offset + x for x in vec_items]) + expected = tm.box_expected(expected, box_with_array) + tm.assert_equal(expected, offset + vec) + msg = "(bad|unsupported) operand type for unary" + with pytest.raises(TypeError, match=msg): + offset - vec def test_dt64arr_add_sub_DateOffset(self, box_with_array): # GH#10699 diff --git a/pandas/tests/arrays/categorical/test_operators.py b/pandas/tests/arrays/categorical/test_operators.py index 508ef6cb9c025..2e49d6d845a2d 100644 --- a/pandas/tests/arrays/categorical/test_operators.py +++ b/pandas/tests/arrays/categorical/test_operators.py @@ -1,5 +1,3 @@ -import warnings - import numpy as np import pytest @@ -196,6 +194,7 @@ def test_comparison_with_tuple(self): result = cat != (0, 1) tm.assert_numpy_array_equal(result, ~expected) + @pytest.mark.filterwarnings("ignore::RuntimeWarning") def test_comparison_of_ordered_categorical_with_nan_to_scalar( self, compare_operators_no_eq_ne ): @@ -206,12 +205,11 @@ def test_comparison_of_ordered_categorical_with_nan_to_scalar( cat = Categorical([1, 2, 3, None], categories=[1, 2, 3], ordered=True) scalar = 2 - with warnings.catch_warnings(): - warnings.simplefilter("ignore", RuntimeWarning) - expected = getattr(np.array(cat), compare_operators_no_eq_ne)(scalar) + expected = getattr(np.array(cat), compare_operators_no_eq_ne)(scalar) actual = getattr(cat, compare_operators_no_eq_ne)(scalar) tm.assert_numpy_array_equal(actual, expected) + @pytest.mark.filterwarnings("ignore::RuntimeWarning") def test_comparison_of_ordered_categorical_with_nan_to_listlike( self, compare_operators_no_eq_ne ): @@ -221,9 +219,7 @@ def test_comparison_of_ordered_categorical_with_nan_to_listlike( cat = Categorical([1, 2, 3, None], categories=[1, 2, 3], ordered=True) other = Categorical([2, 2, 2, 2], categories=[1, 2, 3], ordered=True) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", RuntimeWarning) - expected = getattr(np.array(cat), compare_operators_no_eq_ne)(2) + expected = getattr(np.array(cat), compare_operators_no_eq_ne)(2) actual = getattr(cat, compare_operators_no_eq_ne)(other) tm.assert_numpy_array_equal(actual, expected) diff --git a/pandas/tests/arrays/test_datetimelike.py b/pandas/tests/arrays/test_datetimelike.py index a4fbc8df4a8fa..9eee2e0bea687 100644 --- a/pandas/tests/arrays/test_datetimelike.py +++ b/pandas/tests/arrays/test_datetimelike.py @@ -49,7 +49,9 @@ def period_index(freqstr): # TODO: non-monotone indexes; NaTs, different start dates with warnings.catch_warnings(): # suppress deprecation of Period[B] - warnings.simplefilter("ignore") + warnings.filterwarnings( + "ignore", message="Period with BDay freq", category=FutureWarning + ) pi = pd.period_range(start=Timestamp("2000-01-01"), periods=100, freq=freqstr) return pi diff --git a/pandas/tests/computation/test_eval.py b/pandas/tests/computation/test_eval.py index e87e0beda9251..f958d25e51103 100644 --- a/pandas/tests/computation/test_eval.py +++ b/pandas/tests/computation/test_eval.py @@ -3,7 +3,6 @@ from functools import reduce from itertools import product import operator -import warnings import numpy as np import pytest @@ -761,28 +760,26 @@ def test_align_nested_unary_op(self, engine, parser): res = pd.eval(s, engine=engine, parser=parser) tm.assert_frame_equal(res, df * ~2) + @pytest.mark.filterwarnings("always::RuntimeWarning") @pytest.mark.parametrize("lr_idx_type", lhs_index_types) @pytest.mark.parametrize("rr_idx_type", index_types) @pytest.mark.parametrize("c_idx_type", index_types) def test_basic_frame_alignment( self, engine, parser, lr_idx_type, rr_idx_type, c_idx_type ): - with warnings.catch_warnings(record=True): - warnings.simplefilter("always", RuntimeWarning) - - df = tm.makeCustomDataframe( - 10, 10, data_gen_f=f, r_idx_type=lr_idx_type, c_idx_type=c_idx_type - ) - df2 = tm.makeCustomDataframe( - 20, 10, data_gen_f=f, r_idx_type=rr_idx_type, c_idx_type=c_idx_type - ) - # only warns if not monotonic and not sortable - if should_warn(df.index, df2.index): - with tm.assert_produces_warning(RuntimeWarning): - res = pd.eval("df + df2", engine=engine, parser=parser) - else: + df = tm.makeCustomDataframe( + 10, 10, data_gen_f=f, r_idx_type=lr_idx_type, c_idx_type=c_idx_type + ) + df2 = tm.makeCustomDataframe( + 20, 10, data_gen_f=f, r_idx_type=rr_idx_type, c_idx_type=c_idx_type + ) + # only warns if not monotonic and not sortable + if should_warn(df.index, df2.index): + with tm.assert_produces_warning(RuntimeWarning): res = pd.eval("df + df2", engine=engine, parser=parser) - tm.assert_frame_equal(res, df + df2) + else: + res = pd.eval("df + df2", engine=engine, parser=parser) + tm.assert_frame_equal(res, df + df2) @pytest.mark.parametrize("r_idx_type", lhs_index_types) @pytest.mark.parametrize("c_idx_type", lhs_index_types) @@ -801,55 +798,46 @@ def test_frame_comparison(self, engine, parser, r_idx_type, c_idx_type): res = pd.eval("df < df3", engine=engine, parser=parser) tm.assert_frame_equal(res, df < df3) + @pytest.mark.filterwarnings("ignore::RuntimeWarning") @pytest.mark.parametrize("r1", lhs_index_types) @pytest.mark.parametrize("c1", index_types) @pytest.mark.parametrize("r2", index_types) @pytest.mark.parametrize("c2", index_types) def test_medium_complex_frame_alignment(self, engine, parser, r1, c1, r2, c2): - with warnings.catch_warnings(record=True): - warnings.simplefilter("always", RuntimeWarning) - - df = tm.makeCustomDataframe( - 3, 2, data_gen_f=f, r_idx_type=r1, c_idx_type=c1 - ) - df2 = tm.makeCustomDataframe( - 4, 2, data_gen_f=f, r_idx_type=r2, c_idx_type=c2 - ) - df3 = tm.makeCustomDataframe( - 5, 2, data_gen_f=f, r_idx_type=r2, c_idx_type=c2 - ) - if should_warn(df.index, df2.index, df3.index): - with tm.assert_produces_warning(RuntimeWarning): - res = pd.eval("df + df2 + df3", engine=engine, parser=parser) - else: + df = tm.makeCustomDataframe(3, 2, data_gen_f=f, r_idx_type=r1, c_idx_type=c1) + df2 = tm.makeCustomDataframe(4, 2, data_gen_f=f, r_idx_type=r2, c_idx_type=c2) + df3 = tm.makeCustomDataframe(5, 2, data_gen_f=f, r_idx_type=r2, c_idx_type=c2) + if should_warn(df.index, df2.index, df3.index): + with tm.assert_produces_warning(RuntimeWarning): res = pd.eval("df + df2 + df3", engine=engine, parser=parser) - tm.assert_frame_equal(res, df + df2 + df3) + else: + res = pd.eval("df + df2 + df3", engine=engine, parser=parser) + tm.assert_frame_equal(res, df + df2 + df3) + @pytest.mark.filterwarnings("ignore::RuntimeWarning") @pytest.mark.parametrize("index_name", ["index", "columns"]) @pytest.mark.parametrize("c_idx_type", index_types) @pytest.mark.parametrize("r_idx_type", lhs_index_types) def test_basic_frame_series_alignment( self, engine, parser, index_name, r_idx_type, c_idx_type ): - with warnings.catch_warnings(record=True): - warnings.simplefilter("always", RuntimeWarning) - df = tm.makeCustomDataframe( - 10, 10, data_gen_f=f, r_idx_type=r_idx_type, c_idx_type=c_idx_type - ) - index = getattr(df, index_name) - s = Series(np.random.default_rng(2).standard_normal(5), index[:5]) + df = tm.makeCustomDataframe( + 10, 10, data_gen_f=f, r_idx_type=r_idx_type, c_idx_type=c_idx_type + ) + index = getattr(df, index_name) + s = Series(np.random.default_rng(2).standard_normal(5), index[:5]) - if should_warn(df.index, s.index): - with tm.assert_produces_warning(RuntimeWarning): - res = pd.eval("df + s", engine=engine, parser=parser) - else: + if should_warn(df.index, s.index): + with tm.assert_produces_warning(RuntimeWarning): res = pd.eval("df + s", engine=engine, parser=parser) + else: + res = pd.eval("df + s", engine=engine, parser=parser) - if r_idx_type == "dt" or c_idx_type == "dt": - expected = df.add(s) if engine == "numexpr" else df + s - else: - expected = df + s - tm.assert_frame_equal(res, expected) + if r_idx_type == "dt" or c_idx_type == "dt": + expected = df.add(s) if engine == "numexpr" else df + s + else: + expected = df + s + tm.assert_frame_equal(res, expected) @pytest.mark.parametrize("index_name", ["index", "columns"]) @pytest.mark.parametrize( @@ -890,6 +878,7 @@ def test_basic_series_frame_alignment( expected = s + df tm.assert_frame_equal(res, expected) + @pytest.mark.filterwarnings("ignore::RuntimeWarning") @pytest.mark.parametrize("c_idx_type", index_types) @pytest.mark.parametrize("r_idx_type", lhs_index_types) @pytest.mark.parametrize("index_name", ["index", "columns"]) @@ -897,30 +886,28 @@ def test_basic_series_frame_alignment( def test_series_frame_commutativity( self, engine, parser, index_name, op, r_idx_type, c_idx_type ): - with warnings.catch_warnings(record=True): - warnings.simplefilter("always", RuntimeWarning) + df = tm.makeCustomDataframe( + 10, 10, data_gen_f=f, r_idx_type=r_idx_type, c_idx_type=c_idx_type + ) + index = getattr(df, index_name) + s = Series(np.random.default_rng(2).standard_normal(5), index[:5]) - df = tm.makeCustomDataframe( - 10, 10, data_gen_f=f, r_idx_type=r_idx_type, c_idx_type=c_idx_type - ) - index = getattr(df, index_name) - s = Series(np.random.default_rng(2).standard_normal(5), index[:5]) - - lhs = f"s {op} df" - rhs = f"df {op} s" - if should_warn(df.index, s.index): - with tm.assert_produces_warning(RuntimeWarning): - a = pd.eval(lhs, engine=engine, parser=parser) - with tm.assert_produces_warning(RuntimeWarning): - b = pd.eval(rhs, engine=engine, parser=parser) - else: + lhs = f"s {op} df" + rhs = f"df {op} s" + if should_warn(df.index, s.index): + with tm.assert_produces_warning(RuntimeWarning): a = pd.eval(lhs, engine=engine, parser=parser) + with tm.assert_produces_warning(RuntimeWarning): b = pd.eval(rhs, engine=engine, parser=parser) + else: + a = pd.eval(lhs, engine=engine, parser=parser) + b = pd.eval(rhs, engine=engine, parser=parser) - if r_idx_type != "dt" and c_idx_type != "dt": - if engine == "numexpr": - tm.assert_frame_equal(a, b) + if r_idx_type != "dt" and c_idx_type != "dt": + if engine == "numexpr": + tm.assert_frame_equal(a, b) + @pytest.mark.filterwarnings("always::RuntimeWarning") @pytest.mark.parametrize("r1", lhs_index_types) @pytest.mark.parametrize("c1", index_types) @pytest.mark.parametrize("r2", index_types) @@ -930,44 +917,37 @@ def test_complex_series_frame_alignment(self, engine, parser, r1, c1, r2, c2): m1 = 5 m2 = 2 * m1 - with warnings.catch_warnings(record=True): - warnings.simplefilter("always", RuntimeWarning) + index_name = np.random.default_rng(2).choice(["index", "columns"]) + obj_name = np.random.default_rng(2).choice(["df", "df2"]) - index_name = np.random.default_rng(2).choice(["index", "columns"]) - obj_name = np.random.default_rng(2).choice(["df", "df2"]) + df = tm.makeCustomDataframe(m1, n, data_gen_f=f, r_idx_type=r1, c_idx_type=c1) + df2 = tm.makeCustomDataframe(m2, n, data_gen_f=f, r_idx_type=r2, c_idx_type=c2) + index = getattr(locals().get(obj_name), index_name) + ser = Series(np.random.default_rng(2).standard_normal(n), index[:n]) - df = tm.makeCustomDataframe( - m1, n, data_gen_f=f, r_idx_type=r1, c_idx_type=c1 - ) - df2 = tm.makeCustomDataframe( - m2, n, data_gen_f=f, r_idx_type=r2, c_idx_type=c2 - ) - index = getattr(locals().get(obj_name), index_name) - ser = Series(np.random.default_rng(2).standard_normal(n), index[:n]) - - if r2 == "dt" or c2 == "dt": - if engine == "numexpr": - expected2 = df2.add(ser) - else: - expected2 = df2 + ser + if r2 == "dt" or c2 == "dt": + if engine == "numexpr": + expected2 = df2.add(ser) else: expected2 = df2 + ser + else: + expected2 = df2 + ser - if r1 == "dt" or c1 == "dt": - if engine == "numexpr": - expected = expected2.add(df) - else: - expected = expected2 + df + if r1 == "dt" or c1 == "dt": + if engine == "numexpr": + expected = expected2.add(df) else: expected = expected2 + df + else: + expected = expected2 + df - if should_warn(df2.index, ser.index, df.index): - with tm.assert_produces_warning(RuntimeWarning): - res = pd.eval("df2 + ser + df", engine=engine, parser=parser) - else: + if should_warn(df2.index, ser.index, df.index): + with tm.assert_produces_warning(RuntimeWarning): res = pd.eval("df2 + ser + df", engine=engine, parser=parser) - assert res.shape == expected.shape - tm.assert_frame_equal(res, expected) + else: + res = pd.eval("df2 + ser + df", engine=engine, parser=parser) + assert res.shape == expected.shape + tm.assert_frame_equal(res, expected) def test_performance_warning_for_poor_alignment(self, engine, parser): df = DataFrame(np.random.default_rng(2).standard_normal((1000, 10))) diff --git a/pandas/tests/config/test_config.py b/pandas/tests/config/test_config.py index aad42b27cb80b..f49ae94242399 100644 --- a/pandas/tests/config/test_config.py +++ b/pandas/tests/config/test_config.py @@ -1,11 +1,10 @@ -import warnings - import pytest from pandas._config import config as cf from pandas._config.config import OptionError import pandas as pd +import pandas._testing as tm class TestConfig: @@ -270,38 +269,26 @@ def test_deprecate_option(self): cf.deprecate_option("foo") assert cf._is_deprecated("foo") - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + with tm.assert_produces_warning(FutureWarning, match="deprecated"): with pytest.raises(KeyError, match="No such keys.s.: 'foo'"): cf.get_option("foo") - assert len(w) == 1 # should have raised one warning - assert "deprecated" in str(w[-1]) # we get the default message cf.register_option("a", 1, "doc", validator=cf.is_int) cf.register_option("b.c", "hullo", "doc2") cf.register_option("foo", "hullo", "doc2") cf.deprecate_option("a", removal_ver="nifty_ver") - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + with tm.assert_produces_warning(FutureWarning, match="eprecated.*nifty_ver"): cf.get_option("a") - assert len(w) == 1 # should have raised one warning - assert "eprecated" in str(w[-1]) # we get the default message - assert "nifty_ver" in str(w[-1]) # with the removal_ver quoted - msg = "Option 'a' has already been defined as deprecated" with pytest.raises(OptionError, match=msg): cf.deprecate_option("a") cf.deprecate_option("b.c", "zounds!") - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + with tm.assert_produces_warning(FutureWarning, match="zounds!"): cf.get_option("b.c") - assert len(w) == 1 # should have raised one warning - assert "zounds!" in str(w[-1]) # we get the custom message - # test rerouting keys cf.register_option("d.a", "foo", "doc2") cf.register_option("d.dep", "bar", "doc2") @@ -309,27 +296,15 @@ def test_deprecate_option(self): assert cf.get_option("d.dep") == "bar" cf.deprecate_option("d.dep", rkey="d.a") # reroute d.dep to d.a - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + with tm.assert_produces_warning(FutureWarning, match="eprecated"): assert cf.get_option("d.dep") == "foo" - assert len(w) == 1 # should have raised one warning - assert "eprecated" in str(w[-1]) # we get the custom message - - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + with tm.assert_produces_warning(FutureWarning, match="eprecated"): cf.set_option("d.dep", "baz") # should overwrite "d.a" - assert len(w) == 1 # should have raised one warning - assert "eprecated" in str(w[-1]) # we get the custom message - - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + with tm.assert_produces_warning(FutureWarning, match="eprecated"): assert cf.get_option("d.dep") == "baz" - assert len(w) == 1 # should have raised one warning - assert "eprecated" in str(w[-1]) # we get the custom message - def test_config_prefix(self): with cf.config_prefix("base"): cf.register_option("a", 1, "doc1") diff --git a/pandas/tests/dtypes/test_generic.py b/pandas/tests/dtypes/test_generic.py index 25abad0004e7e..3da3237370e60 100644 --- a/pandas/tests/dtypes/test_generic.py +++ b/pandas/tests/dtypes/test_generic.py @@ -1,5 +1,4 @@ import re -from warnings import catch_warnings import numpy as np import pytest @@ -107,25 +106,22 @@ def test_setattr_warnings(): } df = pd.DataFrame(d) - with catch_warnings(record=True) as w: + with tm.assert_produces_warning(None): # successfully add new column # this should not raise a warning df["three"] = df.two + 1 - assert len(w) == 0 assert df.three.sum() > df.two.sum() - with catch_warnings(record=True) as w: + with tm.assert_produces_warning(None): # successfully modify column in place # this should not raise a warning df.one += 1 - assert len(w) == 0 assert df.one.iloc[0] == 2 - with catch_warnings(record=True) as w: + with tm.assert_produces_warning(None): # successfully add an attribute to a series # this should not raise a warning df.two.not_an_index = [1, 2] - assert len(w) == 0 with tm.assert_produces_warning(UserWarning): # warn when setting column to nonexistent name diff --git a/pandas/tests/extension/base/reduce.py b/pandas/tests/extension/base/reduce.py index 00919b16a2600..3d2870191ff6b 100644 --- a/pandas/tests/extension/base/reduce.py +++ b/pandas/tests/extension/base/reduce.py @@ -1,5 +1,4 @@ from typing import final -import warnings import pytest @@ -87,6 +86,7 @@ def test_reduce_series_boolean(self, data, all_boolean_reductions, skipna): else: self.check_reduce(s, op_name, skipna) + @pytest.mark.filterwarnings("ignore::RuntimeWarning") @pytest.mark.parametrize("skipna", [True, False]) def test_reduce_series_numeric(self, data, all_numeric_reductions, skipna): op_name = all_numeric_reductions @@ -103,9 +103,7 @@ def test_reduce_series_numeric(self, data, all_numeric_reductions, skipna): else: # min/max with empty produce numpy warnings - with warnings.catch_warnings(): - warnings.simplefilter("ignore", RuntimeWarning) - self.check_reduce(s, op_name, skipna) + self.check_reduce(s, op_name, skipna) @pytest.mark.parametrize("skipna", [True, False]) def test_reduce_frame(self, data, all_numeric_reductions, skipna): diff --git a/pandas/tests/frame/methods/test_cov_corr.py b/pandas/tests/frame/methods/test_cov_corr.py index f61cc2d76845c..23a9656193d2c 100644 --- a/pandas/tests/frame/methods/test_cov_corr.py +++ b/pandas/tests/frame/methods/test_cov_corr.py @@ -1,5 +1,3 @@ -import warnings - import numpy as np import pytest @@ -153,6 +151,7 @@ def test_corr_constant(self, meth): rs = df.corr(meth) assert isna(rs.values).all() + @pytest.mark.filterwarnings("ignore::RuntimeWarning") @pytest.mark.parametrize("meth", ["pearson", "kendall", "spearman"]) def test_corr_int_and_boolean(self, meth): # when dtypes of pandas series are different @@ -162,10 +161,7 @@ def test_corr_int_and_boolean(self, meth): df = DataFrame({"a": [True, False], "b": [1, 0]}) expected = DataFrame(np.ones((2, 2)), index=["a", "b"], columns=["a", "b"]) - - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", RuntimeWarning) - result = df.corr(meth) + result = df.corr(meth) tm.assert_frame_equal(result, expected) @pytest.mark.parametrize("method", ["cov", "corr"]) diff --git a/pandas/tests/frame/test_constructors.py b/pandas/tests/frame/test_constructors.py index 8244b62dd9e66..c8b67675b7798 100644 --- a/pandas/tests/frame/test_constructors.py +++ b/pandas/tests/frame/test_constructors.py @@ -14,7 +14,6 @@ ) import functools import re -import warnings import numpy as np from numpy import ma @@ -1054,6 +1053,9 @@ def test_constructor_maskedarray(self): frame = DataFrame(mat, columns=["A", "B", "C"], index=[1, 2]) assert np.all(~np.asarray(frame == frame)) + @pytest.mark.filterwarnings( + "ignore:elementwise comparison failed:DeprecationWarning" + ) def test_constructor_maskedarray_nonfloat(self): # masked int promoted to float mat = ma.masked_all((2, 3), dtype=int) @@ -1088,13 +1090,7 @@ def test_constructor_maskedarray_nonfloat(self): # cast type msg = r"datetime64\[ns\] values and dtype=int64 is not supported" with pytest.raises(TypeError, match=msg): - with warnings.catch_warnings(): - warnings.filterwarnings( - "ignore", - category=DeprecationWarning, - message="elementwise comparison failed", - ) - DataFrame(mat, columns=["A", "B", "C"], index=[1, 2], dtype=np.int64) + DataFrame(mat, columns=["A", "B", "C"], index=[1, 2], dtype=np.int64) # Check non-masked values mat2 = ma.copy(mat) diff --git a/pandas/tests/indexes/datetimes/methods/test_to_period.py b/pandas/tests/indexes/datetimes/methods/test_to_period.py index fe0b9464c7f45..14de6c5907d03 100644 --- a/pandas/tests/indexes/datetimes/methods/test_to_period.py +++ b/pandas/tests/indexes/datetimes/methods/test_to_period.py @@ -1,5 +1,3 @@ -import warnings - import dateutil.tz from dateutil.tz import tzlocal import pytest @@ -93,16 +91,10 @@ def test_to_period_infer(self): freq="5min", ) - with tm.assert_produces_warning(None): - # Using simple filter because we are not checking for the warning here - warnings.simplefilter("ignore", UserWarning) - + with tm.assert_produces_warning(UserWarning): pi1 = rng.to_period("5min") - with tm.assert_produces_warning(None): - # Using simple filter because we are not checking for the warning here - warnings.simplefilter("ignore", UserWarning) - + with tm.assert_produces_warning(UserWarning): pi2 = rng.to_period() tm.assert_index_equal(pi1, pi2) diff --git a/pandas/tests/indexes/multi/test_drop.py b/pandas/tests/indexes/multi/test_drop.py index f069cdbedabf0..99c8ebb1e57b2 100644 --- a/pandas/tests/indexes/multi/test_drop.py +++ b/pandas/tests/indexes/multi/test_drop.py @@ -1,5 +1,3 @@ -import warnings - import numpy as np import pytest @@ -154,12 +152,11 @@ def test_drop_with_nan_in_index(nulls_fixture): mi.drop(pd.Timestamp("2001"), level="date") +@pytest.mark.filterwarnings("ignore::pandas.errors.PerformanceWarning") def test_drop_with_non_monotonic_duplicates(): # GH#33494 mi = MultiIndex.from_tuples([(1, 2), (2, 3), (1, 2)]) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", PerformanceWarning) - result = mi.drop((1, 2)) + result = mi.drop((1, 2)) expected = MultiIndex.from_tuples([(2, 3)]) tm.assert_index_equal(result, expected) diff --git a/pandas/tests/indexing/test_iloc.py b/pandas/tests/indexing/test_iloc.py index 62172ec9a83ad..bc7604330695f 100644 --- a/pandas/tests/indexing/test_iloc.py +++ b/pandas/tests/indexing/test_iloc.py @@ -2,10 +2,6 @@ from datetime import datetime import re -from warnings import ( - catch_warnings, - simplefilter, -) import numpy as np import pytest @@ -742,6 +738,7 @@ def test_iloc_setitem_with_scalar_index(self, indexer, value): assert is_scalar(result) and result == "Z" + @pytest.mark.filterwarnings("ignore::UserWarning") def test_iloc_mask(self): # GH 3631, iloc with a mask (of a series) should raise df = DataFrame(list(range(5)), index=list("ABCDE"), columns=["a"]) @@ -786,32 +783,30 @@ def test_iloc_mask(self): } # UserWarnings from reindex of a boolean mask - with catch_warnings(record=True): - simplefilter("ignore", UserWarning) - for idx in [None, "index", "locs"]: - mask = (df.nums > 2).values - if idx: - mask_index = getattr(df, idx)[::-1] - mask = Series(mask, list(mask_index)) - for method in ["", ".loc", ".iloc"]: - try: - if method: - accessor = getattr(df, method[1:]) - else: - accessor = df - answer = str(bin(accessor[mask]["nums"].sum())) - except (ValueError, IndexingError, NotImplementedError) as e: - answer = str(e) - - key = ( - idx, - method, + for idx in [None, "index", "locs"]: + mask = (df.nums > 2).values + if idx: + mask_index = getattr(df, idx)[::-1] + mask = Series(mask, list(mask_index)) + for method in ["", ".loc", ".iloc"]: + try: + if method: + accessor = getattr(df, method[1:]) + else: + accessor = df + answer = str(bin(accessor[mask]["nums"].sum())) + except (ValueError, IndexingError, NotImplementedError) as e: + answer = str(e) + + key = ( + idx, + method, + ) + r = expected.get(key) + if r != answer: + raise AssertionError( + f"[{key}] does not match [{answer}], received [{r}]" ) - r = expected.get(key) - if r != answer: - raise AssertionError( - f"[{key}] does not match [{answer}], received [{r}]" - ) def test_iloc_non_unique_indexing(self): # GH 4017, non-unique indexing (on the axis) diff --git a/pandas/tests/io/parser/common/test_read_errors.py b/pandas/tests/io/parser/common/test_read_errors.py index 817daad9849c0..492b4d5ec058e 100644 --- a/pandas/tests/io/parser/common/test_read_errors.py +++ b/pandas/tests/io/parser/common/test_read_errors.py @@ -7,7 +7,6 @@ from io import StringIO import os from pathlib import Path -import warnings import numpy as np import pytest @@ -203,6 +202,7 @@ def test_null_byte_char(request, all_parsers): parser.read_csv(StringIO(data), names=names) +@pytest.mark.filterwarnings("always::ResourceWarning") def test_open_file(request, all_parsers): # GH 39024 parser = all_parsers @@ -218,12 +218,10 @@ def test_open_file(request, all_parsers): file = Path(path) file.write_bytes(b"\xe4\na\n1") - with warnings.catch_warnings(record=True) as record: + with tm.assert_produces_warning(None): # should not trigger a ResourceWarning - warnings.simplefilter("always", category=ResourceWarning) with pytest.raises(csv.Error, match="Could not determine delimiter"): parser.read_csv(file, sep=None, encoding_errors="replace") - assert len(record) == 0, record[0].message def test_invalid_on_bad_line(all_parsers): diff --git a/pandas/tests/io/pytables/test_append.py b/pandas/tests/io/pytables/test_append.py index a447601f3d8c4..c5a053a7500fe 100644 --- a/pandas/tests/io/pytables/test_append.py +++ b/pandas/tests/io/pytables/test_append.py @@ -1,7 +1,6 @@ import datetime from datetime import timedelta import re -from warnings import catch_warnings import numpy as np import pytest @@ -25,74 +24,76 @@ pytestmark = pytest.mark.single_cpu +tables = pytest.importorskip("tables") + +@pytest.mark.filterwarnings("ignore::tables.NaturalNameWarning") def test_append(setup_path): with ensure_clean_store(setup_path) as store: # this is allowed by almost always don't want to do it # tables.NaturalNameWarning): - with catch_warnings(record=True): - df = tm.makeTimeDataFrame() - _maybe_remove(store, "df1") - store.append("df1", df[:10]) - store.append("df1", df[10:]) - tm.assert_frame_equal(store["df1"], df) + df = tm.makeTimeDataFrame() + _maybe_remove(store, "df1") + store.append("df1", df[:10]) + store.append("df1", df[10:]) + tm.assert_frame_equal(store["df1"], df) - _maybe_remove(store, "df2") - store.put("df2", df[:10], format="table") - store.append("df2", df[10:]) - tm.assert_frame_equal(store["df2"], df) + _maybe_remove(store, "df2") + store.put("df2", df[:10], format="table") + store.append("df2", df[10:]) + tm.assert_frame_equal(store["df2"], df) - _maybe_remove(store, "df3") - store.append("/df3", df[:10]) - store.append("/df3", df[10:]) - tm.assert_frame_equal(store["df3"], df) + _maybe_remove(store, "df3") + store.append("/df3", df[:10]) + store.append("/df3", df[10:]) + tm.assert_frame_equal(store["df3"], df) - # this is allowed by almost always don't want to do it - # tables.NaturalNameWarning - _maybe_remove(store, "/df3 foo") - store.append("/df3 foo", df[:10]) - store.append("/df3 foo", df[10:]) - tm.assert_frame_equal(store["df3 foo"], df) - - # dtype issues - mizxed type in a single object column - df = DataFrame(data=[[1, 2], [0, 1], [1, 2], [0, 0]]) - df["mixed_column"] = "testing" - df.loc[2, "mixed_column"] = np.nan - _maybe_remove(store, "df") - store.append("df", df) - tm.assert_frame_equal(store["df"], df) + # this is allowed by almost always don't want to do it + # tables.NaturalNameWarning + _maybe_remove(store, "/df3 foo") + store.append("/df3 foo", df[:10]) + store.append("/df3 foo", df[10:]) + tm.assert_frame_equal(store["df3 foo"], df) + + # dtype issues - mizxed type in a single object column + df = DataFrame(data=[[1, 2], [0, 1], [1, 2], [0, 0]]) + df["mixed_column"] = "testing" + df.loc[2, "mixed_column"] = np.nan + _maybe_remove(store, "df") + store.append("df", df) + tm.assert_frame_equal(store["df"], df) - # uints - test storage of uints - uint_data = DataFrame( - { - "u08": Series( - np.random.default_rng(2).integers(0, high=255, size=5), - dtype=np.uint8, - ), - "u16": Series( - np.random.default_rng(2).integers(0, high=65535, size=5), - dtype=np.uint16, - ), - "u32": Series( - np.random.default_rng(2).integers(0, high=2**30, size=5), - dtype=np.uint32, - ), - "u64": Series( - [2**58, 2**59, 2**60, 2**61, 2**62], - dtype=np.uint64, - ), - }, - index=np.arange(5), - ) - _maybe_remove(store, "uints") - store.append("uints", uint_data) - tm.assert_frame_equal(store["uints"], uint_data, check_index_type=True) + # uints - test storage of uints + uint_data = DataFrame( + { + "u08": Series( + np.random.default_rng(2).integers(0, high=255, size=5), + dtype=np.uint8, + ), + "u16": Series( + np.random.default_rng(2).integers(0, high=65535, size=5), + dtype=np.uint16, + ), + "u32": Series( + np.random.default_rng(2).integers(0, high=2**30, size=5), + dtype=np.uint32, + ), + "u64": Series( + [2**58, 2**59, 2**60, 2**61, 2**62], + dtype=np.uint64, + ), + }, + index=np.arange(5), + ) + _maybe_remove(store, "uints") + store.append("uints", uint_data) + tm.assert_frame_equal(store["uints"], uint_data, check_index_type=True) - # uints - test storage of uints in indexable columns - _maybe_remove(store, "uints") - # 64-bit indices not yet supported - store.append("uints", uint_data, data_columns=["u08", "u16", "u32"]) - tm.assert_frame_equal(store["uints"], uint_data, check_index_type=True) + # uints - test storage of uints in indexable columns + _maybe_remove(store, "uints") + # 64-bit indices not yet supported + store.append("uints", uint_data, data_columns=["u08", "u16", "u32"]) + tm.assert_frame_equal(store["uints"], uint_data, check_index_type=True) def test_append_series(setup_path): @@ -357,81 +358,77 @@ def test_append_with_different_block_ordering(setup_path): def test_append_with_strings(setup_path): with ensure_clean_store(setup_path) as store: - with catch_warnings(record=True): - - def check_col(key, name, size): - assert ( - getattr(store.get_storer(key).table.description, name).itemsize - == size - ) - - # avoid truncation on elements - df = DataFrame([[123, "asdqwerty"], [345, "dggnhebbsdfbdfb"]]) - store.append("df_big", df) - tm.assert_frame_equal(store.select("df_big"), df) - check_col("df_big", "values_block_1", 15) - - # appending smaller string ok - df2 = DataFrame([[124, "asdqy"], [346, "dggnhefbdfb"]]) - store.append("df_big", df2) - expected = concat([df, df2]) - tm.assert_frame_equal(store.select("df_big"), expected) - check_col("df_big", "values_block_1", 15) - - # avoid truncation on elements - df = DataFrame([[123, "asdqwerty"], [345, "dggnhebbsdfbdfb"]]) - store.append("df_big2", df, min_itemsize={"values": 50}) - tm.assert_frame_equal(store.select("df_big2"), df) - check_col("df_big2", "values_block_1", 50) - - # bigger string on next append - store.append("df_new", df) - df_new = DataFrame( - [[124, "abcdefqhij"], [346, "abcdefghijklmnopqrtsuvwxyz"]] - ) - msg = ( - r"Trying to store a string with len \[26\] in " - r"\[values_block_1\] column but\n" - r"this column has a limit of \[15\]!\n" - "Consider using min_itemsize to preset the sizes on these " - "columns" + + def check_col(key, name, size): + assert ( + getattr(store.get_storer(key).table.description, name).itemsize == size ) - with pytest.raises(ValueError, match=msg): - store.append("df_new", df_new) - - # min_itemsize on Series index (GH 11412) - df = tm.makeMixedDataFrame().set_index("C") - store.append("ss", df["B"], min_itemsize={"index": 4}) - tm.assert_series_equal(store.select("ss"), df["B"]) - - # same as above, with data_columns=True - store.append("ss2", df["B"], data_columns=True, min_itemsize={"index": 4}) - tm.assert_series_equal(store.select("ss2"), df["B"]) - - # min_itemsize in index without appending (GH 10381) - store.put("ss3", df, format="table", min_itemsize={"index": 6}) - # just make sure there is a longer string: - df2 = df.copy().reset_index().assign(C="longer").set_index("C") - store.append("ss3", df2) - tm.assert_frame_equal(store.select("ss3"), concat([df, df2])) - - # same as above, with a Series - store.put("ss4", df["B"], format="table", min_itemsize={"index": 6}) - store.append("ss4", df2["B"]) - tm.assert_series_equal(store.select("ss4"), concat([df["B"], df2["B"]])) - - # with nans - _maybe_remove(store, "df") - df = tm.makeTimeDataFrame() - df["string"] = "foo" - df.loc[df.index[1:4], "string"] = np.nan - df["string2"] = "bar" - df.loc[df.index[4:8], "string2"] = np.nan - df["string3"] = "bah" - df.loc[df.index[1:], "string3"] = np.nan - store.append("df", df) - result = store.select("df") - tm.assert_frame_equal(result, df) + + # avoid truncation on elements + df = DataFrame([[123, "asdqwerty"], [345, "dggnhebbsdfbdfb"]]) + store.append("df_big", df) + tm.assert_frame_equal(store.select("df_big"), df) + check_col("df_big", "values_block_1", 15) + + # appending smaller string ok + df2 = DataFrame([[124, "asdqy"], [346, "dggnhefbdfb"]]) + store.append("df_big", df2) + expected = concat([df, df2]) + tm.assert_frame_equal(store.select("df_big"), expected) + check_col("df_big", "values_block_1", 15) + + # avoid truncation on elements + df = DataFrame([[123, "asdqwerty"], [345, "dggnhebbsdfbdfb"]]) + store.append("df_big2", df, min_itemsize={"values": 50}) + tm.assert_frame_equal(store.select("df_big2"), df) + check_col("df_big2", "values_block_1", 50) + + # bigger string on next append + store.append("df_new", df) + df_new = DataFrame([[124, "abcdefqhij"], [346, "abcdefghijklmnopqrtsuvwxyz"]]) + msg = ( + r"Trying to store a string with len \[26\] in " + r"\[values_block_1\] column but\n" + r"this column has a limit of \[15\]!\n" + "Consider using min_itemsize to preset the sizes on these " + "columns" + ) + with pytest.raises(ValueError, match=msg): + store.append("df_new", df_new) + + # min_itemsize on Series index (GH 11412) + df = tm.makeMixedDataFrame().set_index("C") + store.append("ss", df["B"], min_itemsize={"index": 4}) + tm.assert_series_equal(store.select("ss"), df["B"]) + + # same as above, with data_columns=True + store.append("ss2", df["B"], data_columns=True, min_itemsize={"index": 4}) + tm.assert_series_equal(store.select("ss2"), df["B"]) + + # min_itemsize in index without appending (GH 10381) + store.put("ss3", df, format="table", min_itemsize={"index": 6}) + # just make sure there is a longer string: + df2 = df.copy().reset_index().assign(C="longer").set_index("C") + store.append("ss3", df2) + tm.assert_frame_equal(store.select("ss3"), concat([df, df2])) + + # same as above, with a Series + store.put("ss4", df["B"], format="table", min_itemsize={"index": 6}) + store.append("ss4", df2["B"]) + tm.assert_series_equal(store.select("ss4"), concat([df["B"], df2["B"]])) + + # with nans + _maybe_remove(store, "df") + df = tm.makeTimeDataFrame() + df["string"] = "foo" + df.loc[df.index[1:4], "string"] = np.nan + df["string2"] = "bar" + df.loc[df.index[4:8], "string2"] = np.nan + df["string3"] = "bah" + df.loc[df.index[1:], "string3"] = np.nan + store.append("df", df) + result = store.select("df") + tm.assert_frame_equal(result, df) with ensure_clean_store(setup_path) as store: df = DataFrame({"A": "foo", "B": "bar"}, index=range(10)) diff --git a/pandas/tests/io/pytables/test_complex.py b/pandas/tests/io/pytables/test_complex.py index 7df6223df70e7..dd2f650a2070d 100644 --- a/pandas/tests/io/pytables/test_complex.py +++ b/pandas/tests/io/pytables/test_complex.py @@ -1,5 +1,3 @@ -from warnings import catch_warnings - import numpy as np import pytest @@ -114,18 +112,17 @@ def test_complex_mixed_table(tmp_path, setup_path): def test_complex_across_dimensions_fixed(tmp_path, setup_path): - with catch_warnings(record=True): - complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) - s = Series(complex128, index=list("abcd")) - df = DataFrame({"A": s, "B": s}) + complex128 = np.array([1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j, 1.0 + 1.0j]) + s = Series(complex128, index=list("abcd")) + df = DataFrame({"A": s, "B": s}) - objs = [s, df] - comps = [tm.assert_series_equal, tm.assert_frame_equal] - for obj, comp in zip(objs, comps): - path = tmp_path / setup_path - obj.to_hdf(path, "obj", format="fixed") - reread = read_hdf(path, "obj") - comp(obj, reread) + objs = [s, df] + comps = [tm.assert_series_equal, tm.assert_frame_equal] + for obj, comp in zip(objs, comps): + path = tmp_path / setup_path + obj.to_hdf(path, "obj", format="fixed") + reread = read_hdf(path, "obj") + comp(obj, reread) def test_complex_across_dimensions(tmp_path, setup_path): @@ -133,14 +130,10 @@ def test_complex_across_dimensions(tmp_path, setup_path): s = Series(complex128, index=list("abcd")) df = DataFrame({"A": s, "B": s}) - with catch_warnings(record=True): - objs = [df] - comps = [tm.assert_frame_equal] - for obj, comp in zip(objs, comps): - path = tmp_path / setup_path - obj.to_hdf(path, "obj", format="table") - reread = read_hdf(path, "obj") - comp(obj, reread) + path = tmp_path / setup_path + df.to_hdf(path, "obj", format="table") + reread = read_hdf(path, "obj") + tm.assert_frame_equal(df, reread) def test_complex_indexing_error(setup_path): diff --git a/pandas/tests/io/pytables/test_errors.py b/pandas/tests/io/pytables/test_errors.py index 44bdbfc3fdd7e..72fdb0f78d8e6 100644 --- a/pandas/tests/io/pytables/test_errors.py +++ b/pandas/tests/io/pytables/test_errors.py @@ -1,7 +1,6 @@ import datetime from io import BytesIO import re -from warnings import catch_warnings import numpy as np import pytest @@ -86,31 +85,28 @@ def test_unimplemented_dtypes_table_columns(setup_path): def test_invalid_terms(tmp_path, setup_path): with ensure_clean_store(setup_path) as store: - with catch_warnings(record=True): - df = tm.makeTimeDataFrame() - df["string"] = "foo" - df.loc[df.index[0:4], "string"] = "bar" + df = tm.makeTimeDataFrame() + df["string"] = "foo" + df.loc[df.index[0:4], "string"] = "bar" - store.put("df", df, format="table") + store.put("df", df, format="table") - # some invalid terms - msg = re.escape( - "__init__() missing 1 required positional argument: 'where'" - ) - with pytest.raises(TypeError, match=msg): - Term() + # some invalid terms + msg = re.escape("__init__() missing 1 required positional argument: 'where'") + with pytest.raises(TypeError, match=msg): + Term() - # more invalid - msg = re.escape( - "cannot process expression [df.index[3]], " - "[2000-01-06 00:00:00] is not a valid condition" - ) - with pytest.raises(ValueError, match=msg): - store.select("df", "df.index[3]") + # more invalid + msg = re.escape( + "cannot process expression [df.index[3]], " + "[2000-01-06 00:00:00] is not a valid condition" + ) + with pytest.raises(ValueError, match=msg): + store.select("df", "df.index[3]") - msg = "invalid syntax" - with pytest.raises(SyntaxError, match=msg): - store.select("df", "index>") + msg = "invalid syntax" + with pytest.raises(SyntaxError, match=msg): + store.select("df", "index>") # from the docs path = tmp_path / setup_path diff --git a/pandas/tests/io/pytables/test_put.py b/pandas/tests/io/pytables/test_put.py index e63e3f71c93b5..5bf94340f4d3f 100644 --- a/pandas/tests/io/pytables/test_put.py +++ b/pandas/tests/io/pytables/test_put.py @@ -1,9 +1,5 @@ import datetime import re -from warnings import ( - catch_warnings, - simplefilter, -) import numpy as np import pytest @@ -193,9 +189,7 @@ def test_put_mixed_type(setup_path): with ensure_clean_store(setup_path) as store: _maybe_remove(store, "df") - # PerformanceWarning - with catch_warnings(record=True): - simplefilter("ignore", pd.errors.PerformanceWarning) + with tm.assert_produces_warning(pd.errors.PerformanceWarning): store.put("df", df) expected = store.get("df") diff --git a/pandas/tests/io/pytables/test_round_trip.py b/pandas/tests/io/pytables/test_round_trip.py index 8ffdc421492a5..085db5f521a9f 100644 --- a/pandas/tests/io/pytables/test_round_trip.py +++ b/pandas/tests/io/pytables/test_round_trip.py @@ -1,9 +1,5 @@ import datetime import re -from warnings import ( - catch_warnings, - simplefilter, -) import numpy as np import pytest @@ -281,60 +277,51 @@ def test_tuple_index(setup_path): data = np.random.default_rng(2).standard_normal(30).reshape((3, 10)) DF = DataFrame(data, index=idx, columns=col) - with catch_warnings(record=True): - simplefilter("ignore", pd.errors.PerformanceWarning) + with tm.assert_produces_warning(pd.errors.PerformanceWarning): _check_roundtrip(DF, tm.assert_frame_equal, path=setup_path) @pytest.mark.filterwarnings("ignore::pandas.errors.PerformanceWarning") def test_index_types(setup_path): - with catch_warnings(record=True): - values = np.random.default_rng(2).standard_normal(2) + values = np.random.default_rng(2).standard_normal(2) - func = lambda lhs, rhs: tm.assert_series_equal(lhs, rhs, check_index_type=True) + func = lambda lhs, rhs: tm.assert_series_equal(lhs, rhs, check_index_type=True) - with catch_warnings(record=True): - ser = Series(values, [0, "y"]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, [0, "y"]) + _check_roundtrip(ser, func, path=setup_path) - with catch_warnings(record=True): - ser = Series(values, [datetime.datetime.today(), 0]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, [datetime.datetime.today(), 0]) + _check_roundtrip(ser, func, path=setup_path) - with catch_warnings(record=True): - ser = Series(values, ["y", 0]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, ["y", 0]) + _check_roundtrip(ser, func, path=setup_path) - with catch_warnings(record=True): - ser = Series(values, [datetime.date.today(), "a"]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, [datetime.date.today(), "a"]) + _check_roundtrip(ser, func, path=setup_path) - with catch_warnings(record=True): - ser = Series(values, [0, "y"]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, [0, "y"]) + _check_roundtrip(ser, func, path=setup_path) - ser = Series(values, [datetime.datetime.today(), 0]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, [datetime.datetime.today(), 0]) + _check_roundtrip(ser, func, path=setup_path) - ser = Series(values, ["y", 0]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, ["y", 0]) + _check_roundtrip(ser, func, path=setup_path) - ser = Series(values, [datetime.date.today(), "a"]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, [datetime.date.today(), "a"]) + _check_roundtrip(ser, func, path=setup_path) - ser = Series(values, [1.23, "b"]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, [1.23, "b"]) + _check_roundtrip(ser, func, path=setup_path) - ser = Series(values, [1, 1.53]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, [1, 1.53]) + _check_roundtrip(ser, func, path=setup_path) - ser = Series(values, [1, 5]) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, [1, 5]) + _check_roundtrip(ser, func, path=setup_path) - ser = Series( - values, [datetime.datetime(2012, 1, 1), datetime.datetime(2012, 1, 2)] - ) - _check_roundtrip(ser, func, path=setup_path) + ser = Series(values, [datetime.datetime(2012, 1, 1), datetime.datetime(2012, 1, 2)]) + _check_roundtrip(ser, func, path=setup_path) def test_timeseries_preepoch(setup_path, request): @@ -499,14 +486,11 @@ def _check_roundtrip_table(obj, comparator, path, compression=False): def test_unicode_index(setup_path): unicode_values = ["\u03c3", "\u03c3\u03c3"] - # PerformanceWarning - with catch_warnings(record=True): - simplefilter("ignore", pd.errors.PerformanceWarning) - s = Series( - np.random.default_rng(2).standard_normal(len(unicode_values)), - unicode_values, - ) - _check_roundtrip(s, tm.assert_series_equal, path=setup_path) + s = Series( + np.random.default_rng(2).standard_normal(len(unicode_values)), + unicode_values, + ) + _check_roundtrip(s, tm.assert_series_equal, path=setup_path) def test_unicode_longer_encoded(setup_path): diff --git a/pandas/tests/io/pytables/test_select.py b/pandas/tests/io/pytables/test_select.py index 77e69f1264f1b..dc592beae45ba 100644 --- a/pandas/tests/io/pytables/test_select.py +++ b/pandas/tests/io/pytables/test_select.py @@ -1,5 +1,3 @@ -from warnings import catch_warnings - import numpy as np import pytest @@ -131,40 +129,39 @@ def test_select_with_dups(setup_path): def test_select(setup_path): with ensure_clean_store(setup_path) as store: - with catch_warnings(record=True): - # select with columns= - df = tm.makeTimeDataFrame() - _maybe_remove(store, "df") - store.append("df", df) - result = store.select("df", columns=["A", "B"]) - expected = df.reindex(columns=["A", "B"]) - tm.assert_frame_equal(expected, result) + # select with columns= + df = tm.makeTimeDataFrame() + _maybe_remove(store, "df") + store.append("df", df) + result = store.select("df", columns=["A", "B"]) + expected = df.reindex(columns=["A", "B"]) + tm.assert_frame_equal(expected, result) - # equivalently - result = store.select("df", [("columns=['A', 'B']")]) - expected = df.reindex(columns=["A", "B"]) - tm.assert_frame_equal(expected, result) + # equivalently + result = store.select("df", [("columns=['A', 'B']")]) + expected = df.reindex(columns=["A", "B"]) + tm.assert_frame_equal(expected, result) - # with a data column - _maybe_remove(store, "df") - store.append("df", df, data_columns=["A"]) - result = store.select("df", ["A > 0"], columns=["A", "B"]) - expected = df[df.A > 0].reindex(columns=["A", "B"]) - tm.assert_frame_equal(expected, result) + # with a data column + _maybe_remove(store, "df") + store.append("df", df, data_columns=["A"]) + result = store.select("df", ["A > 0"], columns=["A", "B"]) + expected = df[df.A > 0].reindex(columns=["A", "B"]) + tm.assert_frame_equal(expected, result) - # all a data columns - _maybe_remove(store, "df") - store.append("df", df, data_columns=True) - result = store.select("df", ["A > 0"], columns=["A", "B"]) - expected = df[df.A > 0].reindex(columns=["A", "B"]) - tm.assert_frame_equal(expected, result) + # all a data columns + _maybe_remove(store, "df") + store.append("df", df, data_columns=True) + result = store.select("df", ["A > 0"], columns=["A", "B"]) + expected = df[df.A > 0].reindex(columns=["A", "B"]) + tm.assert_frame_equal(expected, result) - # with a data column, but different columns - _maybe_remove(store, "df") - store.append("df", df, data_columns=["A"]) - result = store.select("df", ["A > 0"], columns=["C", "D"]) - expected = df[df.A > 0].reindex(columns=["C", "D"]) - tm.assert_frame_equal(expected, result) + # with a data column, but different columns + _maybe_remove(store, "df") + store.append("df", df, data_columns=["A"]) + result = store.select("df", ["A > 0"], columns=["C", "D"]) + expected = df[df.A > 0].reindex(columns=["C", "D"]) + tm.assert_frame_equal(expected, result) def test_select_dtypes(setup_path): diff --git a/pandas/tests/io/pytables/test_store.py b/pandas/tests/io/pytables/test_store.py index 5d45c92ece0dc..9d7cb52e3817d 100644 --- a/pandas/tests/io/pytables/test_store.py +++ b/pandas/tests/io/pytables/test_store.py @@ -3,10 +3,6 @@ import hashlib import tempfile import time -from warnings import ( - catch_warnings, - simplefilter, -) import numpy as np import pytest @@ -36,6 +32,8 @@ pytestmark = pytest.mark.single_cpu +tables = pytest.importorskip("tables") + def test_context(setup_path): with tm.ensure_clean(setup_path) as path: @@ -123,8 +121,7 @@ def test_repr(setup_path): df.loc[df.index[3:6], ["obj1"]] = np.nan df = df._consolidate() - with catch_warnings(record=True): - simplefilter("ignore", pd.errors.PerformanceWarning) + with tm.assert_produces_warning(pd.errors.PerformanceWarning): store["df"] = df # make a random group in hdf space @@ -158,7 +155,9 @@ def test_contains(setup_path): assert "bar" not in store # gh-2694: tables.NaturalNameWarning - with catch_warnings(record=True): + with tm.assert_produces_warning( + tables.NaturalNameWarning, check_stacklevel=False + ): store["node())"] = tm.makeDataFrame() assert "node())" in store @@ -335,63 +334,61 @@ def test_to_hdf_errors(tmp_path, format, setup_path): def test_create_table_index(setup_path): with ensure_clean_store(setup_path) as store: - with catch_warnings(record=True): - - def col(t, column): - return getattr(store.get_storer(t).table.cols, column) - - # data columns - df = tm.makeTimeDataFrame() - df["string"] = "foo" - df["string2"] = "bar" - store.append("f", df, data_columns=["string", "string2"]) - assert col("f", "index").is_indexed is True - assert col("f", "string").is_indexed is True - assert col("f", "string2").is_indexed is True - - # specify index=columns - store.append("f2", df, index=["string"], data_columns=["string", "string2"]) - assert col("f2", "index").is_indexed is False - assert col("f2", "string").is_indexed is True - assert col("f2", "string2").is_indexed is False - - # try to index a non-table - _maybe_remove(store, "f2") - store.put("f2", df) - msg = "cannot create table index on a Fixed format store" - with pytest.raises(TypeError, match=msg): - store.create_table_index("f2") + + def col(t, column): + return getattr(store.get_storer(t).table.cols, column) + + # data columns + df = tm.makeTimeDataFrame() + df["string"] = "foo" + df["string2"] = "bar" + store.append("f", df, data_columns=["string", "string2"]) + assert col("f", "index").is_indexed is True + assert col("f", "string").is_indexed is True + assert col("f", "string2").is_indexed is True + + # specify index=columns + store.append("f2", df, index=["string"], data_columns=["string", "string2"]) + assert col("f2", "index").is_indexed is False + assert col("f2", "string").is_indexed is True + assert col("f2", "string2").is_indexed is False + + # try to index a non-table + _maybe_remove(store, "f2") + store.put("f2", df) + msg = "cannot create table index on a Fixed format store" + with pytest.raises(TypeError, match=msg): + store.create_table_index("f2") def test_create_table_index_data_columns_argument(setup_path): # GH 28156 with ensure_clean_store(setup_path) as store: - with catch_warnings(record=True): - def col(t, column): - return getattr(store.get_storer(t).table.cols, column) + def col(t, column): + return getattr(store.get_storer(t).table.cols, column) - # data columns - df = tm.makeTimeDataFrame() - df["string"] = "foo" - df["string2"] = "bar" - store.append("f", df, data_columns=["string"]) - assert col("f", "index").is_indexed is True - assert col("f", "string").is_indexed is True + # data columns + df = tm.makeTimeDataFrame() + df["string"] = "foo" + df["string2"] = "bar" + store.append("f", df, data_columns=["string"]) + assert col("f", "index").is_indexed is True + assert col("f", "string").is_indexed is True - msg = "'Cols' object has no attribute 'string2'" - with pytest.raises(AttributeError, match=msg): - col("f", "string2").is_indexed + msg = "'Cols' object has no attribute 'string2'" + with pytest.raises(AttributeError, match=msg): + col("f", "string2").is_indexed - # try to index a col which isn't a data_column - msg = ( - "column string2 is not a data_column.\n" - "In order to read column string2 you must reload the dataframe \n" - "into HDFStore and include string2 with the data_columns argument." - ) - with pytest.raises(AttributeError, match=msg): - store.create_table_index("f", columns=["string2"]) + # try to index a col which isn't a data_column + msg = ( + "column string2 is not a data_column.\n" + "In order to read column string2 you must reload the dataframe \n" + "into HDFStore and include string2 with the data_columns argument." + ) + with pytest.raises(AttributeError, match=msg): + store.create_table_index("f", columns=["string2"]) def test_mi_data_columns(setup_path): @@ -948,20 +945,18 @@ def test_to_hdf_with_object_column_names(tmp_path, setup_path): np.random.default_rng(2).standard_normal((10, 2)), columns=index(2) ) path = tmp_path / setup_path - with catch_warnings(record=True): - msg = "cannot have non-object label DataIndexableCol" - with pytest.raises(ValueError, match=msg): - df.to_hdf(path, "df", format="table", data_columns=True) + msg = "cannot have non-object label DataIndexableCol" + with pytest.raises(ValueError, match=msg): + df.to_hdf(path, "df", format="table", data_columns=True) for index in types_should_run: df = DataFrame( np.random.default_rng(2).standard_normal((10, 2)), columns=index(2) ) path = tmp_path / setup_path - with catch_warnings(record=True): - df.to_hdf(path, "df", format="table", data_columns=True) - result = read_hdf(path, "df", where=f"index = [{df.index[0]}]") - assert len(result) + df.to_hdf(path, "df", format="table", data_columns=True) + result = read_hdf(path, "df", where=f"index = [{df.index[0]}]") + assert len(result) def test_hdfstore_strides(setup_path): diff --git a/pandas/tests/io/test_parquet.py b/pandas/tests/io/test_parquet.py index 501e471695a8a..f3b1ac8062f19 100644 --- a/pandas/tests/io/test_parquet.py +++ b/pandas/tests/io/test_parquet.py @@ -3,7 +3,6 @@ from io import BytesIO import os import pathlib -from warnings import catch_warnings import numpy as np import pytest @@ -201,8 +200,7 @@ def check_round_trip( def compare(repeat): for _ in range(repeat): df.to_parquet(path, **write_kwargs) - with catch_warnings(record=True): - actual = read_parquet(path, **read_kwargs) + actual = read_parquet(path, **read_kwargs) if "string_with_nan" in expected: expected.loc[1, "string_with_nan"] = None @@ -354,12 +352,11 @@ def test_cross_engine_fp_pa(df_cross_compat, pa, fp): with tm.ensure_clean() as path: df.to_parquet(path, engine=fp, compression=None) - with catch_warnings(record=True): - result = read_parquet(path, engine=pa) - tm.assert_frame_equal(result, df) + result = read_parquet(path, engine=pa) + tm.assert_frame_equal(result, df) - result = read_parquet(path, engine=pa, columns=["a", "d"]) - tm.assert_frame_equal(result, df[["a", "d"]]) + result = read_parquet(path, engine=pa, columns=["a", "d"]) + tm.assert_frame_equal(result, df[["a", "d"]]) class Base: diff --git a/pandas/tests/io/test_pickle.py b/pandas/tests/io/test_pickle.py index ac24dc21bab38..75e4de7074e63 100644 --- a/pandas/tests/io/test_pickle.py +++ b/pandas/tests/io/test_pickle.py @@ -23,7 +23,6 @@ import shutil import tarfile import uuid -from warnings import catch_warnings import zipfile import numpy as np @@ -44,6 +43,7 @@ period_range, ) import pandas._testing as tm +from pandas.tests.io.generate_legacy_storage_files import create_pickle_data import pandas.io.common as icom from pandas.tseries.offsets import ( @@ -55,10 +55,7 @@ @pytest.fixture def current_pickle_data(): # our current version pickle data - from pandas.tests.io.generate_legacy_storage_files import create_pickle_data - - with catch_warnings(): - return create_pickle_data() + return create_pickle_data() # --------------------- diff --git a/pandas/tests/io/test_stata.py b/pandas/tests/io/test_stata.py index 6130271325bee..3a7762a6a6060 100644 --- a/pandas/tests/io/test_stata.py +++ b/pandas/tests/io/test_stata.py @@ -6,7 +6,6 @@ import os import struct import tarfile -import warnings import zipfile import numpy as np @@ -136,6 +135,7 @@ def test_read_dta1(self, file, datapath): tm.assert_frame_equal(parsed, expected) + @pytest.mark.filterwarnings("always") def test_read_dta2(self, datapath): expected = DataFrame.from_records( [ @@ -174,14 +174,15 @@ def test_read_dta2(self, datapath): ) expected["yearly_date"] = expected["yearly_date"].astype("O") - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + with tm.assert_produces_warning(UserWarning): parsed_114 = self.read_dta( datapath("io", "data", "stata", "stata2_114.dta") ) + with tm.assert_produces_warning(UserWarning): parsed_115 = self.read_dta( datapath("io", "data", "stata", "stata2_115.dta") ) + with tm.assert_produces_warning(UserWarning): parsed_117 = self.read_dta( datapath("io", "data", "stata", "stata2_117.dta") ) @@ -190,12 +191,6 @@ def test_read_dta2(self, datapath): # datapath("io", "data", "stata", "stata2_113.dta") # ) - # Remove resource warnings - w = [x for x in w if x.category is UserWarning] - - # should get warning for each call to read_dta - assert len(w) == 3 - # buggy test because of the NaT comparison on certain platforms # Format 113 test fails since it does not support tc and tC formats # tm.assert_frame_equal(parsed_113, expected) @@ -460,11 +455,9 @@ def test_read_write_dta12(self, version): formatted = formatted.astype(np.int32) with tm.ensure_clean() as path: - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always", InvalidColumnName) + with tm.assert_produces_warning(InvalidColumnName): original.to_stata(path, convert_dates=None, version=version) # should get a warning for that format. - assert len(w) == 1 written_and_read_again = self.read_dta(path) @@ -1156,6 +1149,7 @@ def test_categorical_ordering(self, file, datapath): assert parsed[col].cat.ordered assert not parsed_unordered[col].cat.ordered + @pytest.mark.filterwarnings("ignore::UserWarning") @pytest.mark.parametrize( "file", [ @@ -1180,13 +1174,11 @@ def test_read_chunks_117( ): fname = datapath("io", "data", "stata", f"{file}.dta") - with warnings.catch_warnings(record=True): - warnings.simplefilter("always") - parsed = read_stata( - fname, - convert_categoricals=convert_categoricals, - convert_dates=convert_dates, - ) + parsed = read_stata( + fname, + convert_categoricals=convert_categoricals, + convert_dates=convert_dates, + ) with read_stata( fname, iterator=True, @@ -1195,12 +1187,10 @@ def test_read_chunks_117( ) as itr: pos = 0 for j in range(5): - with warnings.catch_warnings(record=True): - warnings.simplefilter("always") - try: - chunk = itr.read(chunksize) - except StopIteration: - break + try: + chunk = itr.read(chunksize) + except StopIteration: + break from_frame = parsed.iloc[pos : pos + chunksize, :].copy() from_frame = self._convert_categorical(from_frame) tm.assert_frame_equal( @@ -1249,6 +1239,7 @@ def test_iterator(self, datapath): from_chunks = pd.concat(itr) tm.assert_frame_equal(parsed, from_chunks) + @pytest.mark.filterwarnings("ignore::UserWarning") @pytest.mark.parametrize( "file", [ @@ -1273,13 +1264,11 @@ def test_read_chunks_115( fname = datapath("io", "data", "stata", f"{file}.dta") # Read the whole file - with warnings.catch_warnings(record=True): - warnings.simplefilter("always") - parsed = read_stata( - fname, - convert_categoricals=convert_categoricals, - convert_dates=convert_dates, - ) + parsed = read_stata( + fname, + convert_categoricals=convert_categoricals, + convert_dates=convert_dates, + ) # Compare to what we get when reading by chunk with read_stata( @@ -1290,12 +1279,10 @@ def test_read_chunks_115( ) as itr: pos = 0 for j in range(5): - with warnings.catch_warnings(record=True): - warnings.simplefilter("always") - try: - chunk = itr.read(chunksize) - except StopIteration: - break + try: + chunk = itr.read(chunksize) + except StopIteration: + break from_frame = parsed.iloc[pos : pos + chunksize, :].copy() from_frame = self._convert_categorical(from_frame) tm.assert_frame_equal( diff --git a/pandas/tests/plotting/frame/test_frame.py b/pandas/tests/plotting/frame/test_frame.py index c18e995d26d78..a7994f0bdf733 100644 --- a/pandas/tests/plotting/frame/test_frame.py +++ b/pandas/tests/plotting/frame/test_frame.py @@ -7,7 +7,6 @@ import itertools import re import string -import warnings import weakref import numpy as np @@ -1728,13 +1727,12 @@ def test_errorbar_plot_different_yerr_xerr_subplots(self, kind): @pytest.mark.xfail(reason="Iterator is consumed", raises=ValueError) def test_errorbar_plot_iterator(self): - with warnings.catch_warnings(): - d = {"x": np.arange(12), "y": np.arange(12, 0, -1)} - df = DataFrame(d) + d = {"x": np.arange(12), "y": np.arange(12, 0, -1)} + df = DataFrame(d) - # yerr is iterator - ax = _check_plot_works(df.plot, yerr=itertools.repeat(0.1, len(df))) - _check_has_errorbars(ax, xerr=0, yerr=2) + # yerr is iterator + ax = _check_plot_works(df.plot, yerr=itertools.repeat(0.1, len(df))) + _check_has_errorbars(ax, xerr=0, yerr=2) def test_errorbar_with_integer_column_names(self): # test with integer column names diff --git a/pandas/tests/plotting/frame/test_frame_subplots.py b/pandas/tests/plotting/frame/test_frame_subplots.py index a2b54c91693b8..bce00600f6615 100644 --- a/pandas/tests/plotting/frame/test_frame_subplots.py +++ b/pandas/tests/plotting/frame/test_frame_subplots.py @@ -1,7 +1,6 @@ """ Test cases for DataFrame.plot """ import string -import warnings import numpy as np import pytest @@ -336,9 +335,7 @@ def test_subplots_multiple_axes_2_dim(self, layout, exp_layout): np.random.default_rng(2).random((10, 4)), index=list(string.ascii_letters[:10]), ) - with warnings.catch_warnings(): - warnings.simplefilter("ignore", UserWarning) - + with tm.assert_produces_warning(UserWarning): returned = df.plot( subplots=True, ax=axes, layout=layout, sharex=False, sharey=False ) diff --git a/pandas/tests/reshape/concat/test_concat.py b/pandas/tests/reshape/concat/test_concat.py index 4491023125fb2..869bf3ace9492 100644 --- a/pandas/tests/reshape/concat/test_concat.py +++ b/pandas/tests/reshape/concat/test_concat.py @@ -5,18 +5,11 @@ from collections.abc import Iterator from datetime import datetime from decimal import Decimal -from warnings import ( - catch_warnings, - simplefilter, -) import numpy as np import pytest -from pandas.errors import ( - InvalidIndexError, - PerformanceWarning, -) +from pandas.errors import InvalidIndexError import pandas.util._test_decorators as td import pandas as pd @@ -531,15 +524,14 @@ def test_concat_no_unnecessary_upcast(dt, frame_or_series): @pytest.mark.parametrize("pdt", [Series, DataFrame]) @pytest.mark.parametrize("dt", np.sctypes["int"]) def test_concat_will_upcast(dt, pdt): - with catch_warnings(record=True): - dims = pdt().ndim - dfs = [ - pdt(np.array([1], dtype=dt, ndmin=dims)), - pdt(np.array([np.nan], ndmin=dims)), - pdt(np.array([5], dtype=dt, ndmin=dims)), - ] - x = concat(dfs) - assert x.values.dtype == "float64" + dims = pdt().ndim + dfs = [ + pdt(np.array([1], dtype=dt, ndmin=dims)), + pdt(np.array([np.nan], ndmin=dims)), + pdt(np.array([5], dtype=dt, ndmin=dims)), + ] + x = concat(dfs) + assert x.values.dtype == "float64" def test_concat_empty_and_non_empty_frame_regression(): @@ -596,10 +588,7 @@ def test_duplicate_keys_same_frame(): [(keys[0], "a"), (keys[0], "b"), (keys[1], "a"), (keys[1], "b")] ) expected = DataFrame(expected_values, columns=expected_columns) - with catch_warnings(): - # result.columns not sorted, resulting in performance warning - simplefilter("ignore", PerformanceWarning) - tm.assert_frame_equal(result, expected) + tm.assert_frame_equal(result, expected) @pytest.mark.parametrize( diff --git a/pandas/tests/series/accessors/test_cat_accessor.py b/pandas/tests/series/accessors/test_cat_accessor.py index 4cb3624309916..2d50b0f36904a 100644 --- a/pandas/tests/series/accessors/test_cat_accessor.py +++ b/pandas/tests/series/accessors/test_cat_accessor.py @@ -1,5 +1,3 @@ -import warnings - import numpy as np import pytest @@ -198,13 +196,18 @@ def test_dt_accessor_api_for_categorical(self, idx): ) for func, args, kwargs in func_defs: - with warnings.catch_warnings(): - if func == "to_period": - # dropping TZ - warnings.simplefilter("ignore", UserWarning) - if func == "to_pydatetime": - # deprecated to return Index[object] - warnings.simplefilter("ignore", FutureWarning) + warn_cls = [] + if func == "to_period" and getattr(idx, "tz", None) is not None: + # dropping TZ + warn_cls.append(UserWarning) + if func == "to_pydatetime": + # deprecated to return Index[object] + warn_cls.append(FutureWarning) + if warn_cls: + warn_cls = tuple(warn_cls) + else: + warn_cls = None + with tm.assert_produces_warning(warn_cls): res = getattr(cat.dt, func)(*args, **kwargs) exp = getattr(ser.dt, func)(*args, **kwargs) diff --git a/pandas/tests/test_expressions.py b/pandas/tests/test_expressions.py index e61539a9e97a7..17dfa95b66201 100644 --- a/pandas/tests/test_expressions.py +++ b/pandas/tests/test_expressions.py @@ -1,6 +1,5 @@ import operator import re -import warnings import numpy as np import pytest @@ -211,9 +210,7 @@ def test_invalid(self): result = expr._can_use_numexpr(operator.add, "+", array, array2, "evaluate") assert result - @pytest.mark.filterwarnings( - "ignore:invalid value encountered in true_divide:RuntimeWarning" - ) + @pytest.mark.filterwarnings("ignore:invalid value encountered in:RuntimeWarning") @pytest.mark.parametrize( "opname,op_str", [("add", "+"), ("sub", "-"), ("mul", "*"), ("truediv", "/"), ("pow", "**")], @@ -232,12 +229,9 @@ def testit(): op = getattr(operator, opname) - with warnings.catch_warnings(): - # array has 0s - msg = "invalid value encountered in divide|true_divide" - warnings.filterwarnings("ignore", msg, RuntimeWarning) - result = expr.evaluate(op, left, left, use_numexpr=True) - expected = expr.evaluate(op, left, left, use_numexpr=False) + # array has 0s + result = expr.evaluate(op, left, left, use_numexpr=True) + expected = expr.evaluate(op, left, left, use_numexpr=False) tm.assert_numpy_array_equal(result, expected) result = expr._can_use_numexpr(op, op_str, right, right, "evaluate") diff --git a/pandas/tests/test_nanops.py b/pandas/tests/test_nanops.py index 7e1a23022135e..76784ec726afe 100644 --- a/pandas/tests/test_nanops.py +++ b/pandas/tests/test_nanops.py @@ -1,5 +1,4 @@ from functools import partial -import warnings import numpy as np import pytest @@ -465,17 +464,16 @@ def test_nanmean(self, skipna): nanops.nanmean, np.mean, skipna, allow_obj=False, allow_date=False ) + @pytest.mark.filterwarnings("ignore::RuntimeWarning") def test_nanmedian(self, skipna): - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", RuntimeWarning) - self.check_funs( - nanops.nanmedian, - np.median, - skipna, - allow_complex=False, - allow_date=False, - allow_obj="convert", - ) + self.check_funs( + nanops.nanmedian, + np.median, + skipna, + allow_complex=False, + allow_date=False, + allow_obj="convert", + ) @pytest.mark.parametrize("ddof", range(3)) def test_nanvar(self, ddof, skipna): @@ -517,13 +515,12 @@ def test_nansem(self, ddof, skipna): ddof=ddof, ) + @pytest.mark.filterwarnings("ignore::RuntimeWarning") @pytest.mark.parametrize( "nan_op,np_op", [(nanops.nanmin, np.min), (nanops.nanmax, np.max)] ) def test_nanops_with_warnings(self, nan_op, np_op, skipna): - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", RuntimeWarning) - self.check_funs(nan_op, np_op, skipna, allow_obj=False) + self.check_funs(nan_op, np_op, skipna, allow_obj=False) def _argminmax_wrap(self, value, axis=None, func=None): res = func(value, axis) @@ -540,17 +537,15 @@ def _argminmax_wrap(self, value, axis=None, func=None): res = -1 return res + @pytest.mark.filterwarnings("ignore::RuntimeWarning") def test_nanargmax(self, skipna): - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", RuntimeWarning) - func = partial(self._argminmax_wrap, func=np.argmax) - self.check_funs(nanops.nanargmax, func, skipna, allow_obj=False) + func = partial(self._argminmax_wrap, func=np.argmax) + self.check_funs(nanops.nanargmax, func, skipna, allow_obj=False) + @pytest.mark.filterwarnings("ignore::RuntimeWarning") def test_nanargmin(self, skipna): - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", RuntimeWarning) - func = partial(self._argminmax_wrap, func=np.argmin) - self.check_funs(nanops.nanargmin, func, skipna, allow_obj=False) + func = partial(self._argminmax_wrap, func=np.argmin) + self.check_funs(nanops.nanargmin, func, skipna, allow_obj=False) def _skew_kurt_wrap(self, values, axis=None, func=None): if not isinstance(values.dtype.type, np.floating): diff --git a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py index 2ea3dae19a3e4..e74ff89b11581 100644 --- a/pandas/tests/util/test_deprecate_nonkeyword_arguments.py +++ b/pandas/tests/util/test_deprecate_nonkeyword_arguments.py @@ -3,7 +3,6 @@ """ import inspect -import warnings from pandas.util._decorators import deprecate_nonkeyword_arguments @@ -52,16 +51,12 @@ def test_four_arguments(): def test_three_arguments_with_name_in_warning(): - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + msg = ( + "Starting with pandas version 1.1 all arguments of f_add_inputs " + "except for the arguments 'a' and 'b' will be keyword-only." + ) + with tm.assert_produces_warning(FutureWarning, match=msg): assert f(6, 3, 3) == 12 - assert len(w) == 1 - for actual_warning in w: - assert actual_warning.category == FutureWarning - assert str(actual_warning.message) == ( - "Starting with pandas version 1.1 all arguments of f_add_inputs " - "except for the arguments 'a' and 'b' will be keyword-only." - ) @deprecate_nonkeyword_arguments(version="1.1") @@ -85,16 +80,12 @@ def test_three_arguments_default_allowed_args(): def test_three_positional_argument_with_warning_message_analysis(): - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + msg = ( + "Starting with pandas version 1.1 all arguments of g " + "except for the argument 'a' will be keyword-only." + ) + with tm.assert_produces_warning(FutureWarning, match=msg): assert g(6, 3, 3) == 12 - assert len(w) == 1 - for actual_warning in w: - assert actual_warning.category == FutureWarning - assert str(actual_warning.message) == ( - "Starting with pandas version 1.1 all arguments of g " - "except for the argument 'a' will be keyword-only." - ) @deprecate_nonkeyword_arguments(version="1.1") @@ -117,16 +108,9 @@ def test_one_positional_argument(): def test_one_positional_argument_with_warning_message_analysis(): - with warnings.catch_warnings(record=True) as w: - warnings.simplefilter("always") + msg = "Starting with pandas version 1.1 all arguments of h will be keyword-only." + with tm.assert_produces_warning(FutureWarning, match=msg): assert h(19) == 19 - assert len(w) == 1 - for actual_warning in w: - assert actual_warning.category == FutureWarning - assert str(actual_warning.message) == ( - "Starting with pandas version 1.1 all arguments " - "of h will be keyword-only." - ) @deprecate_nonkeyword_arguments(version="1.1") diff --git a/pandas/tests/window/test_apply.py b/pandas/tests/window/test_apply.py index d2114c425218c..6af5a41e96e0a 100644 --- a/pandas/tests/window/test_apply.py +++ b/pandas/tests/window/test_apply.py @@ -1,5 +1,3 @@ -import warnings - import numpy as np import pytest @@ -18,17 +16,15 @@ from pandas.tseries import offsets +# suppress warnings about empty slices, as we are deliberately testing +# with a 0-length Series +pytestmark = pytest.mark.filterwarnings( + "ignore:.*(empty slice|0 for slice).*:RuntimeWarning" +) + def f(x): - # suppress warnings about empty slices, as we are deliberately testing - # with a 0-length Series - with warnings.catch_warnings(): - warnings.filterwarnings( - "ignore", - message=".*(empty slice|0 for slice).*", - category=RuntimeWarning, - ) - return x[np.isfinite(x)].mean() + return x[np.isfinite(x)].mean() @pytest.mark.parametrize("bad_raw", [None, 1, 0]) diff --git a/pandas/tests/window/test_pairwise.py b/pandas/tests/window/test_pairwise.py index ec794e818edf1..8dac6d271510a 100644 --- a/pandas/tests/window/test_pairwise.py +++ b/pandas/tests/window/test_pairwise.py @@ -1,5 +1,3 @@ -import warnings - import numpy as np import pytest @@ -325,6 +323,7 @@ def test_pairwise_with_other( tm.assert_numpy_array_equal(result, expected, check_dtype=False) + @pytest.mark.filterwarnings("ignore:RuntimeWarning") @pytest.mark.parametrize( "f", [ @@ -344,13 +343,11 @@ def test_no_pairwise_with_other(self, pairwise_frames, pairwise_other_frame, f): else None ) if result is not None: - with warnings.catch_warnings(record=True): - warnings.simplefilter("ignore", RuntimeWarning) - # we can have int and str columns - expected_index = pairwise_frames.index.union(pairwise_other_frame.index) - expected_columns = pairwise_frames.columns.union( - pairwise_other_frame.columns - ) + # we can have int and str columns + expected_index = pairwise_frames.index.union(pairwise_other_frame.index) + expected_columns = pairwise_frames.columns.union( + pairwise_other_frame.columns + ) tm.assert_index_equal(result.index, expected_index) tm.assert_index_equal(result.columns, expected_columns) else: