Skip to content

TST: check_stacklevel=True in various tests #44449

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 2 commits into from
Nov 14, 2021
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
4 changes: 1 addition & 3 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,9 +1198,7 @@ def test_nuiscance_columns():
)
tm.assert_frame_equal(result, expected)

with tm.assert_produces_warning(
FutureWarning, match="Select only valid", check_stacklevel=False
):
with tm.assert_produces_warning(FutureWarning, match="Select only valid"):
result = df.agg("sum")
expected = Series([6, 6.0, "foobarbaz"], index=["A", "B", "C"])
tm.assert_series_equal(result, expected)
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/arrays/test_datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,9 +783,7 @@ def test_to_perioddelta(self, datetime_index, freqstr):
with tm.assert_produces_warning(FutureWarning, match=msg):
# Deprecation GH#34853
expected = dti.to_perioddelta(freq=freqstr)
with tm.assert_produces_warning(
FutureWarning, match=msg, check_stacklevel=False
):
with tm.assert_produces_warning(FutureWarning, match=msg):
# stacklevel is chosen to be "correct" for DatetimeIndex, not
# DatetimeArray
result = arr.to_perioddelta(freq=freqstr)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/dtypes/cast/test_promote.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def test_maybe_promote_any_with_datetime64(
# Casting date to dt64 is deprecated
warn = FutureWarning

with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
with tm.assert_produces_warning(warn, match=msg):
# stacklevel is chosen to make sense when called from higher-level functions
_check_promote(dtype, fill_value, expected_dtype, exp_val_for_scalar)

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ def test_setitem_mask_categorical(self):
df = DataFrame({"cats": catsf, "values": valuesf}, index=idxf)

exp_fancy = exp_multi_row.copy()
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
# issue #37643 inplace kwarg deprecated
return_value = exp_fancy["cats"].cat.set_categories(
["a", "b", "c"], inplace=True
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/frame/methods/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,7 @@ def test_merge_join_different_levels(self):
columns = ["a", "b", ("a", ""), ("c", "c1")]
expected = DataFrame(columns=columns, data=[[1, 11, 0, 44], [0, 22, 1, 33]])
msg = "merging between different levels is deprecated"
with tm.assert_produces_warning(
FutureWarning, match=msg, check_stacklevel=False
):
with tm.assert_produces_warning(FutureWarning, match=msg):
# stacklevel is chosen to be correct for pd.merge, not DataFrame.join
result = df1.join(df2, on="a")
tm.assert_frame_equal(result, expected)
Expand Down
14 changes: 6 additions & 8 deletions pandas/tests/frame/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2664,27 +2664,27 @@ def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture, pydt):
expected = DataFrame({0: [ts_naive]})
tm.assert_frame_equal(result, expected)

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
result = DataFrame({0: ts}, index=[0], dtype="datetime64[ns]")
tm.assert_frame_equal(result, expected)

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
result = DataFrame([ts], dtype="datetime64[ns]")
tm.assert_frame_equal(result, expected)

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
result = DataFrame(np.array([ts], dtype=object), dtype="datetime64[ns]")
tm.assert_frame_equal(result, expected)

with tm.assert_produces_warning(FutureWarning):
result = DataFrame(ts, index=[0], columns=[0], dtype="datetime64[ns]")
tm.assert_frame_equal(result, expected)

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
df = DataFrame([Series([ts])], dtype="datetime64[ns]")
tm.assert_frame_equal(result, expected)

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
df = DataFrame([[ts]], columns=[0], dtype="datetime64[ns]")
tm.assert_equal(df, expected)

Expand Down Expand Up @@ -2946,9 +2946,7 @@ def test_tzaware_data_tznaive_dtype(self, constructor):
ts = Timestamp("2019", tz=tz)
ts_naive = Timestamp("2019")

with tm.assert_produces_warning(
FutureWarning, match="Data is timezone-aware", check_stacklevel=False
):
with tm.assert_produces_warning(FutureWarning, match="Data is timezone-aware"):
result = constructor(ts, dtype="M8[ns]")

assert np.all(result.dtypes == "M8[ns]")
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/aggregate/test_cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def test_cython_agg_nothing_to_agg():

frame = DataFrame({"a": np.random.randint(0, 5, 50), "b": ["foo", "bar"] * 25})

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
result = frame[["b"]].groupby(frame["a"]).mean()
expected = DataFrame([], index=frame["a"].sort_values().drop_duplicates())
tm.assert_frame_equal(result, expected)
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/groupby/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ def test_averages(self, df, method):
],
)

with tm.assert_produces_warning(
FutureWarning, match="Dropping invalid", check_stacklevel=False
):
with tm.assert_produces_warning(FutureWarning, match="Dropping invalid"):
result = getattr(gb, method)(numeric_only=False)
tm.assert_frame_equal(result.reindex_like(expected), expected)

Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,9 +904,7 @@ def test_omit_nuisance_agg(df, agg_function):

def test_omit_nuisance_warnings(df):
# GH 38815
with tm.assert_produces_warning(
FutureWarning, filter_level="always", check_stacklevel=False
):
with tm.assert_produces_warning(FutureWarning, filter_level="always"):
grouped = df.groupby("A")
result = grouped.skew()
expected = df.loc[:, ["A", "C", "D"]].groupby("A").skew()
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,9 +516,7 @@ def test_get_loc_tz_aware(self):
freq="5s",
)
key = Timestamp("2019-12-12 10:19:25", tz="US/Eastern")
with tm.assert_produces_warning(
FutureWarning, match="deprecated", check_stacklevel=False
):
with tm.assert_produces_warning(FutureWarning, match="deprecated"):
result = dti.get_loc(key, method="nearest")
assert result == 7433

Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/indexes/datetimes/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ def test_datetimeindex_accessors4(self):
assert dti.is_month_start[0] == 1

def test_datetimeindex_accessors5(self):
with tm.assert_produces_warning(
FutureWarning, match="The 'freq' argument", check_stacklevel=False
):
with tm.assert_produces_warning(FutureWarning, match="The 'freq' argument"):
tests = [
(Timestamp("2013-06-01", freq="M").is_month_start, 1),
(Timestamp("2013-06-01", freq="BM").is_month_start, 0),
Expand Down
4 changes: 1 addition & 3 deletions pandas/tests/indexes/datetimes/test_scalar_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ def test_dti_timestamp_fields(self, field):
expected = getattr(idx, field)[-1]

warn = FutureWarning if field.startswith("is_") else None
with tm.assert_produces_warning(
warn, match="Timestamp.freq is deprecated", check_stacklevel=False
):
with tm.assert_produces_warning(warn, match="Timestamp.freq is deprecated"):
result = getattr(Timestamp(idx[-1]), field)
assert result == expected

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2630,7 +2630,7 @@ def test_loc_slice_disallows_positional():
with pytest.raises(TypeError, match=msg):
df.loc[1:3, 1]

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
# GH#31840 deprecated incorrect behavior
df.loc[1:3, 1] = 2

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/io/formats/style/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,7 @@ def test_applymap_subset_multiindex(self, slice_):
col = MultiIndex.from_product([["x", "y"], ["A", "B"]])
df = DataFrame(np.random.rand(4, 4), columns=col, index=idx)

with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
with tm.assert_produces_warning(warn, match=msg):
df.style.applymap(lambda x: "color: red;", subset=slice_).to_html()

def test_applymap_subset_multiindex_code(self):
Expand Down
6 changes: 3 additions & 3 deletions pandas/tests/resample/test_deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def test_deprecating_on_loffset_and_base():
# not checking the stacklevel for .groupby().resample() because it's complicated to
# reconcile it with the stacklevel for Series.resample() and DataFrame.resample();
# see GH #37603
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
df.groupby("a").resample("3T", base=0).sum()
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
df.groupby("a").resample("3T", loffset="0s").sum()
msg = "'offset' and 'base' cannot be present at the same time"
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
with pytest.raises(ValueError, match=msg):
df.groupby("a").resample("3T", base=0, offset=0).sum()

Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/reshape/merge/test_join.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def test_join_dups(self):
dta = x.merge(y, left_index=True, right_index=True).merge(
z, left_index=True, right_index=True, how="outer"
)
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
dta = dta.merge(w, left_index=True, right_index=True)
expected = concat([x, y, z, w], axis=1)
expected.columns = ["x_x", "y_x", "x_y", "y_y", "x_x", "y_x", "x_y", "y_y"]
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/accessors/test_cat_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_cat_accessor(self):

exp = Categorical(["a", "b", np.nan, "a"], categories=["b", "a"])

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
# issue #37643 inplace kwarg deprecated
return_value = ser.cat.set_categories(["b", "a"], inplace=True)

Expand Down Expand Up @@ -88,7 +88,7 @@ def test_cat_accessor_updates_on_inplace(self):
return_value = ser.drop(0, inplace=True)
assert return_value is None

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
return_value = ser.cat.remove_unused_categories(inplace=True)

assert return_value is None
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ def test_series_ops_name_retention(
# GH#37374 logical ops behaving as set ops deprecated
warn = FutureWarning if is_rlogical and box is Index else None
msg = "operating as a set operation is deprecated"
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
with tm.assert_produces_warning(warn, match=msg):
# stacklevel is correct for Index op, not reversed op
result = op(left, right)

Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1645,12 +1645,12 @@ def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture, pydt):
ts = ts.to_pydatetime()
ts_naive = Timestamp("2019")

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
result = Series([ts], dtype="datetime64[ns]")
expected = Series([ts_naive])
tm.assert_series_equal(result, expected)

with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
result = Series(np.array([ts], dtype=object), dtype="datetime64[ns]")
tm.assert_series_equal(result, expected)

Expand Down
8 changes: 2 additions & 6 deletions pandas/tests/series/test_logical_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,7 @@ def test_reversed_xor_with_index_returns_index(self):
tm.assert_index_equal(result, expected)

expected = Index.symmetric_difference(idx2, ser)
with tm.assert_produces_warning(
FutureWarning, match=msg, check_stacklevel=False
):
with tm.assert_produces_warning(FutureWarning, match=msg):
result = idx2 ^ ser
tm.assert_index_equal(result, expected)

Expand Down Expand Up @@ -337,9 +335,7 @@ def test_reverse_ops_with_index(self, op, expected):
idx = Index([False, True])

msg = "operating as a set operation"
with tm.assert_produces_warning(
FutureWarning, match=msg, check_stacklevel=False
):
with tm.assert_produces_warning(FutureWarning, match=msg):
# behaving as set ops is deprecated, will become logical ops
result = op(ser, idx)
tm.assert_index_equal(result, expected)
Expand Down
12 changes: 6 additions & 6 deletions pandas/tests/test_expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,32 +287,32 @@ def test_bool_ops_warn_on_arithmetic(self, op_str, opname):
return

with tm.use_numexpr(True, min_elements=5):
with tm.assert_produces_warning(check_stacklevel=False):
with tm.assert_produces_warning():
r = f(df, df)
e = fe(df, df)
tm.assert_frame_equal(r, e)

with tm.assert_produces_warning(check_stacklevel=False):
with tm.assert_produces_warning():
r = f(df.a, df.b)
e = fe(df.a, df.b)
tm.assert_series_equal(r, e)

with tm.assert_produces_warning(check_stacklevel=False):
with tm.assert_produces_warning():
r = f(df.a, True)
e = fe(df.a, True)
tm.assert_series_equal(r, e)

with tm.assert_produces_warning(check_stacklevel=False):
with tm.assert_produces_warning():
r = f(False, df.a)
e = fe(False, df.a)
tm.assert_series_equal(r, e)

with tm.assert_produces_warning(check_stacklevel=False):
with tm.assert_produces_warning():
r = f(False, df)
e = fe(False, df)
tm.assert_frame_equal(r, e)

with tm.assert_produces_warning(check_stacklevel=False):
with tm.assert_produces_warning():
r = f(df, True)
e = fe(df, True)
tm.assert_frame_equal(r, e)
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/window/test_rolling.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ def test_rolling_count_default_min_periods_with_null_values(frame_or_series):
expected_counts = [1.0, 2.0, 3.0, 2.0, 2.0, 2.0, 3.0]

# GH 31302
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
with tm.assert_produces_warning(FutureWarning):
result = frame_or_series(values).rolling(3).count()
expected = frame_or_series(expected_counts)
tm.assert_equal(result, expected)
Expand Down