-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
CLN: Use more pytest idioms in test_momemts_ewm.py #36801
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,21 +7,15 @@ | |
import pandas._testing as tm | ||
|
||
|
||
def check_ew(name=None, preserve_nan=False, series=None, frame=None, nan_locs=None): | ||
series_result = getattr(series.ewm(com=10), name)() | ||
def test_ewma(series, frame): | ||
series_result = series.ewm(com=10).mean() | ||
assert isinstance(series_result, Series) | ||
|
||
frame_result = getattr(frame.ewm(com=10), name)() | ||
frame_result = frame.ewm(com=10).mean() | ||
assert type(frame_result) == DataFrame | ||
|
||
result = getattr(series.ewm(com=10), name)() | ||
if preserve_nan: | ||
assert result[nan_locs].isna().all() | ||
|
||
|
||
def test_ewma(series, frame, nan_locs): | ||
check_ew(name="mean", frame=frame, series=series, nan_locs=nan_locs) | ||
|
||
def test_ewma_adjust(): | ||
vals = pd.Series(np.zeros(1000)) | ||
vals[5] = 1 | ||
result = vals.ewm(span=100, adjust=False).mean().sum() | ||
|
@@ -53,63 +47,162 @@ def test_ewma_nan_handling(): | |
result = s.ewm(com=5).mean() | ||
tm.assert_series_equal(result, Series([np.nan] * 2 + [1.0] * 4)) | ||
|
||
# GH 7603 | ||
s0 = Series([np.nan, 1.0, 101.0]) | ||
s1 = Series([1.0, np.nan, 101.0]) | ||
s2 = Series([np.nan, 1.0, np.nan, np.nan, 101.0, np.nan]) | ||
s3 = Series([1.0, np.nan, 101.0, 50.0]) | ||
com = 2.0 | ||
alpha = 1.0 / (1.0 + com) | ||
|
||
def simple_wma(s, w): | ||
return (s.multiply(w).cumsum() / w.cumsum()).fillna(method="ffill") | ||
|
||
for (s, adjust, ignore_na, w) in [ | ||
(s0, True, False, [np.nan, (1.0 - alpha), 1.0]), | ||
(s0, True, True, [np.nan, (1.0 - alpha), 1.0]), | ||
(s0, False, False, [np.nan, (1.0 - alpha), alpha]), | ||
(s0, False, True, [np.nan, (1.0 - alpha), alpha]), | ||
(s1, True, False, [(1.0 - alpha) ** 2, np.nan, 1.0]), | ||
(s1, True, True, [(1.0 - alpha), np.nan, 1.0]), | ||
(s1, False, False, [(1.0 - alpha) ** 2, np.nan, alpha]), | ||
(s1, False, True, [(1.0 - alpha), np.nan, alpha]), | ||
(s2, True, False, [np.nan, (1.0 - alpha) ** 3, np.nan, np.nan, 1.0, np.nan]), | ||
(s2, True, True, [np.nan, (1.0 - alpha), np.nan, np.nan, 1.0, np.nan]), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i'm not sure that the parameterisation is clearer. maybe parameterise s, adjust and ignore_na independently (maybe with fixtures) and create a maybe use a class just for namespacing and use class level fixtures. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll consider doing this change in a follow up PR. The main motivation with this pass is to ensure that parameterized test failures are more easily visible within pytest |
||
|
||
@pytest.mark.parametrize( | ||
"s, adjust, ignore_na, w", | ||
[ | ||
( | ||
Series([np.nan, 1.0, 101.0]), | ||
True, | ||
False, | ||
[np.nan, (1.0 - (1.0 / (1.0 + 2.0))), 1.0], | ||
), | ||
( | ||
Series([np.nan, 1.0, 101.0]), | ||
True, | ||
True, | ||
[np.nan, (1.0 - (1.0 / (1.0 + 2.0))), 1.0], | ||
), | ||
( | ||
Series([np.nan, 1.0, 101.0]), | ||
False, | ||
False, | ||
[np.nan, (1.0 - (1.0 / (1.0 + 2.0))), (1.0 / (1.0 + 2.0))], | ||
), | ||
( | ||
Series([np.nan, 1.0, 101.0]), | ||
False, | ||
True, | ||
[np.nan, (1.0 - (1.0 / (1.0 + 2.0))), (1.0 / (1.0 + 2.0))], | ||
), | ||
( | ||
Series([1.0, np.nan, 101.0]), | ||
True, | ||
False, | ||
[(1.0 - (1.0 / (1.0 + 2.0))) ** 2, np.nan, 1.0], | ||
), | ||
( | ||
s2, | ||
Series([1.0, np.nan, 101.0]), | ||
True, | ||
True, | ||
[(1.0 - (1.0 / (1.0 + 2.0))), np.nan, 1.0], | ||
), | ||
( | ||
Series([1.0, np.nan, 101.0]), | ||
False, | ||
False, | ||
[np.nan, (1.0 - alpha) ** 3, np.nan, np.nan, alpha, np.nan], | ||
[(1.0 - (1.0 / (1.0 + 2.0))) ** 2, np.nan, (1.0 / (1.0 + 2.0))], | ||
), | ||
(s2, False, True, [np.nan, (1.0 - alpha), np.nan, np.nan, alpha, np.nan]), | ||
(s3, True, False, [(1.0 - alpha) ** 3, np.nan, (1.0 - alpha), 1.0]), | ||
(s3, True, True, [(1.0 - alpha) ** 2, np.nan, (1.0 - alpha), 1.0]), | ||
( | ||
s3, | ||
Series([1.0, np.nan, 101.0]), | ||
False, | ||
True, | ||
[(1.0 - (1.0 / (1.0 + 2.0))), np.nan, (1.0 / (1.0 + 2.0))], | ||
), | ||
( | ||
Series([np.nan, 1.0, np.nan, np.nan, 101.0, np.nan]), | ||
True, | ||
False, | ||
[np.nan, (1.0 - (1.0 / (1.0 + 2.0))) ** 3, np.nan, np.nan, 1.0, np.nan], | ||
), | ||
( | ||
Series([np.nan, 1.0, np.nan, np.nan, 101.0, np.nan]), | ||
True, | ||
True, | ||
[np.nan, (1.0 - (1.0 / (1.0 + 2.0))), np.nan, np.nan, 1.0, np.nan], | ||
), | ||
( | ||
Series([np.nan, 1.0, np.nan, np.nan, 101.0, np.nan]), | ||
False, | ||
False, | ||
[ | ||
(1.0 - alpha) ** 3, | ||
np.nan, | ||
(1.0 - alpha) * alpha, | ||
alpha * ((1.0 - alpha) ** 2 + alpha), | ||
(1.0 - (1.0 / (1.0 + 2.0))) ** 3, | ||
np.nan, | ||
np.nan, | ||
(1.0 / (1.0 + 2.0)), | ||
np.nan, | ||
], | ||
), | ||
(s3, False, True, [(1.0 - alpha) ** 2, np.nan, (1.0 - alpha) * alpha, alpha]), | ||
]: | ||
expected = simple_wma(s, Series(w)) | ||
result = s.ewm(com=com, adjust=adjust, ignore_na=ignore_na).mean() | ||
( | ||
Series([np.nan, 1.0, np.nan, np.nan, 101.0, np.nan]), | ||
False, | ||
True, | ||
[ | ||
np.nan, | ||
(1.0 - (1.0 / (1.0 + 2.0))), | ||
np.nan, | ||
np.nan, | ||
(1.0 / (1.0 + 2.0)), | ||
np.nan, | ||
], | ||
), | ||
( | ||
Series([1.0, np.nan, 101.0, 50.0]), | ||
True, | ||
False, | ||
[ | ||
(1.0 - (1.0 / (1.0 + 2.0))) ** 3, | ||
np.nan, | ||
(1.0 - (1.0 / (1.0 + 2.0))), | ||
1.0, | ||
], | ||
), | ||
( | ||
Series([1.0, np.nan, 101.0, 50.0]), | ||
True, | ||
True, | ||
[ | ||
(1.0 - (1.0 / (1.0 + 2.0))) ** 2, | ||
np.nan, | ||
(1.0 - (1.0 / (1.0 + 2.0))), | ||
1.0, | ||
], | ||
), | ||
( | ||
Series([1.0, np.nan, 101.0, 50.0]), | ||
False, | ||
False, | ||
[ | ||
(1.0 - (1.0 / (1.0 + 2.0))) ** 3, | ||
np.nan, | ||
(1.0 - (1.0 / (1.0 + 2.0))) * (1.0 / (1.0 + 2.0)), | ||
(1.0 / (1.0 + 2.0)) | ||
* ((1.0 - (1.0 / (1.0 + 2.0))) ** 2 + (1.0 / (1.0 + 2.0))), | ||
], | ||
), | ||
( | ||
Series([1.0, np.nan, 101.0, 50.0]), | ||
False, | ||
True, | ||
[ | ||
(1.0 - (1.0 / (1.0 + 2.0))) ** 2, | ||
np.nan, | ||
(1.0 - (1.0 / (1.0 + 2.0))) * (1.0 / (1.0 + 2.0)), | ||
(1.0 / (1.0 + 2.0)), | ||
], | ||
), | ||
], | ||
) | ||
def test_ewma_nan_handling_cases(s, adjust, ignore_na, w): | ||
# GH 7603 | ||
expected = (s.multiply(w).cumsum() / Series(w).cumsum()).fillna(method="ffill") | ||
result = s.ewm(com=2.0, adjust=adjust, ignore_na=ignore_na).mean() | ||
|
||
tm.assert_series_equal(result, expected) | ||
if ignore_na is False: | ||
# check that ignore_na defaults to False | ||
result = s.ewm(com=2.0, adjust=adjust).mean() | ||
tm.assert_series_equal(result, expected) | ||
if ignore_na is False: | ||
# check that ignore_na defaults to False | ||
result = s.ewm(com=com, adjust=adjust).mean() | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize("name", ["var", "vol"]) | ||
def test_ewmvar_ewmvol(series, frame, nan_locs, name): | ||
check_ew(name=name, frame=frame, series=series, nan_locs=nan_locs) | ||
def test_ewmvar_ewmvol(series, frame, name): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this be combined with test_ewma |
||
series_result = getattr(series.ewm(com=10), name)() | ||
assert isinstance(series_result, Series) | ||
|
||
frame_result = getattr(frame.ewm(com=10), name)() | ||
assert type(frame_result) == DataFrame | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not isinstance? |
||
|
||
|
||
def test_ewma_span_com_args(series): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than a test use two fixtures independently, could have a test_ewma_series and test_ewma_frame