-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
BUG: Adding skipna as an option to groupby cumsum and cumprod #19914
Changes from 7 commits
90b7978
84db344
6182352
2579eaa
75d2870
bb740fd
47fe8d6
107bc0b
5aceda9
3072df7
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 |
---|---|---|
|
@@ -498,6 +498,30 @@ def test_cython_transform_series(self, op, args, targop): | |
tm.assert_series_equal(expected, getattr( | ||
data.groupby(labels), op)(*args)) | ||
|
||
@pytest.mark.parametrize("op", ['cumprod', 'cumsum']) | ||
@pytest.mark.parametrize("kwargs", [{'skipna': False}, {'skipna': True}]) | ||
@pytest.mark.parametrize('input, exp', [ | ||
# When everything is NaN | ||
({'key': ['b'] * 10, 'value': np.nan}, [np.nan] * 10), | ||
# When there is a single NaN | ||
({'key': ['b'] * 10 + ['a'] * 2, | ||
'value': [3] * 3 + [np.nan] + [3] * 8}, | ||
{('cumprod', False): [3.0, 9.0, 27.0] + [np.nan] * 7 + [3.0, 9.0], | ||
('cumprod', True): [3.0, 9.0, 27.0, np.nan, 81., 243., 729., | ||
2187., 6561., 19683., 3.0, 9.0], | ||
('cumsum', False): [3.0, 6.0, 9.0] + [np.nan] * 7 + [3.0, 6.0], | ||
('cumsum', True): [3.0, 6.0, 9.0, np.nan, 12., 15., 18., | ||
21., 24., 27., 3.0, 6.0]})]) | ||
def test_groupby_cum_skip(self, op, kwargs, input, exp): | ||
df = pd.DataFrame(input) | ||
result = df.groupby('key')['value'].transform(op, **kwargs) | ||
if isinstance(exp, dict): | ||
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. might be cleaner to just wrap Series in the parameterization itself, then don't need to add the index. Can add the 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. Hmm, I changed it so that we do the |
||
expected = pd.Series(exp[(op, kwargs['skipna'])], | ||
name='value', index=range(12)) | ||
else: | ||
expected = pd.Series(exp, name='value', index=range(10)) | ||
tm.assert_series_equal(expected, result) | ||
|
||
@pytest.mark.parametrize( | ||
"op, args, targop", | ||
[('cumprod', (), lambda x: x.cumprod()), | ||
|
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.
you can just rename this to skipna and make it take [False, True].
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.
Good point, did that.