Skip to content

Commit 6280eb3

Browse files
committed
use catch_warnings in test_window
1 parent db68195 commit 6280eb3

File tree

2 files changed

+40
-44
lines changed

2 files changed

+40
-44
lines changed

pandas/tests/io/test_sql.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import os
2626
import sys
2727

28-
import pytest
2928
import warnings
3029
import numpy as np
3130
import pandas as pd

pandas/tests/test_window.py

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import pytest
33
import sys
44
import warnings
5+
from warnings import catch_warnings
56

67
from datetime import datetime
78
from numpy.random import randn
@@ -291,8 +292,7 @@ def test_how_compat(self):
291292
for op in ['mean', 'sum', 'std', 'var', 'kurt', 'skew']:
292293
for t in ['rolling', 'expanding']:
293294

294-
with tm.assert_produces_warning(FutureWarning,
295-
check_stacklevel=False):
295+
with catch_warnings(record=True):
296296

297297
dfunc = getattr(pd, "{0}_{1}".format(t, op))
298298
if dfunc is None:
@@ -526,7 +526,7 @@ def setUp(self):
526526

527527
def test_deprecations(self):
528528

529-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
529+
with catch_warnings(record=True):
530530
mom.rolling_mean(np.ones(10), 3, center=True, axis=0)
531531
mom.rolling_mean(Series(np.ones(10)), 3, center=True, axis=0)
532532

@@ -791,7 +791,7 @@ def test_cmov_mean(self):
791791
xp = np.array([np.nan, np.nan, 9.962, 11.27, 11.564, 12.516, 12.818,
792792
12.952, np.nan, np.nan])
793793

794-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
794+
with catch_warnings(record=True):
795795
rs = mom.rolling_mean(vals, 5, center=True)
796796
tm.assert_almost_equal(xp, rs)
797797

@@ -808,7 +808,7 @@ def test_cmov_window(self):
808808
xp = np.array([np.nan, np.nan, 9.962, 11.27, 11.564, 12.516, 12.818,
809809
12.952, np.nan, np.nan])
810810

811-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
811+
with catch_warnings(record=True):
812812
rs = mom.rolling_window(vals, 5, 'boxcar', center=True)
813813
tm.assert_almost_equal(xp, rs)
814814

@@ -823,19 +823,19 @@ def test_cmov_window_corner(self):
823823
# all nan
824824
vals = np.empty(10, dtype=float)
825825
vals.fill(np.nan)
826-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
826+
with catch_warnings(record=True):
827827
rs = mom.rolling_window(vals, 5, 'boxcar', center=True)
828828
self.assertTrue(np.isnan(rs).all())
829829

830830
# empty
831831
vals = np.array([])
832-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
832+
with catch_warnings(record=True):
833833
rs = mom.rolling_window(vals, 5, 'boxcar', center=True)
834834
self.assertEqual(len(rs), 0)
835835

836836
# shorter than window
837837
vals = np.random.randn(5)
838-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
838+
with catch_warnings(record=True):
839839
rs = mom.rolling_window(vals, 10, 'boxcar')
840840
self.assertTrue(np.isnan(rs).all())
841841
self.assertEqual(len(rs), 5)
@@ -1014,16 +1014,16 @@ def test_cmov_window_special_linear_range(self):
10141014
tm.assert_series_equal(xp, rs)
10151015

10161016
def test_rolling_median(self):
1017-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1017+
with catch_warnings(record=True):
10181018
self._check_moment_func(mom.rolling_median, np.median,
10191019
name='median')
10201020

10211021
def test_rolling_min(self):
10221022

1023-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1023+
with catch_warnings(record=True):
10241024
self._check_moment_func(mom.rolling_min, np.min, name='min')
10251025

1026-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1026+
with catch_warnings(record=True):
10271027
a = np.array([1, 2, 3, 4, 5])
10281028
b = mom.rolling_min(a, window=100, min_periods=1)
10291029
tm.assert_almost_equal(b, np.ones(len(a)))
@@ -1033,10 +1033,10 @@ def test_rolling_min(self):
10331033

10341034
def test_rolling_max(self):
10351035

1036-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1036+
with catch_warnings(record=True):
10371037
self._check_moment_func(mom.rolling_max, np.max, name='max')
10381038

1039-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1039+
with catch_warnings(record=True):
10401040
a = np.array([1, 2, 3, 4, 5], dtype=np.float64)
10411041
b = mom.rolling_max(a, window=100, min_periods=1)
10421042
tm.assert_almost_equal(a, b)
@@ -1102,11 +1102,11 @@ def test_rolling_apply_out_of_bounds(self):
11021102
arr = np.arange(4)
11031103

11041104
# it works!
1105-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1105+
with catch_warnings(record=True):
11061106
result = mom.rolling_apply(arr, 10, np.sum)
11071107
self.assertTrue(isnull(result).all())
11081108

1109-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1109+
with catch_warnings(record=True):
11101110
result = mom.rolling_apply(arr, 10, np.sum, min_periods=1)
11111111
tm.assert_almost_equal(result, result)
11121112

@@ -1117,19 +1117,19 @@ def test_rolling_std(self):
11171117
name='std', ddof=0)
11181118

11191119
def test_rolling_std_1obs(self):
1120-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1120+
with catch_warnings(record=True):
11211121
result = mom.rolling_std(np.array([1., 2., 3., 4., 5.]),
11221122
1, min_periods=1)
11231123
expected = np.array([np.nan] * 5)
11241124
tm.assert_almost_equal(result, expected)
11251125

1126-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1126+
with catch_warnings(record=True):
11271127
result = mom.rolling_std(np.array([1., 2., 3., 4., 5.]),
11281128
1, min_periods=1, ddof=0)
11291129
expected = np.zeros(5)
11301130
tm.assert_almost_equal(result, expected)
11311131

1132-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1132+
with catch_warnings(record=True):
11331133
result = mom.rolling_std(np.array([np.nan, np.nan, 3., 4., 5.]),
11341134
3, min_periods=2)
11351135
self.assertTrue(np.isnan(result[2]))
@@ -1142,11 +1142,11 @@ def test_rolling_std_neg_sqrt(self):
11421142
a = np.array([0.0011448196318903589, 0.00028718669878572767,
11431143
0.00028718669878572767, 0.00028718669878572767,
11441144
0.00028718669878572767])
1145-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1145+
with catch_warnings(record=True):
11461146
b = mom.rolling_std(a, window=3)
11471147
self.assertTrue(np.isfinite(b[2:]).all())
11481148

1149-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1149+
with catch_warnings(record=True):
11501150
b = mom.ewmstd(a, span=3)
11511151
self.assertTrue(np.isfinite(b[2:]).all())
11521152

@@ -1184,25 +1184,25 @@ def test_fperr_robustness(self):
11841184
if sys.byteorder != "little":
11851185
arr = arr.byteswap().newbyteorder()
11861186

1187-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1187+
with catch_warnings(record=True):
11881188
result = mom.rolling_sum(arr, 2)
11891189
self.assertTrue((result[1:] >= 0).all())
11901190

1191-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1191+
with catch_warnings(record=True):
11921192
result = mom.rolling_mean(arr, 2)
11931193
self.assertTrue((result[1:] >= 0).all())
11941194

1195-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1195+
with catch_warnings(record=True):
11961196
result = mom.rolling_var(arr, 2)
11971197
self.assertTrue((result[1:] >= 0).all())
11981198

11991199
# #2527, ugh
12001200
arr = np.array([0.00012456, 0.0003, 0])
1201-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1201+
with catch_warnings(record=True):
12021202
result = mom.rolling_mean(arr, 1)
12031203
self.assertTrue(result[-1] >= 0)
12041204

1205-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1205+
with catch_warnings(record=True):
12061206
result = mom.rolling_mean(-arr, 1)
12071207
self.assertTrue(result[-1] <= 0)
12081208

@@ -1327,15 +1327,13 @@ def get_result(obj, window, min_periods=None, freq=None, center=False):
13271327

13281328
# catch a freq deprecation warning if freq is provided and not
13291329
# None
1330-
w = FutureWarning if freq is not None else None
1331-
with tm.assert_produces_warning(w, check_stacklevel=False):
1330+
with catch_warnings(record=True):
13321331
r = obj.rolling(window=window, min_periods=min_periods,
13331332
freq=freq, center=center)
13341333
return getattr(r, name)(**kwargs)
13351334

13361335
# check via the moments API
1337-
with tm.assert_produces_warning(FutureWarning,
1338-
check_stacklevel=False):
1336+
with catch_warnings(record=True):
13391337
return f(obj, window=window, min_periods=min_periods,
13401338
freq=freq, center=center, **kwargs)
13411339

@@ -1419,7 +1417,7 @@ def test_ewma(self):
14191417

14201418
arr = np.zeros(1000)
14211419
arr[5] = 1
1422-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1420+
with catch_warnings(record=True):
14231421
result = mom.ewma(arr, span=100, adjust=False).sum()
14241422
self.assertTrue(np.abs(result - 1) < 1e-2)
14251423

@@ -1506,7 +1504,7 @@ def test_ewmvol(self):
15061504
self._check_ew(mom.ewmvol, name='vol')
15071505

15081506
def test_ewma_span_com_args(self):
1509-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1507+
with catch_warnings(record=True):
15101508
A = mom.ewma(self.arr, com=9.5)
15111509
B = mom.ewma(self.arr, span=20)
15121510
tm.assert_almost_equal(A, B)
@@ -1515,7 +1513,7 @@ def test_ewma_span_com_args(self):
15151513
self.assertRaises(ValueError, mom.ewma, self.arr)
15161514

15171515
def test_ewma_halflife_arg(self):
1518-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1516+
with catch_warnings(record=True):
15191517
A = mom.ewma(self.arr, com=13.932726172912965)
15201518
B = mom.ewma(self.arr, halflife=10.0)
15211519
tm.assert_almost_equal(A, B)
@@ -1530,7 +1528,7 @@ def test_ewma_halflife_arg(self):
15301528

15311529
def test_ewma_alpha_old_api(self):
15321530
# GH 10789
1533-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1531+
with catch_warnings(record=True):
15341532
a = mom.ewma(self.arr, alpha=0.61722699889169674)
15351533
b = mom.ewma(self.arr, com=0.62014947789973052)
15361534
c = mom.ewma(self.arr, span=2.240298955799461)
@@ -1541,7 +1539,7 @@ def test_ewma_alpha_old_api(self):
15411539

15421540
def test_ewma_alpha_arg_old_api(self):
15431541
# GH 10789
1544-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1542+
with catch_warnings(record=True):
15451543
self.assertRaises(ValueError, mom.ewma, self.arr)
15461544
self.assertRaises(ValueError, mom.ewma, self.arr,
15471545
com=10.0, alpha=0.5)
@@ -1598,13 +1596,12 @@ def test_ew_empty_arrays(self):
15981596

15991597
funcs = [mom.ewma, mom.ewmvol, mom.ewmvar]
16001598
for f in funcs:
1601-
with tm.assert_produces_warning(FutureWarning,
1602-
check_stacklevel=False):
1599+
with catch_warnings(record=True):
16031600
result = f(arr, 3)
16041601
tm.assert_almost_equal(result, arr)
16051602

16061603
def _check_ew(self, func, name=None):
1607-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
1604+
with catch_warnings(record=True):
16081605
self._check_ew_ndarray(func, name=name)
16091606
self._check_ew_structures(func, name=name)
16101607

@@ -2870,7 +2867,7 @@ def test_rolling_max_gh6297(self):
28702867

28712868
expected = Series([1.0, 2.0, 6.0, 4.0, 5.0],
28722869
index=[datetime(1975, 1, i, 0) for i in range(1, 6)])
2873-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2870+
with catch_warnings(record=True):
28742871
x = series.rolling(window=1, freq='D').max()
28752872
tm.assert_series_equal(expected, x)
28762873

@@ -2889,22 +2886,22 @@ def test_rolling_max_how_resample(self):
28892886
# Default how should be max
28902887
expected = Series([0.0, 1.0, 2.0, 3.0, 20.0],
28912888
index=[datetime(1975, 1, i, 0) for i in range(1, 6)])
2892-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2889+
with catch_warnings(record=True):
28932890
x = series.rolling(window=1, freq='D').max()
28942891
tm.assert_series_equal(expected, x)
28952892

28962893
# Now specify median (10.0)
28972894
expected = Series([0.0, 1.0, 2.0, 3.0, 10.0],
28982895
index=[datetime(1975, 1, i, 0) for i in range(1, 6)])
2899-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2896+
with catch_warnings(record=True):
29002897
x = series.rolling(window=1, freq='D').max(how='median')
29012898
tm.assert_series_equal(expected, x)
29022899

29032900
# Now specify mean (4+10+20)/3
29042901
v = (4.0 + 10.0 + 20.0) / 3.0
29052902
expected = Series([0.0, 1.0, 2.0, 3.0, v],
29062903
index=[datetime(1975, 1, i, 0) for i in range(1, 6)])
2907-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2904+
with catch_warnings(record=True):
29082905
x = series.rolling(window=1, freq='D').max(how='mean')
29092906
tm.assert_series_equal(expected, x)
29102907

@@ -2923,7 +2920,7 @@ def test_rolling_min_how_resample(self):
29232920
# Default how should be min
29242921
expected = Series([0.0, 1.0, 2.0, 3.0, 4.0],
29252922
index=[datetime(1975, 1, i, 0) for i in range(1, 6)])
2926-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2923+
with catch_warnings(record=True):
29272924
r = series.rolling(window=1, freq='D')
29282925
tm.assert_series_equal(expected, r.min())
29292926

@@ -2942,7 +2939,7 @@ def test_rolling_median_how_resample(self):
29422939
# Default how should be median
29432940
expected = Series([0.0, 1.0, 2.0, 3.0, 10],
29442941
index=[datetime(1975, 1, i, 0) for i in range(1, 6)])
2945-
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
2942+
with catch_warnings(record=True):
29462943
x = series.rolling(window=1, freq='D').median()
29472944
tm.assert_series_equal(expected, x)
29482945

0 commit comments

Comments
 (0)