-
-
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 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: | ||
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. This can just be 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. Yeah, that makes more sense. I changed that for both |
||
if skipna: | ||
out[i, j] = NaN | ||
else: | ||
accum[lab, j] = NaN | ||
out[i, j] = accum[lab, j] | ||
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 you break early here? If you ever hit a 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. That's true - I added a break there. |
||
|
||
|
||
@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: | ||
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. Same comments about |
||
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 | ||
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 you split this into a new test? Make sure to add a reference to the Github issue as a comment. 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'd find this a bit easier to read as df = pd.DataFrame({"x": [...], "gp": ['a'] * 6 + ['b'] * 6}) instead of 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. And maybe have a group with all-NA to ensure that is handled properly. 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 added a new test ( |
||
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() | ||
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. Don't think you need this test. 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. Yep, removed it. |
||
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) | ||
|
||
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. Could you add a test for 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. That should be covered by |
||
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