-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
CLN: Separate transform tests #36146
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import operator | ||
|
||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
import pandas._testing as tm | ||
from pandas.tests.frame.common import zip_frames | ||
|
||
|
||
def test_agg_transform(axis, float_frame): | ||
other_axis = 1 if axis in {0, "index"} else 0 | ||
|
||
with np.errstate(all="ignore"): | ||
|
||
f_abs = np.abs(float_frame) | ||
f_sqrt = np.sqrt(float_frame) | ||
|
||
# ufunc | ||
result = float_frame.transform(np.sqrt, axis=axis) | ||
expected = f_sqrt.copy() | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = float_frame.transform(np.sqrt, axis=axis) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# list-like | ||
expected = f_sqrt.copy() | ||
if axis in {0, "index"}: | ||
expected.columns = pd.MultiIndex.from_product( | ||
[float_frame.columns, ["sqrt"]] | ||
) | ||
else: | ||
expected.index = pd.MultiIndex.from_product([float_frame.index, ["sqrt"]]) | ||
result = float_frame.transform([np.sqrt], axis=axis) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# multiple items in list | ||
# these are in the order as if we are applying both | ||
# functions per series and then concatting | ||
expected = zip_frames([f_abs, f_sqrt], axis=other_axis) | ||
if axis in {0, "index"}: | ||
expected.columns = pd.MultiIndex.from_product( | ||
[float_frame.columns, ["absolute", "sqrt"]] | ||
) | ||
else: | ||
expected.index = pd.MultiIndex.from_product( | ||
[float_frame.index, ["absolute", "sqrt"]] | ||
) | ||
result = float_frame.transform([np.abs, "sqrt"], axis=axis) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_transform_and_agg_err(axis, float_frame): | ||
# cannot both transform and agg | ||
msg = "transforms cannot produce aggregated results" | ||
with pytest.raises(ValueError, match=msg): | ||
float_frame.transform(["max", "min"], axis=axis) | ||
|
||
msg = "cannot combine transform and aggregation operations" | ||
with pytest.raises(ValueError, match=msg): | ||
with np.errstate(all="ignore"): | ||
float_frame.transform(["max", "sqrt"], axis=axis) | ||
|
||
|
||
@pytest.mark.parametrize("method", ["abs", "shift", "pct_change", "cumsum", "rank"]) | ||
def test_transform_method_name(method): | ||
# GH 19760 | ||
df = pd.DataFrame({"A": [-1, 2]}) | ||
result = df.transform(method) | ||
expected = operator.methodcaller(method)(df) | ||
tm.assert_frame_equal(result, expected) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
import pandas._testing as tm | ||
|
||
|
||
def test_transform(string_series): | ||
# transforming functions | ||
|
||
with np.errstate(all="ignore"): | ||
f_sqrt = np.sqrt(string_series) | ||
f_abs = np.abs(string_series) | ||
|
||
# ufunc | ||
result = string_series.transform(np.sqrt) | ||
expected = f_sqrt.copy() | ||
tm.assert_series_equal(result, expected) | ||
|
||
# list-like | ||
result = string_series.transform([np.sqrt]) | ||
expected = f_sqrt.to_frame().copy() | ||
expected.columns = ["sqrt"] | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = string_series.transform([np.sqrt]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = string_series.transform(["sqrt"]) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# multiple items in list | ||
# these are in the order as if we are applying both functions per | ||
# series and then concatting | ||
expected = pd.concat([f_sqrt, f_abs], axis=1) | ||
result = string_series.transform(["sqrt", "abs"]) | ||
expected.columns = ["sqrt", "abs"] | ||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_transform_and_agg_error(string_series): | ||
# we are trying to transform with an aggregator | ||
msg = "transforms cannot produce aggregated results" | ||
with pytest.raises(ValueError, match=msg): | ||
string_series.transform(["min", "max"]) | ||
|
||
msg = "cannot combine transform and aggregation operations" | ||
with pytest.raises(ValueError, match=msg): | ||
with np.errstate(all="ignore"): | ||
string_series.transform(["sqrt", "max"]) | ||
|
||
|
||
def test_transform_none_to_type(): | ||
# GH34377 | ||
df = pd.DataFrame({"a": [None]}) | ||
|
||
msg = "DataFrame constructor called with incompatible data and dtype" | ||
with pytest.raises(TypeError, match=msg): | ||
df.transform({"a": int}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
same comments in other PR, ideally these cases are separate tests (or parameterized but that might be harder), (but can certainly be a followon PR)