-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
POC: Don't special case Python builtin and NumPy functions #53426
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 all commits
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 |
---|---|---|
|
@@ -1502,13 +1502,14 @@ def foo2(x, b=2, c=0): | |
|
||
def test_agg_std(): | ||
df = DataFrame(np.arange(6).reshape(3, 2), columns=["A", "B"]) | ||
expected_value = 1.632993161855452 | ||
|
||
result = df.agg(np.std) | ||
expected = Series({"A": 2.0, "B": 2.0}, dtype=float) | ||
expected = Series({"A": expected_value, "B": expected_value}, dtype=float) | ||
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. Different ddof here will be import to communicate in a possible deprecation. |
||
tm.assert_series_equal(result, expected) | ||
|
||
result = df.agg([np.std]) | ||
expected = DataFrame({"A": 2.0, "B": 2.0}, index=["std"]) | ||
expected = DataFrame({"A": expected_value, "B": expected_value}, index=["std"]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
from itertools import chain | ||
import operator | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
from pandas.core.dtypes.common import is_number | ||
|
||
from pandas import ( | ||
DataFrame, | ||
Series, | ||
|
@@ -86,162 +83,6 @@ def test_apply_np_transformer(float_frame, op, how): | |
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"series, func, expected", | ||
chain( | ||
tm.get_cython_table_params( | ||
Series(dtype=np.float64), | ||
[ | ||
("sum", 0), | ||
("max", np.nan), | ||
("min", np.nan), | ||
("all", True), | ||
("any", False), | ||
("mean", np.nan), | ||
("prod", 1), | ||
("std", np.nan), | ||
("var", np.nan), | ||
("median", np.nan), | ||
], | ||
), | ||
tm.get_cython_table_params( | ||
Series([np.nan, 1, 2, 3]), | ||
[ | ||
("sum", 6), | ||
("max", 3), | ||
("min", 1), | ||
("all", True), | ||
("any", True), | ||
("mean", 2), | ||
("prod", 6), | ||
("std", 1), | ||
("var", 1), | ||
("median", 2), | ||
], | ||
), | ||
tm.get_cython_table_params( | ||
Series("a b c".split()), | ||
[ | ||
("sum", "abc"), | ||
("max", "c"), | ||
("min", "a"), | ||
("all", True), | ||
("any", True), | ||
], | ||
), | ||
), | ||
) | ||
def test_agg_cython_table_series(series, func, expected): | ||
# GH21224 | ||
# test reducing functions in | ||
# pandas.core.base.SelectionMixin._cython_table | ||
result = series.agg(func) | ||
if is_number(expected): | ||
assert np.isclose(result, expected, equal_nan=True) | ||
else: | ||
assert result == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"series, func, expected", | ||
chain( | ||
tm.get_cython_table_params( | ||
Series(dtype=np.float64), | ||
[ | ||
("cumprod", Series([], dtype=np.float64)), | ||
("cumsum", Series([], dtype=np.float64)), | ||
], | ||
), | ||
tm.get_cython_table_params( | ||
Series([np.nan, 1, 2, 3]), | ||
[ | ||
("cumprod", Series([np.nan, 1, 2, 6])), | ||
("cumsum", Series([np.nan, 1, 3, 6])), | ||
], | ||
), | ||
tm.get_cython_table_params( | ||
Series("a b c".split()), [("cumsum", Series(["a", "ab", "abc"]))] | ||
), | ||
), | ||
) | ||
def test_agg_cython_table_transform_series(series, func, expected): | ||
# GH21224 | ||
# test transforming functions in | ||
# pandas.core.base.SelectionMixin._cython_table (cumprod, cumsum) | ||
result = series.agg(func) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"df, func, expected", | ||
chain( | ||
tm.get_cython_table_params( | ||
DataFrame(), | ||
[ | ||
("sum", Series(dtype="float64")), | ||
("max", Series(dtype="float64")), | ||
("min", Series(dtype="float64")), | ||
("all", Series(dtype=bool)), | ||
("any", Series(dtype=bool)), | ||
("mean", Series(dtype="float64")), | ||
("prod", Series(dtype="float64")), | ||
("std", Series(dtype="float64")), | ||
("var", Series(dtype="float64")), | ||
("median", Series(dtype="float64")), | ||
], | ||
), | ||
tm.get_cython_table_params( | ||
DataFrame([[np.nan, 1], [1, 2]]), | ||
[ | ||
("sum", Series([1.0, 3])), | ||
("max", Series([1.0, 2])), | ||
("min", Series([1.0, 1])), | ||
("all", Series([True, True])), | ||
("any", Series([True, True])), | ||
("mean", Series([1, 1.5])), | ||
("prod", Series([1.0, 2])), | ||
("std", Series([np.nan, 0.707107])), | ||
("var", Series([np.nan, 0.5])), | ||
("median", Series([1, 1.5])), | ||
], | ||
), | ||
), | ||
) | ||
def test_agg_cython_table_frame(df, func, expected, axis): | ||
# GH 21224 | ||
# test reducing functions in | ||
# pandas.core.base.SelectionMixin._cython_table | ||
result = df.agg(func, axis=axis) | ||
tm.assert_series_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"df, func, expected", | ||
chain( | ||
tm.get_cython_table_params( | ||
DataFrame(), [("cumprod", DataFrame()), ("cumsum", DataFrame())] | ||
), | ||
tm.get_cython_table_params( | ||
DataFrame([[np.nan, 1], [1, 2]]), | ||
[ | ||
("cumprod", DataFrame([[np.nan, 1], [1, 2]])), | ||
("cumsum", DataFrame([[np.nan, 1], [1, 3]])), | ||
], | ||
), | ||
), | ||
) | ||
def test_agg_cython_table_transform_frame(df, func, expected, axis): | ||
# GH 21224 | ||
# test transforming functions in | ||
# pandas.core.base.SelectionMixin._cython_table (cumprod, cumsum) | ||
if axis in ("columns", 1): | ||
# operating blockwise doesn't let us preserve dtypes | ||
expected = expected.astype("float64") | ||
|
||
result = df.agg(func, axis=axis) | ||
tm.assert_frame_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. These tests should probably be testing, eventually for the results of the numpy functions? 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. Yea, I wasn't sure if this was just testing compatibility between e.g. With this test removed, we have 1308 tests (counting different parametrizations) that pass a NumPy function into agg/apply/transform (within |
||
@pytest.mark.parametrize("op", series_transform_kernels) | ||
def test_transform_groupby_kernel_series(request, string_series, op): | ||
# GH 35964 | ||
|
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
_cython_table
also ve removed?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.
We need to know whether the passed function is a NumPy function (maybe there is a better way to do this?) for backwards compatibility. This is because
Series.apply
treats these differently.