Skip to content

Commit ce12646

Browse files
authored
TST: check_stacklevel=True in various tests (#44449)
1 parent 253d26a commit ce12646

22 files changed

+37
-59
lines changed

pandas/tests/apply/test_frame_apply.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1198,9 +1198,7 @@ def test_nuiscance_columns():
11981198
)
11991199
tm.assert_frame_equal(result, expected)
12001200

1201-
with tm.assert_produces_warning(
1202-
FutureWarning, match="Select only valid", check_stacklevel=False
1203-
):
1201+
with tm.assert_produces_warning(FutureWarning, match="Select only valid"):
12041202
result = df.agg("sum")
12051203
expected = Series([6, 6.0, "foobarbaz"], index=["A", "B", "C"])
12061204
tm.assert_series_equal(result, expected)

pandas/tests/arrays/test_datetimelike.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -783,9 +783,7 @@ def test_to_perioddelta(self, datetime_index, freqstr):
783783
with tm.assert_produces_warning(FutureWarning, match=msg):
784784
# Deprecation GH#34853
785785
expected = dti.to_perioddelta(freq=freqstr)
786-
with tm.assert_produces_warning(
787-
FutureWarning, match=msg, check_stacklevel=False
788-
):
786+
with tm.assert_produces_warning(FutureWarning, match=msg):
789787
# stacklevel is chosen to be "correct" for DatetimeIndex, not
790788
# DatetimeArray
791789
result = arr.to_perioddelta(freq=freqstr)

pandas/tests/dtypes/cast/test_promote.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ def test_maybe_promote_any_with_datetime64(
411411
# Casting date to dt64 is deprecated
412412
warn = FutureWarning
413413

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

pandas/tests/frame/indexing/test_setitem.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ def test_setitem_mask_categorical(self):
949949
df = DataFrame({"cats": catsf, "values": valuesf}, index=idxf)
950950

951951
exp_fancy = exp_multi_row.copy()
952-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
952+
with tm.assert_produces_warning(FutureWarning):
953953
# issue #37643 inplace kwarg deprecated
954954
return_value = exp_fancy["cats"].cat.set_categories(
955955
["a", "b", "c"], inplace=True

pandas/tests/frame/methods/test_join.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -344,9 +344,7 @@ def test_merge_join_different_levels(self):
344344
columns = ["a", "b", ("a", ""), ("c", "c1")]
345345
expected = DataFrame(columns=columns, data=[[1, 11, 0, 44], [0, 22, 1, 33]])
346346
msg = "merging between different levels is deprecated"
347-
with tm.assert_produces_warning(
348-
FutureWarning, match=msg, check_stacklevel=False
349-
):
347+
with tm.assert_produces_warning(FutureWarning, match=msg):
350348
# stacklevel is chosen to be correct for pd.merge, not DataFrame.join
351349
result = df1.join(df2, on="a")
352350
tm.assert_frame_equal(result, expected)

pandas/tests/frame/test_constructors.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -2664,27 +2664,27 @@ def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture, pydt):
26642664
expected = DataFrame({0: [ts_naive]})
26652665
tm.assert_frame_equal(result, expected)
26662666

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

2671-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2671+
with tm.assert_produces_warning(FutureWarning):
26722672
result = DataFrame([ts], dtype="datetime64[ns]")
26732673
tm.assert_frame_equal(result, expected)
26742674

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

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

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

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

@@ -2946,9 +2946,7 @@ def test_tzaware_data_tznaive_dtype(self, constructor):
29462946
ts = Timestamp("2019", tz=tz)
29472947
ts_naive = Timestamp("2019")
29482948

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

29542952
assert np.all(result.dtypes == "M8[ns]")

pandas/tests/groupby/aggregate/test_cython.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def test_cython_agg_nothing_to_agg():
9797

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

100-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
100+
with tm.assert_produces_warning(FutureWarning):
101101
result = frame[["b"]].groupby(frame["a"]).mean()
102102
expected = DataFrame([], index=frame["a"].sort_values().drop_duplicates())
103103
tm.assert_frame_equal(result, expected)

pandas/tests/groupby/test_function.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,7 @@ def test_averages(self, df, method):
154154
],
155155
)
156156

157-
with tm.assert_produces_warning(
158-
FutureWarning, match="Dropping invalid", check_stacklevel=False
159-
):
157+
with tm.assert_produces_warning(FutureWarning, match="Dropping invalid"):
160158
result = getattr(gb, method)(numeric_only=False)
161159
tm.assert_frame_equal(result.reindex_like(expected), expected)
162160

pandas/tests/groupby/test_groupby.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -904,9 +904,7 @@ def test_omit_nuisance_agg(df, agg_function):
904904

905905
def test_omit_nuisance_warnings(df):
906906
# GH 38815
907-
with tm.assert_produces_warning(
908-
FutureWarning, filter_level="always", check_stacklevel=False
909-
):
907+
with tm.assert_produces_warning(FutureWarning, filter_level="always"):
910908
grouped = df.groupby("A")
911909
result = grouped.skew()
912910
expected = df.loc[:, ["A", "C", "D"]].groupby("A").skew()

pandas/tests/indexes/datetimes/test_indexing.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,7 @@ def test_get_loc_tz_aware(self):
516516
freq="5s",
517517
)
518518
key = Timestamp("2019-12-12 10:19:25", tz="US/Eastern")
519-
with tm.assert_produces_warning(
520-
FutureWarning, match="deprecated", check_stacklevel=False
521-
):
519+
with tm.assert_produces_warning(FutureWarning, match="deprecated"):
522520
result = dti.get_loc(key, method="nearest")
523521
assert result == 7433
524522

pandas/tests/indexes/datetimes/test_misc.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ def test_datetimeindex_accessors4(self):
142142
assert dti.is_month_start[0] == 1
143143

144144
def test_datetimeindex_accessors5(self):
145-
with tm.assert_produces_warning(
146-
FutureWarning, match="The 'freq' argument", check_stacklevel=False
147-
):
145+
with tm.assert_produces_warning(FutureWarning, match="The 'freq' argument"):
148146
tests = [
149147
(Timestamp("2013-06-01", freq="M").is_month_start, 1),
150148
(Timestamp("2013-06-01", freq="BM").is_month_start, 0),

pandas/tests/indexes/datetimes/test_scalar_compat.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,7 @@ def test_dti_timestamp_fields(self, field):
6565
expected = getattr(idx, field)[-1]
6666

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

pandas/tests/indexing/test_loc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,7 @@ def test_loc_slice_disallows_positional():
26302630
with pytest.raises(TypeError, match=msg):
26312631
df.loc[1:3, 1]
26322632

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

pandas/tests/io/formats/style/test_style.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ def test_applymap_subset_multiindex(self, slice_):
749749
col = MultiIndex.from_product([["x", "y"], ["A", "B"]])
750750
df = DataFrame(np.random.rand(4, 4), columns=col, index=idx)
751751

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

755755
def test_applymap_subset_multiindex_code(self):

pandas/tests/resample/test_deprecated.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ def test_deprecating_on_loffset_and_base():
6363
# not checking the stacklevel for .groupby().resample() because it's complicated to
6464
# reconcile it with the stacklevel for Series.resample() and DataFrame.resample();
6565
# see GH #37603
66-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
66+
with tm.assert_produces_warning(FutureWarning):
6767
df.groupby("a").resample("3T", base=0).sum()
68-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
68+
with tm.assert_produces_warning(FutureWarning):
6969
df.groupby("a").resample("3T", loffset="0s").sum()
7070
msg = "'offset' and 'base' cannot be present at the same time"
71-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
71+
with tm.assert_produces_warning(FutureWarning):
7272
with pytest.raises(ValueError, match=msg):
7373
df.groupby("a").resample("3T", base=0, offset=0).sum()
7474

pandas/tests/reshape/merge/test_join.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ def test_join_dups(self):
630630
dta = x.merge(y, left_index=True, right_index=True).merge(
631631
z, left_index=True, right_index=True, how="outer"
632632
)
633-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
633+
with tm.assert_produces_warning(FutureWarning):
634634
dta = dta.merge(w, left_index=True, right_index=True)
635635
expected = concat([x, y, z, w], axis=1)
636636
expected.columns = ["x_x", "y_x", "x_y", "y_y", "x_x", "y_x", "x_y", "y_y"]

pandas/tests/series/accessors/test_cat_accessor.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_cat_accessor(self):
5151

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

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

@@ -88,7 +88,7 @@ def test_cat_accessor_updates_on_inplace(self):
8888
return_value = ser.drop(0, inplace=True)
8989
assert return_value is None
9090

91-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
91+
with tm.assert_produces_warning(FutureWarning):
9292
return_value = ser.cat.remove_unused_categories(inplace=True)
9393

9494
assert return_value is None

pandas/tests/series/test_arithmetic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ def test_series_ops_name_retention(
790790
# GH#37374 logical ops behaving as set ops deprecated
791791
warn = FutureWarning if is_rlogical and box is Index else None
792792
msg = "operating as a set operation is deprecated"
793-
with tm.assert_produces_warning(warn, match=msg, check_stacklevel=False):
793+
with tm.assert_produces_warning(warn, match=msg):
794794
# stacklevel is correct for Index op, not reversed op
795795
result = op(left, right)
796796

pandas/tests/series/test_constructors.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1645,12 +1645,12 @@ def test_constructor_data_aware_dtype_naive(self, tz_aware_fixture, pydt):
16451645
ts = ts.to_pydatetime()
16461646
ts_naive = Timestamp("2019")
16471647

1648-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1648+
with tm.assert_produces_warning(FutureWarning):
16491649
result = Series([ts], dtype="datetime64[ns]")
16501650
expected = Series([ts_naive])
16511651
tm.assert_series_equal(result, expected)
16521652

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

pandas/tests/series/test_logical_ops.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,7 @@ def test_reversed_xor_with_index_returns_index(self):
279279
tm.assert_index_equal(result, expected)
280280

281281
expected = Index.symmetric_difference(idx2, ser)
282-
with tm.assert_produces_warning(
283-
FutureWarning, match=msg, check_stacklevel=False
284-
):
282+
with tm.assert_produces_warning(FutureWarning, match=msg):
285283
result = idx2 ^ ser
286284
tm.assert_index_equal(result, expected)
287285

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

339337
msg = "operating as a set operation"
340-
with tm.assert_produces_warning(
341-
FutureWarning, match=msg, check_stacklevel=False
342-
):
338+
with tm.assert_produces_warning(FutureWarning, match=msg):
343339
# behaving as set ops is deprecated, will become logical ops
344340
result = op(ser, idx)
345341
tm.assert_index_equal(result, expected)

pandas/tests/test_expressions.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -287,32 +287,32 @@ def test_bool_ops_warn_on_arithmetic(self, op_str, opname):
287287
return
288288

289289
with tm.use_numexpr(True, min_elements=5):
290-
with tm.assert_produces_warning(check_stacklevel=False):
290+
with tm.assert_produces_warning():
291291
r = f(df, df)
292292
e = fe(df, df)
293293
tm.assert_frame_equal(r, e)
294294

295-
with tm.assert_produces_warning(check_stacklevel=False):
295+
with tm.assert_produces_warning():
296296
r = f(df.a, df.b)
297297
e = fe(df.a, df.b)
298298
tm.assert_series_equal(r, e)
299299

300-
with tm.assert_produces_warning(check_stacklevel=False):
300+
with tm.assert_produces_warning():
301301
r = f(df.a, True)
302302
e = fe(df.a, True)
303303
tm.assert_series_equal(r, e)
304304

305-
with tm.assert_produces_warning(check_stacklevel=False):
305+
with tm.assert_produces_warning():
306306
r = f(False, df.a)
307307
e = fe(False, df.a)
308308
tm.assert_series_equal(r, e)
309309

310-
with tm.assert_produces_warning(check_stacklevel=False):
310+
with tm.assert_produces_warning():
311311
r = f(False, df)
312312
e = fe(False, df)
313313
tm.assert_frame_equal(r, e)
314314

315-
with tm.assert_produces_warning(check_stacklevel=False):
315+
with tm.assert_produces_warning():
316316
r = f(df, True)
317317
e = fe(df, True)
318318
tm.assert_frame_equal(r, e)

pandas/tests/window/test_rolling.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ def test_rolling_count_default_min_periods_with_null_values(frame_or_series):
697697
expected_counts = [1.0, 2.0, 3.0, 2.0, 2.0, 2.0, 3.0]
698698

699699
# GH 31302
700-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
700+
with tm.assert_produces_warning(FutureWarning):
701701
result = frame_or_series(values).rolling(3).count()
702702
expected = frame_or_series(expected_counts)
703703
tm.assert_equal(result, expected)

0 commit comments

Comments
 (0)