From 97c277b0f1832aebe97b54154bab12ac42f87ad8 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Tue, 18 Feb 2020 01:53:19 +0000 Subject: [PATCH 01/10] Add tests for transform fillna --- pandas/tests/groupby/test_transform.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index 740103eec185a..5efbb66e94eb7 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -1196,3 +1196,27 @@ def test_transform_lambda_indexing(): ), ) tm.assert_frame_equal(result, expected) + + +@pytest.mark.parametrize( + "input_df", + [ + DataFrame( + { + "A": [121, 121, 121, 121, 231, 231, 676], + "B": [1, 2, np.nan, 3, 3, np.nan, 4], + } + ), + DataFrame( + { + "A": [121, 121, 121, 121, 231, 231, 676], + "B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0], + } + ), + ], +) +def test_groupby_transform_fillna(input_df): + # GH 27905 + result = input_df.groupby("A").transform(lambda x: x.fillna(x.mean())) + expected = pd.DataFrame({"B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0]}) + tm.assert_frame_equal(result, expected) From 4492fab17660c9a1b30c08f7aaa84c4fd3612144 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 23 Feb 2020 05:25:12 +0000 Subject: [PATCH 02/10] Add tests with transformation_func fixture --- pandas/tests/groupby/test_transform.py | 78 ++++++++++++++++++-------- 1 file changed, 56 insertions(+), 22 deletions(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index 5efbb66e94eb7..aaf3af1006de3 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -1198,25 +1198,59 @@ def test_transform_lambda_indexing(): tm.assert_frame_equal(result, expected) -@pytest.mark.parametrize( - "input_df", - [ - DataFrame( - { - "A": [121, 121, 121, 121, 231, 231, 676], - "B": [1, 2, np.nan, 3, 3, np.nan, 4], - } - ), - DataFrame( - { - "A": [121, 121, 121, 121, 231, 231, 676], - "B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0], - } - ), - ], -) -def test_groupby_transform_fillna(input_df): - # GH 27905 - result = input_df.groupby("A").transform(lambda x: x.fillna(x.mean())) - expected = pd.DataFrame({"B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0]}) - tm.assert_frame_equal(result, expected) +def test_transform_nan_tshift_corrwith(transformation_func): + + df1 = DataFrame( + { + "A": [121, 121, 121, 121, 231, 231, 676], + "B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0], + } + ) + g1 = df1.groupby("A") + + if transformation_func == "corrwith": + result = g1.corrwith(df1) + expected = pd.DataFrame(dict(B=[1, np.nan, np.nan], A=[np.nan] * 3)) + expected.index = pd.Index([121, 231, 676], name="A") + tm.assert_frame_equal(result, expected) + + if transformation_func == "fillna": + df3 = df1.copy() + df3["B"] = [1, np.nan, np.nan, 3, np.nan, 3, 4] + result = df3.groupby("A").transform(lambda x: x.fillna(x.mean())) + expected = pd.DataFrame({"B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0]}) + tm.assert_frame_equal(result, expected) + + result = df3.groupby("A").transform(transformation_func, value=1) + expected = pd.DataFrame({"B": [1.0, 1.0, 1.0, 3.0, 1.0, 3.0, 4.0]}) + tm.assert_frame_equal(result, expected) + + if transformation_func == "tshift": + df2 = df1.copy() + dt_periods = pd.date_range("2013-11-03", periods=7, freq="D") + df2["C"] = dt_periods + result = df2.set_index("C").groupby("A").tshift(2, "D") + df2["C"] = dt_periods + dt_periods.freq * 2 + expected = df2 + tm.assert_frame_equal( + result.reset_index().reindex(columns=["A", "B", "C"]), expected + ) + + +def test_check_original_and_transformed_index(transformation_func): + df = DataFrame({"A": [0, 0, 0, 1, 1, 1], "B": [0, 1, 2, 3, 4, 5]}) + g = df.groupby("A") + + if transformation_func in [ + "cummax", + "cummin", + "cumprod", + "cumsum", + "diff", + "ffill", + "pct_change", + "rank", + "shift", + ]: + result = g.transform(transformation_func) + tm.assert_index_equal(result.index, df.index) From 25a524fde865dd6f986abe0bccc667f5c2e35e92 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sun, 23 Feb 2020 22:47:34 +0000 Subject: [PATCH 03/10] Splt into separate tests and add use fixture for input df --- pandas/tests/groupby/test_transform.py | 72 ++++++++++++++++++-------- 1 file changed, 51 insertions(+), 21 deletions(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index aaf3af1006de3..5f5002877827e 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -23,6 +23,16 @@ from pandas.core.groupby.groupby import DataError +@pytest.fixture +def df_for_transformation_func(): + return DataFrame( + { + "A": [121, 121, 121, 121, 231, 231, 676], + "B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0], + } + ) + + def assert_fp_equal(a, b): assert (np.abs(a - b) < 1e-12).all() @@ -1198,46 +1208,66 @@ def test_transform_lambda_indexing(): tm.assert_frame_equal(result, expected) -def test_transform_nan_tshift_corrwith(transformation_func): +def test_groupby_corrwith(transformation_func, df_for_transformation_func): - df1 = DataFrame( - { - "A": [121, 121, 121, 121, 231, 231, 676], - "B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0], - } - ) - g1 = df1.groupby("A") + # GH 27905 + df = df_for_transformation_func.copy() + g = df.groupby("A") if transformation_func == "corrwith": - result = g1.corrwith(df1) + op = lambda x: getattr(x, transformation_func)(df) + result = op(g) expected = pd.DataFrame(dict(B=[1, np.nan, np.nan], A=[np.nan] * 3)) expected.index = pd.Index([121, 231, 676], name="A") tm.assert_frame_equal(result, expected) + +def test_groupby_transform_nan(transformation_func, df_for_transformation_func): + + # GH 27905 + df = df_for_transformation_func.copy() + g = df.groupby("A") + if transformation_func == "fillna": - df3 = df1.copy() - df3["B"] = [1, np.nan, np.nan, 3, np.nan, 3, 4] - result = df3.groupby("A").transform(lambda x: x.fillna(x.mean())) - expected = pd.DataFrame({"B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0]}) - tm.assert_frame_equal(result, expected) - result = df3.groupby("A").transform(transformation_func, value=1) + df["B"] = [1, np.nan, np.nan, 3, np.nan, 3, 4] + result = g.transform(transformation_func, value=1) expected = pd.DataFrame({"B": [1.0, 1.0, 1.0, 3.0, 1.0, 3.0, 4.0]}) tm.assert_frame_equal(result, expected) + op = lambda x: getattr(x, transformation_func)(1) + result = op(g) + tm.assert_frame_equal(result, expected) + + +def test_groupby_transform_tshift(transformation_func, df_for_transformation_func): + + # GH 27905 + df = df_for_transformation_func.copy() + dt_periods = pd.date_range("2013-11-03", periods=7, freq="D") + df["C"] = dt_periods + g = df.set_index("C").groupby("A") if transformation_func == "tshift": - df2 = df1.copy() - dt_periods = pd.date_range("2013-11-03", periods=7, freq="D") - df2["C"] = dt_periods - result = df2.set_index("C").groupby("A").tshift(2, "D") - df2["C"] = dt_periods + dt_periods.freq * 2 - expected = df2 + + op = lambda x: getattr(x, transformation_func)(2, "D") + result = op(g) + df["C"] = dt_periods + dt_periods.freq * 2 + expected = df tm.assert_frame_equal( result.reset_index().reindex(columns=["A", "B", "C"]), expected ) def test_check_original_and_transformed_index(transformation_func): + + # GH 27905 + df = DataFrame( + { + "A": [121, 121, 121, 121, 231, 231, 676], + "B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0], + } + ) + df = DataFrame({"A": [0, 0, 0, 1, 1, 1], "B": [0, 1, 2, 3, 4, 5]}) g = df.groupby("A") From 9857dc61d9f58eb0d7ead216a51e795db65bfcd8 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Mon, 24 Feb 2020 00:49:27 +0000 Subject: [PATCH 04/10] change name of tshift test --- pandas/tests/groupby/test_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index 5f5002877827e..ae6bdffe907b5 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -1239,7 +1239,7 @@ def test_groupby_transform_nan(transformation_func, df_for_transformation_func): tm.assert_frame_equal(result, expected) -def test_groupby_transform_tshift(transformation_func, df_for_transformation_func): +def test_groupby_tshift(transformation_func, df_for_transformation_func): # GH 27905 df = df_for_transformation_func.copy() From 2019af41b18b089cdd45c53b4c5ed6d064de28b6 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Thu, 27 Feb 2020 20:05:18 +0000 Subject: [PATCH 05/10] Shift test functions up --- pandas/tests/groupby/test_transform.py | 158 ++++++++++++------------- 1 file changed, 79 insertions(+), 79 deletions(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index ae6bdffe907b5..cf8f747d3a528 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -356,6 +356,84 @@ def test_transform_transformation_func(transformation_func): tm.assert_frame_equal(result, expected) +def test_groupby_corrwith(transformation_func, df_for_transformation_func): + + # GH 27905 + df = df_for_transformation_func.copy() + g = df.groupby("A") + + if transformation_func == "corrwith": + op = lambda x: getattr(x, transformation_func)(df) + result = op(g) + expected = pd.DataFrame(dict(B=[1, np.nan, np.nan], A=[np.nan] * 3)) + expected.index = pd.Index([121, 231, 676], name="A") + tm.assert_frame_equal(result, expected) + + +def test_groupby_transform_nan(transformation_func, df_for_transformation_func): + + # GH 27905 + df = df_for_transformation_func.copy() + g = df.groupby("A") + + if transformation_func == "fillna": + + df["B"] = [1, np.nan, np.nan, 3, np.nan, 3, 4] + result = g.transform(transformation_func, value=1) + expected = pd.DataFrame({"B": [1.0, 1.0, 1.0, 3.0, 1.0, 3.0, 4.0]}) + tm.assert_frame_equal(result, expected) + op = lambda x: getattr(x, transformation_func)(1) + result = op(g) + tm.assert_frame_equal(result, expected) + + +def test_groupby_tshift(transformation_func, df_for_transformation_func): + + # GH 27905 + df = df_for_transformation_func.copy() + dt_periods = pd.date_range("2013-11-03", periods=7, freq="D") + df["C"] = dt_periods + g = df.set_index("C").groupby("A") + + if transformation_func == "tshift": + + op = lambda x: getattr(x, transformation_func)(2, "D") + result = op(g) + df["C"] = dt_periods + dt_periods.freq * 2 + expected = df + tm.assert_frame_equal( + result.reset_index().reindex(columns=["A", "B", "C"]), expected + ) + + +def test_check_original_and_transformed_index(transformation_func): + + # GH 27905 + df = DataFrame( + { + "A": [121, 121, 121, 121, 231, 231, 676], + "B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0], + } + ) + + df = DataFrame({"A": [0, 0, 0, 1, 1, 1], "B": [0, 1, 2, 3, 4, 5]}) + g = df.groupby("A") + + if transformation_func in [ + "cummax", + "cummin", + "cumprod", + "cumsum", + "diff", + "ffill", + "pct_change", + "rank", + "shift", + ]: + result = g.transform(transformation_func) + tm.assert_index_equal(result.index, df.index) + + def test_transform_select_columns(df): f = lambda x: x.mean() result = df.groupby("A")[["C", "D"]].transform(f) @@ -1205,82 +1283,4 @@ def test_transform_lambda_indexing(): names=["A", "B"], ), ) - tm.assert_frame_equal(result, expected) - - -def test_groupby_corrwith(transformation_func, df_for_transformation_func): - - # GH 27905 - df = df_for_transformation_func.copy() - g = df.groupby("A") - - if transformation_func == "corrwith": - op = lambda x: getattr(x, transformation_func)(df) - result = op(g) - expected = pd.DataFrame(dict(B=[1, np.nan, np.nan], A=[np.nan] * 3)) - expected.index = pd.Index([121, 231, 676], name="A") - tm.assert_frame_equal(result, expected) - - -def test_groupby_transform_nan(transformation_func, df_for_transformation_func): - - # GH 27905 - df = df_for_transformation_func.copy() - g = df.groupby("A") - - if transformation_func == "fillna": - - df["B"] = [1, np.nan, np.nan, 3, np.nan, 3, 4] - result = g.transform(transformation_func, value=1) - expected = pd.DataFrame({"B": [1.0, 1.0, 1.0, 3.0, 1.0, 3.0, 4.0]}) - tm.assert_frame_equal(result, expected) - op = lambda x: getattr(x, transformation_func)(1) - result = op(g) - tm.assert_frame_equal(result, expected) - - -def test_groupby_tshift(transformation_func, df_for_transformation_func): - - # GH 27905 - df = df_for_transformation_func.copy() - dt_periods = pd.date_range("2013-11-03", periods=7, freq="D") - df["C"] = dt_periods - g = df.set_index("C").groupby("A") - - if transformation_func == "tshift": - - op = lambda x: getattr(x, transformation_func)(2, "D") - result = op(g) - df["C"] = dt_periods + dt_periods.freq * 2 - expected = df - tm.assert_frame_equal( - result.reset_index().reindex(columns=["A", "B", "C"]), expected - ) - - -def test_check_original_and_transformed_index(transformation_func): - - # GH 27905 - df = DataFrame( - { - "A": [121, 121, 121, 121, 231, 231, 676], - "B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0], - } - ) - - df = DataFrame({"A": [0, 0, 0, 1, 1, 1], "B": [0, 1, 2, 3, 4, 5]}) - g = df.groupby("A") - - if transformation_func in [ - "cummax", - "cummin", - "cumprod", - "cumsum", - "diff", - "ffill", - "pct_change", - "rank", - "shift", - ]: - result = g.transform(transformation_func) - tm.assert_index_equal(result.index, df.index) + tm.assert_frame_equal(result, expected) \ No newline at end of file From 4d0fa2a986aa238f9946815b7d09f563344fc98b Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Thu, 27 Feb 2020 20:08:39 +0000 Subject: [PATCH 06/10] black --- pandas/tests/groupby/test_transform.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index cf8f747d3a528..bbfe956afca0c 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -1283,4 +1283,4 @@ def test_transform_lambda_indexing(): names=["A", "B"], ), ) - tm.assert_frame_equal(result, expected) \ No newline at end of file + tm.assert_frame_equal(result, expected) From 94befe6bc2522d9441b95e55602dbe746f63c1bc Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Thu, 27 Feb 2020 21:04:10 +0000 Subject: [PATCH 07/10] rremove copy --- pandas/tests/groupby/test_transform.py | 55 ++++++++++++-------------- 1 file changed, 25 insertions(+), 30 deletions(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index bbfe956afca0c..a23d230fc7dd1 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -356,54 +356,49 @@ def test_transform_transformation_func(transformation_func): tm.assert_frame_equal(result, expected) -def test_groupby_corrwith(transformation_func, df_for_transformation_func): +def test_groupby_corrwith(df_for_transformation_func): # GH 27905 - df = df_for_transformation_func.copy() + df = df_for_transformation_func g = df.groupby("A") - if transformation_func == "corrwith": - op = lambda x: getattr(x, transformation_func)(df) - result = op(g) - expected = pd.DataFrame(dict(B=[1, np.nan, np.nan], A=[np.nan] * 3)) - expected.index = pd.Index([121, 231, 676], name="A") - tm.assert_frame_equal(result, expected) + op = lambda x: getattr(x, "corrwith")(df) + result = op(g) + expected = pd.DataFrame(dict(B=[1, np.nan, np.nan], A=[np.nan] * 3)) + expected.index = pd.Index([121, 231, 676], name="A") + tm.assert_frame_equal(result, expected) -def test_groupby_transform_nan(transformation_func, df_for_transformation_func): +def test_groupby_transform_nan(df_for_transformation_func): # GH 27905 - df = df_for_transformation_func.copy() + df = df_for_transformation_func g = df.groupby("A") - if transformation_func == "fillna": - - df["B"] = [1, np.nan, np.nan, 3, np.nan, 3, 4] - result = g.transform(transformation_func, value=1) - expected = pd.DataFrame({"B": [1.0, 1.0, 1.0, 3.0, 1.0, 3.0, 4.0]}) - tm.assert_frame_equal(result, expected) - op = lambda x: getattr(x, transformation_func)(1) - result = op(g) - tm.assert_frame_equal(result, expected) + df["B"] = [1, np.nan, np.nan, 3, np.nan, 3, 4] + result = g.transform("fillna", value=1) + expected = pd.DataFrame({"B": [1.0, 1.0, 1.0, 3.0, 1.0, 3.0, 4.0]}) + tm.assert_frame_equal(result, expected) + op = lambda x: getattr(x, "fillna")(1) + result = op(g) + tm.assert_frame_equal(result, expected) -def test_groupby_tshift(transformation_func, df_for_transformation_func): +def test_groupby_tshift(df_for_transformation_func): # GH 27905 - df = df_for_transformation_func.copy() + df = df_for_transformation_func dt_periods = pd.date_range("2013-11-03", periods=7, freq="D") df["C"] = dt_periods g = df.set_index("C").groupby("A") - if transformation_func == "tshift": - - op = lambda x: getattr(x, transformation_func)(2, "D") - result = op(g) - df["C"] = dt_periods + dt_periods.freq * 2 - expected = df - tm.assert_frame_equal( - result.reset_index().reindex(columns=["A", "B", "C"]), expected - ) + op = lambda x: getattr(x, "tshift")(2, "D") + result = op(g) + df["C"] = dt_periods + dt_periods.freq * 2 + expected = df + tm.assert_frame_equal( + result.reset_index().reindex(columns=["A", "B", "C"]), expected + ) def test_check_original_and_transformed_index(transformation_func): From d210c42014db26a692ff1c65f7ee93c7020d264b Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sat, 29 Feb 2020 01:23:47 +0000 Subject: [PATCH 08/10] Remove redundant tests and some more changes --- pandas/tests/groupby/test_transform.py | 63 +++++--------------------- 1 file changed, 12 insertions(+), 51 deletions(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index a23d230fc7dd1..acdbd73baa52d 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -1,4 +1,5 @@ """ test with the .transform """ +from datetime import timedelta from io import StringIO import numpy as np @@ -29,6 +30,7 @@ def df_for_transformation_func(): { "A": [121, 121, 121, 121, 231, 231, 676], "B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0], + "C": pd.date_range("2013-11-03", periods=7, freq="D"), } ) @@ -328,7 +330,7 @@ def test_dispatch_transform(tsframe): tm.assert_frame_equal(filled, expected) -def test_transform_transformation_func(transformation_func): +def test_transform_transformation_func(transformation_func, df_for_transformation_func): # GH 30918 df = DataFrame( { @@ -356,77 +358,36 @@ def test_transform_transformation_func(transformation_func): tm.assert_frame_equal(result, expected) -def test_groupby_corrwith(df_for_transformation_func): +def test_groupby_transform_corrwith(df_for_transformation_func): # GH 27905 df = df_for_transformation_func g = df.groupby("A") - op = lambda x: getattr(x, "corrwith")(df) - result = op(g) + result = g.corrwith(df) expected = pd.DataFrame(dict(B=[1, np.nan, np.nan], A=[np.nan] * 3)) expected.index = pd.Index([121, 231, 676], name="A") tm.assert_frame_equal(result, expected) + with pytest.raises(AttributeError) as m: + g.transform("corrwith", df) -def test_groupby_transform_nan(df_for_transformation_func): - - # GH 27905 - df = df_for_transformation_func - g = df.groupby("A") - - df["B"] = [1, np.nan, np.nan, 3, np.nan, 3, 4] - result = g.transform("fillna", value=1) - expected = pd.DataFrame({"B": [1.0, 1.0, 1.0, 3.0, 1.0, 3.0, 4.0]}) - tm.assert_frame_equal(result, expected) - op = lambda x: getattr(x, "fillna")(1) - result = op(g) - tm.assert_frame_equal(result, expected) + m.match("'Series' object has no attribute 'corrwith'") -def test_groupby_tshift(df_for_transformation_func): +def test_groupby_transform_tshift(df_for_transformation_func): # GH 27905 df = df_for_transformation_func - dt_periods = pd.date_range("2013-11-03", periods=7, freq="D") - df["C"] = dt_periods g = df.set_index("C").groupby("A") - - op = lambda x: getattr(x, "tshift")(2, "D") - result = op(g) - df["C"] = dt_periods + dt_periods.freq * 2 + result = g.tshift(2, "D") + df["C"] = df["C"] + timedelta(days=2) expected = df tm.assert_frame_equal( result.reset_index().reindex(columns=["A", "B", "C"]), expected ) - -def test_check_original_and_transformed_index(transformation_func): - - # GH 27905 - df = DataFrame( - { - "A": [121, 121, 121, 121, 231, 231, 676], - "B": [1.0, 2.0, 2.0, 3.0, 3.0, 3.0, 4.0], - } - ) - - df = DataFrame({"A": [0, 0, 0, 1, 1, 1], "B": [0, 1, 2, 3, 4, 5]}) - g = df.groupby("A") - - if transformation_func in [ - "cummax", - "cummin", - "cumprod", - "cumsum", - "diff", - "ffill", - "pct_change", - "rank", - "shift", - ]: - result = g.transform(transformation_func) - tm.assert_index_equal(result.index, df.index) + result = g.transform(lambda x: x.tshift(2, "D")) def test_transform_select_columns(df): From 9575312f246fa91bcbfa1ffa2ef5378057fb3894 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sat, 29 Feb 2020 03:02:29 +0000 Subject: [PATCH 09/10] add case for groupby transfrorm tshift and mark fail --- pandas/tests/groupby/test_transform.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index acdbd73baa52d..122ac54deaeba 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -387,7 +387,16 @@ def test_groupby_transform_tshift(df_for_transformation_func): result.reset_index().reindex(columns=["A", "B", "C"]), expected ) - result = g.transform(lambda x: x.tshift(2, "D")) + op1 = g.transform(lambda x: x.tshift(2, "D")) + op2 = g.transform("tshift", *[2, "D"]) + + for result in [op1, op2]: + pytest.xfail( + "The output of groupby.transform with tshift is wrong, see GH 32344" + ) + tm.assert_frame_equal( + result.reset_index().reindex(columns=["A", "B", "C"]), expected + ) def test_transform_select_columns(df): From 3b9d91058d241c6515a56ae9cffe0e82537f6e28 Mon Sep 17 00:00:00 2001 From: Ryan Nazareth Date: Sat, 29 Feb 2020 11:20:04 +0000 Subject: [PATCH 10/10] refactor error message --- pandas/tests/groupby/test_transform.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pandas/tests/groupby/test_transform.py b/pandas/tests/groupby/test_transform.py index 122ac54deaeba..73a99f2dd4402 100644 --- a/pandas/tests/groupby/test_transform.py +++ b/pandas/tests/groupby/test_transform.py @@ -369,10 +369,10 @@ def test_groupby_transform_corrwith(df_for_transformation_func): expected.index = pd.Index([121, 231, 676], name="A") tm.assert_frame_equal(result, expected) - with pytest.raises(AttributeError) as m: - g.transform("corrwith", df) + msg = "'Series' object has no attribute 'corrwith'" - m.match("'Series' object has no attribute 'corrwith'") + with pytest.raises(AttributeError, match=msg): + g.transform("corrwith", df) def test_groupby_transform_tshift(df_for_transformation_func):