-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: Min_weight for Rolling #12750
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
Closed
Closed
ENH: Min_weight for Rolling #12750
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import pandas.stats.moments as mom | ||
import pandas.core.window as rwindow | ||
import pandas.tseries.offsets as offsets | ||
from pandas.core.window import _min_weight_mask | ||
from pandas.core.base import SpecificationError | ||
from pandas.core.common import UnsupportedFunctionCall | ||
import pandas.util.testing as tm | ||
|
@@ -495,7 +496,7 @@ def test_deprecations(self): | |
# make sure rolling functions works for different dtypes | ||
# | ||
# NOTE that these are yielded tests and so _create_data is | ||
# explicity called, nor do these inherit from unittest.TestCase | ||
# explicitly called, nor do these inherit from unittest.TestCase | ||
# | ||
# further note that we are only checking rolling for fully dtype | ||
# compliance (though both expanding and ewm inherit) | ||
|
@@ -1619,6 +1620,58 @@ def _check_ew_structures(self, func, name): | |
frame_result = getattr(self.frame.ewm(com=10), name)() | ||
self.assertEqual(type(frame_result), DataFrame) | ||
|
||
def test_min_weight_mask_series(self): | ||
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. make this a separate class (you can inherit from Base) |
||
|
||
rolling = Series([pd.np.NaN, -8, 3, 10, pd.np.NaN, 5]).rolling(3) | ||
|
||
# 30% | ||
result = _min_weight_mask(rolling, 0.3) | ||
expected = Series([False, True, True, True, True, True]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# 50% | ||
result = _min_weight_mask(rolling, 0.6) | ||
expected = Series([False, False, True, True, True, True]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
# 70% | ||
result = _min_weight_mask(rolling, 0.7) | ||
expected = Series([False, False, False, True, False, False]) | ||
tm.assert_series_equal(result, expected) | ||
|
||
def test_min_weight_rolling(self): | ||
|
||
series = Series([pd.np.NaN, -8, 3, 10, pd.np.NaN, 5]) | ||
rolling = series.rolling(3, min_periods=1, min_weight=0.6) | ||
|
||
result = rolling.sum() | ||
expected = Series([pd.np.NaN, pd.np.NaN, -5, 5, 13, 15]) | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
def test_min_weight_expanding(self): | ||
|
||
series = Series([pd.np.NaN, -8, 3, pd.np.NaN, 10, 5]) | ||
rolling = series.expanding(min_periods=1, min_weight=0.51) | ||
|
||
result = rolling.sum() | ||
expected = Series([pd.np.NaN, pd.np.NaN, -5, pd.np.NaN, 5, 10]) | ||
|
||
tm.assert_series_equal(result, expected) | ||
|
||
def test_min_weight_ewm(self): | ||
|
||
from itertools import chain | ||
|
||
# create a series with a big gap in the middle | ||
series = Series(list(chain(range(9), [pd.np.NaN] * 80, range(9, 0)))) | ||
rolling = series.ewm(span=10, min_weight=0.5) | ||
|
||
result = rolling.mean() | ||
|
||
# check that all points between 25 and 90 are NaN | ||
self.assertTrue(result.iloc[24:89].isnull().all()) | ||
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. construct the actual result |
||
|
||
|
||
# create the data only once as we are not setting it | ||
def _create_consistency_data(): | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ parentdir_prefix = pandas- | |
|
||
[flake8] | ||
ignore = E731 | ||
max-line-length = 79 | ||
|
||
[yapf] | ||
based_on_style = pep8 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
add to the doc-string
min_weight
with a versionadded tag (I don't remember exactly where these are defined)