-
-
Notifications
You must be signed in to change notification settings - Fork 19k
BUG: Adding skipna as an option to groupby cumsum and cumprod #19914
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 5 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 |
---|---|---|
|
@@ -139,7 +139,8 @@ def group_median_float64(ndarray[float64_t, ndim=2] out, | |
def group_cumprod_float64(float64_t[:, :] out, | ||
float64_t[:, :] values, | ||
int64_t[:] labels, | ||
bint is_datetimelike): | ||
bint is_datetimelike, | ||
bint skipna=True): | ||
""" | ||
Only transforms on axis=0 | ||
""" | ||
|
@@ -163,14 +164,21 @@ def group_cumprod_float64(float64_t[:, :] out, | |
if val == val: | ||
accum[lab, j] *= val | ||
out[i, j] = accum[lab, j] | ||
if val != val: | ||
|
||
if skipna: | ||
out[i, j] = NaN | ||
else: | ||
accum[lab, j] = NaN | ||
out[i, j] = accum[lab, j] | ||
|
||
|
||
|
||
@cython.boundscheck(False) | ||
@cython.wraparound(False) | ||
def group_cumsum(numeric[:, :] out, | ||
numeric[:, :] values, | ||
int64_t[:] labels, | ||
is_datetimelike): | ||
is_datetimelike, | ||
bint skipna=True): | ||
""" | ||
Only transforms on axis=0 | ||
""" | ||
|
@@ -196,6 +204,12 @@ def group_cumsum(numeric[:, :] out, | |
if val == val: | ||
accum[lab, j] += val | ||
out[i, j] = accum[lab, j] | ||
if val != val: | ||
|
||
if skipna: | ||
out[i, j] = NaN | ||
else: | ||
accum[lab, j] = NaN | ||
out[i, j] = accum[lab, j] | ||
else: | ||
accum[lab, j] += val | ||
out[i, j] = accum[lab, j] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2521,6 +2521,26 @@ def test_groupby_cumprod(self): | |
expected.name = 'value' | ||
tm.assert_series_equal(actual, expected) | ||
|
||
# make sure that skipna works | ||
|
||
df = pd.concat( | ||
[pd.DataFrame({'x': [1.0, 2.0, np.nan, np.nan, 3.0, 2.0], | ||
'gp': 'a'}), | ||
pd.DataFrame({'x': [2.0, 5.0, 6.0, 1.0, np.nan, 1.0], | ||
'gp': 'b'})]) | ||
result = df.groupby('gp')['x'].cumprod(skipna=False) | ||
expected = pd.Series([1.0, 2.0, np.nan, np.nan, np.nan, np.nan, | ||
2.0, 10.0, 60.0, 60.0, np.nan, np.nan], | ||
name='x', index=(0, 1, 2, 3, 4, 5, | ||
0, 1, 2, 3, 4, 5)) | ||
tm.assert_series_equal(result, expected) | ||
|
||
result = df.groupby('gp')['x'].cumprod() | ||
|
||
expected = pd.Series([1.0, 2.0, np.nan, np.nan, 6.0, 12.0, | ||
2.0, 10.0, 60.0, 60.0, np.nan, 60.0], | ||
name='x', index=(0, 1, 2, 3, 4, 5, | ||
0, 1, 2, 3, 4, 5)) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
def test_ops_general(self): | ||
ops = [('mean', np.mean), | ||
('median', np.median), | ||
|
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.
These should be
likewise for cumprod.
And skipna should probably be in double backticks.
And could you add a
:issue:
for the issue you're closing.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.
Added those in