forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CLN: Separate transform tests (pandas-dev#36146)
- Loading branch information
1 parent
23e6e72
commit eb0103b
Showing
5 changed files
with
157 additions
and
78 deletions.
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}) |