-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
REGR: fix numpy accumulate ufuncs for DataFrame #39260
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
d285e5a
668a9d2
d28e32a
13dcd5c
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 |
---|---|---|
|
@@ -117,6 +117,27 @@ def test_binary_frame_series_raises(): | |
np.logaddexp(df["A"], df) | ||
|
||
|
||
def test_unary_accumulate_axis(): | ||
# https://github.com/pandas-dev/pandas/issues/39259 | ||
df = pd.DataFrame({"a": [1, 3, 2, 4]}) | ||
result = np.maximum.accumulate(df) | ||
expected = pd.DataFrame({"a": [1, 3, 3, 4]}) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
df = pd.DataFrame({"a": [1, 3, 2, 4], "b": [0.1, 4.0, 3.0, 2.0]}) | ||
result = np.maximum.accumulate(df) | ||
# in theory could preserve int dtype for default axis=0 | ||
expected = pd.DataFrame({"a": [1.0, 3.0, 3.0, 4.0], "b": [0.1, 4.0, 4.0, 4.0]}) | ||
tm.assert_frame_equal(result, expected) | ||
Comment on lines
+129
to
+131
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. Mmm, I'd like this to be more than "in theory". I'd consider this a buggy test, since things should be done blockwise for axis=0. Can you you change the test case to have just floats or just ints (even if you have to manually split it for test coverage?). 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 specifically used two dtypes to have two blocks to ensure we handle this case correctly for axis=1 (which can never be done clockwise) Just above there is already a case with only ints that preserves the int dtype. 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. Note that before pandas 1.2.0, this also didn't preserve the dtypes per column, and 1.2.0 itself didn't calculate a proper result (so I would call this PR a strict improvement ;)) 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. agreed re preserving for axis=0. couldn't we still use mgr.apply in that case? 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. To be clear, I could certainly detect the special case of accumulate with axis=0, and then pass axis=1 to the blocks, but: 1) that requires special case code like this in array_ufunc: else:
# the ufunc(dataframe) case
...
elif method == "accumulate" and ("axis" not in kwargs or ("axis" in kwargs and kwargs["axis"] == 0)):
# swap axis for the transposed Block values
kwargs["axis"] = 1
result = mgr.apply(getattr(ufunc, method), **kwargs) 2) that requires 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.
Once more with feeling: this wouldn't be an issue with 2D EAs. 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 I can also say: this wouldn't be an issue with only 1D arrays .. 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. @TomAugspurger @jbrockmendel I opened #39275 to keep track of the fact that this can be improved to preserve dtypes |
||
|
||
result = np.maximum.accumulate(df, axis=0) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = np.maximum.accumulate(df, axis=1) | ||
expected = pd.DataFrame({"a": [1.0, 3.0, 2.0, 4.0], "b": [1.0, 4.0, 3.0, 4.0]}) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_frame_outer_deprecated(): | ||
df = pd.DataFrame({"A": [1, 2]}) | ||
with tm.assert_produces_warning(FutureWarning): | ||
|
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.
can you properly reference accumulate. i would be more clear about this note, and show and example
np.maximum.accumulate(df)
or similar (just the name no need to show computation)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.
Do you mean adding a reference to the numpy docs?
In case, I already added the example here.
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.
yes a refernce to the numpy docs
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.
OK, done