2
2
import pytest
3
3
import sys
4
4
import warnings
5
+ from warnings import catch_warnings
5
6
6
7
from datetime import datetime
7
8
from numpy .random import randn
@@ -291,8 +292,7 @@ def test_how_compat(self):
291
292
for op in ['mean' , 'sum' , 'std' , 'var' , 'kurt' , 'skew' ]:
292
293
for t in ['rolling' , 'expanding' ]:
293
294
294
- with tm .assert_produces_warning (FutureWarning ,
295
- check_stacklevel = False ):
295
+ with catch_warnings (record = True ):
296
296
297
297
dfunc = getattr (pd , "{0}_{1}" .format (t , op ))
298
298
if dfunc is None :
@@ -526,7 +526,7 @@ def setUp(self):
526
526
527
527
def test_deprecations (self ):
528
528
529
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
529
+ with catch_warnings ( record = True ):
530
530
mom .rolling_mean (np .ones (10 ), 3 , center = True , axis = 0 )
531
531
mom .rolling_mean (Series (np .ones (10 )), 3 , center = True , axis = 0 )
532
532
@@ -791,7 +791,7 @@ def test_cmov_mean(self):
791
791
xp = np .array ([np .nan , np .nan , 9.962 , 11.27 , 11.564 , 12.516 , 12.818 ,
792
792
12.952 , np .nan , np .nan ])
793
793
794
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
794
+ with catch_warnings ( record = True ):
795
795
rs = mom .rolling_mean (vals , 5 , center = True )
796
796
tm .assert_almost_equal (xp , rs )
797
797
@@ -808,7 +808,7 @@ def test_cmov_window(self):
808
808
xp = np .array ([np .nan , np .nan , 9.962 , 11.27 , 11.564 , 12.516 , 12.818 ,
809
809
12.952 , np .nan , np .nan ])
810
810
811
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
811
+ with catch_warnings ( record = True ):
812
812
rs = mom .rolling_window (vals , 5 , 'boxcar' , center = True )
813
813
tm .assert_almost_equal (xp , rs )
814
814
@@ -823,19 +823,19 @@ def test_cmov_window_corner(self):
823
823
# all nan
824
824
vals = np .empty (10 , dtype = float )
825
825
vals .fill (np .nan )
826
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
826
+ with catch_warnings ( record = True ):
827
827
rs = mom .rolling_window (vals , 5 , 'boxcar' , center = True )
828
828
self .assertTrue (np .isnan (rs ).all ())
829
829
830
830
# empty
831
831
vals = np .array ([])
832
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
832
+ with catch_warnings ( record = True ):
833
833
rs = mom .rolling_window (vals , 5 , 'boxcar' , center = True )
834
834
self .assertEqual (len (rs ), 0 )
835
835
836
836
# shorter than window
837
837
vals = np .random .randn (5 )
838
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
838
+ with catch_warnings ( record = True ):
839
839
rs = mom .rolling_window (vals , 10 , 'boxcar' )
840
840
self .assertTrue (np .isnan (rs ).all ())
841
841
self .assertEqual (len (rs ), 5 )
@@ -1014,16 +1014,16 @@ def test_cmov_window_special_linear_range(self):
1014
1014
tm .assert_series_equal (xp , rs )
1015
1015
1016
1016
def test_rolling_median (self ):
1017
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1017
+ with catch_warnings ( record = True ):
1018
1018
self ._check_moment_func (mom .rolling_median , np .median ,
1019
1019
name = 'median' )
1020
1020
1021
1021
def test_rolling_min (self ):
1022
1022
1023
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1023
+ with catch_warnings ( record = True ):
1024
1024
self ._check_moment_func (mom .rolling_min , np .min , name = 'min' )
1025
1025
1026
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1026
+ with catch_warnings ( record = True ):
1027
1027
a = np .array ([1 , 2 , 3 , 4 , 5 ])
1028
1028
b = mom .rolling_min (a , window = 100 , min_periods = 1 )
1029
1029
tm .assert_almost_equal (b , np .ones (len (a )))
@@ -1033,10 +1033,10 @@ def test_rolling_min(self):
1033
1033
1034
1034
def test_rolling_max (self ):
1035
1035
1036
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1036
+ with catch_warnings ( record = True ):
1037
1037
self ._check_moment_func (mom .rolling_max , np .max , name = 'max' )
1038
1038
1039
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1039
+ with catch_warnings ( record = True ):
1040
1040
a = np .array ([1 , 2 , 3 , 4 , 5 ], dtype = np .float64 )
1041
1041
b = mom .rolling_max (a , window = 100 , min_periods = 1 )
1042
1042
tm .assert_almost_equal (a , b )
@@ -1102,11 +1102,11 @@ def test_rolling_apply_out_of_bounds(self):
1102
1102
arr = np .arange (4 )
1103
1103
1104
1104
# it works!
1105
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1105
+ with catch_warnings ( record = True ):
1106
1106
result = mom .rolling_apply (arr , 10 , np .sum )
1107
1107
self .assertTrue (isnull (result ).all ())
1108
1108
1109
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1109
+ with catch_warnings ( record = True ):
1110
1110
result = mom .rolling_apply (arr , 10 , np .sum , min_periods = 1 )
1111
1111
tm .assert_almost_equal (result , result )
1112
1112
@@ -1117,19 +1117,19 @@ def test_rolling_std(self):
1117
1117
name = 'std' , ddof = 0 )
1118
1118
1119
1119
def test_rolling_std_1obs (self ):
1120
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1120
+ with catch_warnings ( record = True ):
1121
1121
result = mom .rolling_std (np .array ([1. , 2. , 3. , 4. , 5. ]),
1122
1122
1 , min_periods = 1 )
1123
1123
expected = np .array ([np .nan ] * 5 )
1124
1124
tm .assert_almost_equal (result , expected )
1125
1125
1126
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1126
+ with catch_warnings ( record = True ):
1127
1127
result = mom .rolling_std (np .array ([1. , 2. , 3. , 4. , 5. ]),
1128
1128
1 , min_periods = 1 , ddof = 0 )
1129
1129
expected = np .zeros (5 )
1130
1130
tm .assert_almost_equal (result , expected )
1131
1131
1132
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1132
+ with catch_warnings ( record = True ):
1133
1133
result = mom .rolling_std (np .array ([np .nan , np .nan , 3. , 4. , 5. ]),
1134
1134
3 , min_periods = 2 )
1135
1135
self .assertTrue (np .isnan (result [2 ]))
@@ -1142,11 +1142,11 @@ def test_rolling_std_neg_sqrt(self):
1142
1142
a = np .array ([0.0011448196318903589 , 0.00028718669878572767 ,
1143
1143
0.00028718669878572767 , 0.00028718669878572767 ,
1144
1144
0.00028718669878572767 ])
1145
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1145
+ with catch_warnings ( record = True ):
1146
1146
b = mom .rolling_std (a , window = 3 )
1147
1147
self .assertTrue (np .isfinite (b [2 :]).all ())
1148
1148
1149
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1149
+ with catch_warnings ( record = True ):
1150
1150
b = mom .ewmstd (a , span = 3 )
1151
1151
self .assertTrue (np .isfinite (b [2 :]).all ())
1152
1152
@@ -1184,25 +1184,25 @@ def test_fperr_robustness(self):
1184
1184
if sys .byteorder != "little" :
1185
1185
arr = arr .byteswap ().newbyteorder ()
1186
1186
1187
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1187
+ with catch_warnings ( record = True ):
1188
1188
result = mom .rolling_sum (arr , 2 )
1189
1189
self .assertTrue ((result [1 :] >= 0 ).all ())
1190
1190
1191
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1191
+ with catch_warnings ( record = True ):
1192
1192
result = mom .rolling_mean (arr , 2 )
1193
1193
self .assertTrue ((result [1 :] >= 0 ).all ())
1194
1194
1195
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1195
+ with catch_warnings ( record = True ):
1196
1196
result = mom .rolling_var (arr , 2 )
1197
1197
self .assertTrue ((result [1 :] >= 0 ).all ())
1198
1198
1199
1199
# #2527, ugh
1200
1200
arr = np .array ([0.00012456 , 0.0003 , 0 ])
1201
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1201
+ with catch_warnings ( record = True ):
1202
1202
result = mom .rolling_mean (arr , 1 )
1203
1203
self .assertTrue (result [- 1 ] >= 0 )
1204
1204
1205
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1205
+ with catch_warnings ( record = True ):
1206
1206
result = mom .rolling_mean (- arr , 1 )
1207
1207
self .assertTrue (result [- 1 ] <= 0 )
1208
1208
@@ -1327,15 +1327,13 @@ def get_result(obj, window, min_periods=None, freq=None, center=False):
1327
1327
1328
1328
# catch a freq deprecation warning if freq is provided and not
1329
1329
# 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 ):
1332
1331
r = obj .rolling (window = window , min_periods = min_periods ,
1333
1332
freq = freq , center = center )
1334
1333
return getattr (r , name )(** kwargs )
1335
1334
1336
1335
# check via the moments API
1337
- with tm .assert_produces_warning (FutureWarning ,
1338
- check_stacklevel = False ):
1336
+ with catch_warnings (record = True ):
1339
1337
return f (obj , window = window , min_periods = min_periods ,
1340
1338
freq = freq , center = center , ** kwargs )
1341
1339
@@ -1419,7 +1417,7 @@ def test_ewma(self):
1419
1417
1420
1418
arr = np .zeros (1000 )
1421
1419
arr [5 ] = 1
1422
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1420
+ with catch_warnings ( record = True ):
1423
1421
result = mom .ewma (arr , span = 100 , adjust = False ).sum ()
1424
1422
self .assertTrue (np .abs (result - 1 ) < 1e-2 )
1425
1423
@@ -1506,7 +1504,7 @@ def test_ewmvol(self):
1506
1504
self ._check_ew (mom .ewmvol , name = 'vol' )
1507
1505
1508
1506
def test_ewma_span_com_args (self ):
1509
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1507
+ with catch_warnings ( record = True ):
1510
1508
A = mom .ewma (self .arr , com = 9.5 )
1511
1509
B = mom .ewma (self .arr , span = 20 )
1512
1510
tm .assert_almost_equal (A , B )
@@ -1515,7 +1513,7 @@ def test_ewma_span_com_args(self):
1515
1513
self .assertRaises (ValueError , mom .ewma , self .arr )
1516
1514
1517
1515
def test_ewma_halflife_arg (self ):
1518
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1516
+ with catch_warnings ( record = True ):
1519
1517
A = mom .ewma (self .arr , com = 13.932726172912965 )
1520
1518
B = mom .ewma (self .arr , halflife = 10.0 )
1521
1519
tm .assert_almost_equal (A , B )
@@ -1530,7 +1528,7 @@ def test_ewma_halflife_arg(self):
1530
1528
1531
1529
def test_ewma_alpha_old_api (self ):
1532
1530
# GH 10789
1533
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1531
+ with catch_warnings ( record = True ):
1534
1532
a = mom .ewma (self .arr , alpha = 0.61722699889169674 )
1535
1533
b = mom .ewma (self .arr , com = 0.62014947789973052 )
1536
1534
c = mom .ewma (self .arr , span = 2.240298955799461 )
@@ -1541,7 +1539,7 @@ def test_ewma_alpha_old_api(self):
1541
1539
1542
1540
def test_ewma_alpha_arg_old_api (self ):
1543
1541
# GH 10789
1544
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1542
+ with catch_warnings ( record = True ):
1545
1543
self .assertRaises (ValueError , mom .ewma , self .arr )
1546
1544
self .assertRaises (ValueError , mom .ewma , self .arr ,
1547
1545
com = 10.0 , alpha = 0.5 )
@@ -1598,13 +1596,12 @@ def test_ew_empty_arrays(self):
1598
1596
1599
1597
funcs = [mom .ewma , mom .ewmvol , mom .ewmvar ]
1600
1598
for f in funcs :
1601
- with tm .assert_produces_warning (FutureWarning ,
1602
- check_stacklevel = False ):
1599
+ with catch_warnings (record = True ):
1603
1600
result = f (arr , 3 )
1604
1601
tm .assert_almost_equal (result , arr )
1605
1602
1606
1603
def _check_ew (self , func , name = None ):
1607
- with tm . assert_produces_warning ( FutureWarning , check_stacklevel = False ):
1604
+ with catch_warnings ( record = True ):
1608
1605
self ._check_ew_ndarray (func , name = name )
1609
1606
self ._check_ew_structures (func , name = name )
1610
1607
@@ -2870,7 +2867,7 @@ def test_rolling_max_gh6297(self):
2870
2867
2871
2868
expected = Series ([1.0 , 2.0 , 6.0 , 4.0 , 5.0 ],
2872
2869
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 ):
2874
2871
x = series .rolling (window = 1 , freq = 'D' ).max ()
2875
2872
tm .assert_series_equal (expected , x )
2876
2873
@@ -2889,22 +2886,22 @@ def test_rolling_max_how_resample(self):
2889
2886
# Default how should be max
2890
2887
expected = Series ([0.0 , 1.0 , 2.0 , 3.0 , 20.0 ],
2891
2888
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 ):
2893
2890
x = series .rolling (window = 1 , freq = 'D' ).max ()
2894
2891
tm .assert_series_equal (expected , x )
2895
2892
2896
2893
# Now specify median (10.0)
2897
2894
expected = Series ([0.0 , 1.0 , 2.0 , 3.0 , 10.0 ],
2898
2895
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 ):
2900
2897
x = series .rolling (window = 1 , freq = 'D' ).max (how = 'median' )
2901
2898
tm .assert_series_equal (expected , x )
2902
2899
2903
2900
# Now specify mean (4+10+20)/3
2904
2901
v = (4.0 + 10.0 + 20.0 ) / 3.0
2905
2902
expected = Series ([0.0 , 1.0 , 2.0 , 3.0 , v ],
2906
2903
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 ):
2908
2905
x = series .rolling (window = 1 , freq = 'D' ).max (how = 'mean' )
2909
2906
tm .assert_series_equal (expected , x )
2910
2907
@@ -2923,7 +2920,7 @@ def test_rolling_min_how_resample(self):
2923
2920
# Default how should be min
2924
2921
expected = Series ([0.0 , 1.0 , 2.0 , 3.0 , 4.0 ],
2925
2922
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 ):
2927
2924
r = series .rolling (window = 1 , freq = 'D' )
2928
2925
tm .assert_series_equal (expected , r .min ())
2929
2926
@@ -2942,7 +2939,7 @@ def test_rolling_median_how_resample(self):
2942
2939
# Default how should be median
2943
2940
expected = Series ([0.0 , 1.0 , 2.0 , 3.0 , 10 ],
2944
2941
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 ):
2946
2943
x = series .rolling (window = 1 , freq = 'D' ).median ()
2947
2944
tm .assert_series_equal (expected , x )
2948
2945
0 commit comments