Skip to content

CLN/BUG: Allow assert_produces_warning to raise during an exception #50614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion pandas/_libs/tslibs/offsets.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ cdef class BaseOffset:
_attributes = tuple(["n", "normalize"])
_use_relativedelta = False
_adjust_dst = True
_deprecations = frozenset(["isAnchored", "onOffset"])

# cdef readonly:
# int64_t n
Expand Down
32 changes: 16 additions & 16 deletions pandas/_testing/_warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,22 @@ class for all warnings. To raise multiple types of exceptions,

with warnings.catch_warnings(record=True) as w:
warnings.simplefilter(filter_level)
yield w

if expected_warning:
expected_warning = cast(Type[Warning], expected_warning)
_assert_caught_expected_warning(
caught_warnings=w,
expected_warning=expected_warning,
match=match,
check_stacklevel=check_stacklevel,
)

if raise_on_extra_warnings:
_assert_caught_no_extra_warnings(
caught_warnings=w,
expected_warning=expected_warning,
)
try:
yield w
finally:
if expected_warning:
expected_warning = cast(Type[Warning], expected_warning)
_assert_caught_expected_warning(
caught_warnings=w,
expected_warning=expected_warning,
match=match,
check_stacklevel=check_stacklevel,
)
if raise_on_extra_warnings:
_assert_caught_no_extra_warnings(
caught_warnings=w,
expected_warning=expected_warning,
)


def maybe_produces_warning(warning: type[Warning], condition: bool, **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2182,8 +2182,8 @@ def objects_to_datetime64ns(

def maybe_convert_dtype(data, copy: bool, tz: tzinfo | None = None):
"""
Convert data based on dtype conventions, issuing deprecation warnings
or errors where appropriate.
Convert data based on dtype conventions, issuing
errors where appropriate.

Parameters
----------
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/apply/test_invalid_arg.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def test_transform_wont_agg_series(string_series, func):
warn = RuntimeWarning if func[0] == "sqrt" else None
warn_msg = "invalid value encountered in sqrt"
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(warn, match=warn_msg):
with tm.assert_produces_warning(warn, match=warn_msg, check_stacklevel=False):
string_series.transform(func)


Expand Down
20 changes: 15 additions & 5 deletions pandas/tests/extension/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,9 @@ def test_groupby_extension_transform(self, data_for_grouping, request):
)
)
with tm.maybe_produces_warning(
PerformanceWarning, pa_version_under7p0, check_stacklevel=False
PerformanceWarning,
pa_version_under7p0 and not pa.types.is_duration(pa_dtype),
check_stacklevel=False,
):
super().test_groupby_extension_transform(data_for_grouping)

Expand All @@ -543,7 +545,9 @@ def test_groupby_extension_apply(
)
)
with tm.maybe_produces_warning(
PerformanceWarning, pa_version_under7p0, check_stacklevel=False
PerformanceWarning,
pa_version_under7p0 and not pa.types.is_duration(pa_dtype),
check_stacklevel=False,
):
super().test_groupby_extension_apply(data_for_grouping, groupby_apply_op)

Expand All @@ -565,7 +569,9 @@ def test_groupby_extension_agg(self, as_index, data_for_grouping, request):
)
)
with tm.maybe_produces_warning(
PerformanceWarning, pa_version_under7p0, check_stacklevel=False
PerformanceWarning,
pa_version_under7p0 and not pa.types.is_duration(pa_dtype),
check_stacklevel=False,
):
super().test_groupby_extension_agg(as_index, data_for_grouping)

Expand Down Expand Up @@ -804,7 +810,9 @@ def test_value_counts_with_normalize(self, data, request):
)
)
with tm.maybe_produces_warning(
PerformanceWarning, pa_version_under7p0, check_stacklevel=False
PerformanceWarning,
pa_version_under7p0 and not pa.types.is_duration(pa_dtype),
check_stacklevel=False,
):
super().test_value_counts_with_normalize(data)

Expand Down Expand Up @@ -923,7 +931,9 @@ def test_sort_values_frame(self, data_for_sorting, ascending, request):
)
)
with tm.maybe_produces_warning(
PerformanceWarning, pa_version_under7p0, check_stacklevel=False
PerformanceWarning,
pa_version_under7p0 and not pa.types.is_duration(pa_dtype),
check_stacklevel=False,
):
super().test_sort_values_frame(data_for_sorting, ascending)

Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/extension/test_sparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,7 @@ class TestParsing(BaseSparseTests, base.BaseParsingTests):
def test_EA_types(self, engine, data):
expected_msg = r".*must implement _from_sequence_of_strings.*"
with pytest.raises(NotImplementedError, match=expected_msg):
with tm.assert_produces_warning(FutureWarning, match="astype from"):
super().test_EA_types(engine, data)
super().test_EA_types(engine, data)


class TestNoNumericAccumulations(base.BaseAccumulateTests):
Expand Down
6 changes: 2 additions & 4 deletions pandas/tests/frame/indexing/test_getitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ def test_getitem_listlike(self, idx_type, levels, float_frame):

idx = idx_type(keys + [missing])
with pytest.raises(KeyError, match="not in index"):
with tm.assert_produces_warning(FutureWarning):
frame[idx]
frame[idx]

def test_getitem_iloc_generator(self):
# GH#39614
Expand Down Expand Up @@ -357,8 +356,7 @@ def test_getitem_boolean_frame_unaligned_with_duplicate_columns(self, df_dup_col
df = df_dup_cols
msg = "cannot reindex on an axis with duplicate labels"
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
df[df.A > 6]
df[df.A > 6]

def test_getitem_boolean_series_with_duplicate_columns(self, df_dup_cols):
# boolean indexing
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ def test_setitem_error_msmgs(self):
)
msg = "cannot reindex on an axis with duplicate labels"
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
df["newcol"] = ser
df["newcol"] = ser

# GH 4107, more descriptive error message
df = DataFrame(np.random.randint(0, 2, (4, 4)), columns=["a", "b", "c", "d"])
Expand Down
12 changes: 4 additions & 8 deletions pandas/tests/frame/methods/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,8 +808,7 @@ def test_reindex_dups(self):
# reindex fails
msg = "cannot reindex on an axis with duplicate labels"
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
df.reindex(index=list(range(len(df))))
df.reindex(index=list(range(len(df))))

def test_reindex_with_duplicate_columns(self):

Expand All @@ -819,11 +818,9 @@ def test_reindex_with_duplicate_columns(self):
)
msg = "cannot reindex on an axis with duplicate labels"
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
df.reindex(columns=["bar"])
df.reindex(columns=["bar"])
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
df.reindex(columns=["bar", "foo"])
df.reindex(columns=["bar", "foo"])

def test_reindex_axis_style(self):
# https://github.com/pandas-dev/pandas/issues/12392
Expand Down Expand Up @@ -1091,8 +1088,7 @@ def test_reindex_with_categoricalindex(self):
# passed duplicate indexers are not allowed
msg = "cannot reindex on an axis with duplicate labels"
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
df2.reindex(["a", "b"])
df2.reindex(["a", "b"])

# args NotImplemented ATM
msg = r"argument {} is not implemented for CategoricalIndex\.reindex"
Expand Down
8 changes: 3 additions & 5 deletions pandas/tests/frame/methods/test_sort_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,12 @@ def test_sort_index_and_reconstruction_doc_example(self):
assert result.index.is_monotonic_increasing
tm.assert_frame_equal(result, expected)

# FIXME: the FutureWarning is issued on a setitem-with-expansion
# which will *not* change behavior, so should not get a warning.
@pytest.mark.filterwarnings("ignore:.*will attempt to set.*:FutureWarning")
def test_sort_index_non_existent_label_multiindex(self):
# GH#12261
df = DataFrame(0, columns=[], index=MultiIndex.from_product([[], []]))
df.loc["b", "2"] = 1
df.loc["a", "3"] = 1
with tm.assert_produces_warning(None):
df.loc["b", "2"] = 1
df.loc["a", "3"] = 1
result = df.sort_index().index.is_monotonic_increasing
assert result is True

Expand Down
10 changes: 7 additions & 3 deletions pandas/tests/frame/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ def test_frame_with_frame_reindex(self):
],
ids=lambda x: x.__name__,
)
def test_binop_other(self, op, value, dtype, switch_numexpr_min_elements):
def test_binop_other(self, op, value, dtype, switch_numexpr_min_elements, request):

skip = {
(operator.truediv, "bool"),
Expand Down Expand Up @@ -1099,10 +1099,14 @@ def test_binop_other(self, op, value, dtype, switch_numexpr_min_elements):
msg = None
elif dtype == "complex128":
msg = "ufunc 'remainder' not supported for the input types"
warn = UserWarning # "evaluating in Python space because ..."
elif op is operator.sub:
msg = "numpy boolean subtract, the `-` operator, is "
warn = UserWarning # "evaluating in Python space because ..."
if (
dtype == "bool"
and expr.USE_NUMEXPR
and switch_numexpr_min_elements == 0
):
warn = UserWarning # "evaluating in Python space because ..."
else:
msg = (
f"cannot perform __{op.__name__}__ with this "
Expand Down
5 changes: 1 addition & 4 deletions pandas/tests/groupby/test_categorical.py
Original file line number Diff line number Diff line change
Expand Up @@ -1893,10 +1893,7 @@ def test_category_order_transformer(
df = df.set_index(keys)
args = get_groupby_method_args(transformation_func, df)
gb = df.groupby(keys, as_index=as_index, sort=sort, observed=observed)
msg = "is deprecated and will be removed in a future version"
warn = FutureWarning if transformation_func == "tshift" else None
with tm.assert_produces_warning(warn, match=msg):
op_result = getattr(gb, transformation_func)(*args)
op_result = getattr(gb, transformation_func)(*args)
result = op_result.index.get_level_values("a").categories
expected = Index([1, 4, 3, 2])
tm.assert_index_equal(result, expected)
Expand Down
11 changes: 3 additions & 8 deletions pandas/tests/groupby/transform/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -1362,10 +1362,7 @@ def test_null_group_str_transformer(request, dropna, transformation_func):
# ngroup/cumcount always returns a Series as it counts the groups, not values
expected = expected["B"].rename(None)

warn = FutureWarning if transformation_func in ("backfill", "pad") else None
msg = f"{transformation_func} is deprecated"
with tm.assert_produces_warning(warn, match=msg):
result = gb.transform(transformation_func, *args)
result = gb.transform(transformation_func, *args)

tm.assert_equal(result, expected)

Expand Down Expand Up @@ -1411,7 +1408,7 @@ def test_null_group_str_reducer_series(request, dropna, reduction_func):
tm.assert_series_equal(result, expected)


def test_null_group_str_transformer_series(request, dropna, transformation_func):
def test_null_group_str_transformer_series(dropna, transformation_func):
# GH 17093
ser = Series([1, 2, 2], index=[1, 2, 3])
args = get_groupby_method_args(transformation_func, ser)
Expand All @@ -1432,9 +1429,7 @@ def test_null_group_str_transformer_series(request, dropna, transformation_func)
buffer.append(Series([np.nan], index=[3], dtype=dtype))
expected = concat(buffer)

warn = FutureWarning if transformation_func in ("backfill", "pad") else None
msg = f"{transformation_func} is deprecated"
with tm.assert_produces_warning(warn, match=msg):
with tm.assert_produces_warning(None):
result = gb.transform(transformation_func, *args)

tm.assert_equal(result, expected)
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexes/interval/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ def test_getitem_2d_deprecated(self, simple_index):
# GH#30588 multi-dim indexing is deprecated, but raising is also acceptable
idx = simple_index
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
with tm.assert_produces_warning(FutureWarning):
idx[:, None]
idx[:, None]
with pytest.raises(ValueError, match="multi-dimensional indexing not allowed"):
# GH#44051
idx[True]
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexes/multi/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ def test_reindex_non_unique():

msg = "cannot handle a non-unique multi-index!"
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(FutureWarning, match="non-unique"):
a.reindex(new_idx)
a.reindex(new_idx)


@pytest.mark.parametrize("values", [[["a"], ["x"]], [[], []]])
Expand Down
10 changes: 1 addition & 9 deletions pandas/tests/indexes/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import pandas as pd
from pandas import (
CategoricalIndex,
DatetimeIndex,
MultiIndex,
PeriodIndex,
RangeIndex,
Expand Down Expand Up @@ -389,14 +388,7 @@ def test_astype_preserves_name(self, index, dtype):
index.name = "idx"

warn = None
if (
isinstance(index, DatetimeIndex)
and index.tz is not None
and dtype == "datetime64[ns]"
):
# This astype is deprecated in favor of tz_localize
warn = FutureWarning
elif index.dtype.kind == "c" and dtype in ["float64", "int64", "uint64"]:
if index.dtype.kind == "c" and dtype in ["float64", "int64", "uint64"]:
# imaginary components discarded
warn = np.ComplexWarning

Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ def test_setitem_ndarray_1d_2(self):

msg = "Must have equal len keys and value when setting with an iterable"
with pytest.raises(ValueError, match=msg):
with tm.assert_produces_warning(FutureWarning, match="label-based"):
df[2:5] = np.arange(1, 4) * 1j
df[2:5] = np.arange(1, 4) * 1j

def test_getitem_ndarray_3d(
self, index, frame_or_series, indexer_sli, using_array_manager
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/io/parser/test_parse_dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,9 +924,7 @@ def test_parse_dates_custom_euro_format(all_parsers, kwargs):
tm.assert_frame_equal(df, expected)
else:
msg = "got an unexpected keyword argument 'day_first'"
with pytest.raises(TypeError, match=msg), tm.assert_produces_warning(
FutureWarning
):
with pytest.raises(TypeError, match=msg):
parser.read_csv(
StringIO(data),
names=["time", "Q", "NTU"],
Expand Down
3 changes: 1 addition & 2 deletions pandas/tests/io/test_stata.py
Original file line number Diff line number Diff line change
Expand Up @@ -1725,8 +1725,7 @@ def test_invalid_file_not_written(self, version):
r"ordinal not in range\(128\)"
)
with pytest.raises(UnicodeEncodeError, match=f"{msg1}|{msg2}"):
with tm.assert_produces_warning(ResourceWarning):
df.to_stata(path)
df.to_stata(path)

def test_strl_latin1(self):
# GH 23573, correct GSO data to reflect correct size
Expand Down
Loading