From ab11f3fe1f8c3dda5cac702b96e8a4e872861d1f Mon Sep 17 00:00:00 2001 From: Hardev Khandhar Date: Sat, 21 Aug 2021 16:29:10 +0530 Subject: [PATCH 1/5] Axis Arg REF --- pandas/tests/apply/test_frame_apply.py | 31 ++++++++++++++++++++------ 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index 62983b5327a26..ed6a345a1e8f6 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -995,7 +995,11 @@ def test_consistency_for_boxed(box, int_frame_const_col): def test_agg_transform(axis, float_frame): - other_axis = 1 if axis in {0, "index"} else 0 + axis = float_frame._get_axis_number(axis) + if axis == 0: + other_axis = 1 + else: + other_axis = 0 with np.errstate(all="ignore"): @@ -1010,7 +1014,7 @@ def test_agg_transform(axis, float_frame): # list-like result = float_frame.apply([np.sqrt], axis=axis) expected = f_sqrt.copy() - if axis in {0, "index"}: + if axis == 0: expected.columns = MultiIndex.from_product([float_frame.columns, ["sqrt"]]) else: expected.index = MultiIndex.from_product([float_frame.index, ["sqrt"]]) @@ -1021,7 +1025,7 @@ def test_agg_transform(axis, float_frame): # functions per series and then concatting result = float_frame.apply([np.abs, np.sqrt], axis=axis) expected = zip_frames([f_abs, f_sqrt], axis=other_axis) - if axis in {0, "index"}: + if axis == 0: expected.columns = MultiIndex.from_product( [float_frame.columns, ["absolute", "sqrt"]] ) @@ -1102,7 +1106,11 @@ def test_agg_multiple_mixed_no_warning(): def test_agg_reduce(axis, float_frame): - other_axis = 1 if axis in {0, "index"} else 0 + axis = float_frame._get_axis_number(axis) + if axis == 0: + other_axis = 1 + else: + other_axis = 0 name1, name2 = float_frame.axes[other_axis].unique()[:2].sort_values() # all reducers @@ -1115,7 +1123,10 @@ def test_agg_reduce(axis, float_frame): axis=1, ) expected.columns = ["mean", "max", "sum"] - expected = expected.T if axis in {0, "index"} else expected + if axis == 0: + expected = expected.T + else: + expected = expected result = float_frame.agg(["mean", "max", "sum"], axis=axis) tm.assert_frame_equal(result, expected) @@ -1141,7 +1152,10 @@ def test_agg_reduce(axis, float_frame): name2: Series([float_frame.loc(other_axis)[name2].sum()], index=["sum"]), } ) - expected = expected.T if axis in {1, "columns"} else expected + if axis == 1: + expected = expected.T + else: + expected = expected tm.assert_frame_equal(result, expected) # dict input with lists with multiple @@ -1166,7 +1180,10 @@ def test_agg_reduce(axis, float_frame): }, axis=1, ) - expected = expected.T if axis in {1, "columns"} else expected + if axis == 1: + expected = expected.T + else: + expected = expected tm.assert_frame_equal(result, expected) From 5b2cd9de460f8152a18eaba1d8af88763bbfa3ba Mon Sep 17 00:00:00 2001 From: Hardev Khandhar Date: Sat, 21 Aug 2021 17:02:11 +0530 Subject: [PATCH 2/5] Updated Minor Changes --- pandas/tests/apply/test_frame_apply.py | 25 +++++-------------------- 1 file changed, 5 insertions(+), 20 deletions(-) diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index ed6a345a1e8f6..234bad87b30d1 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -996,10 +996,7 @@ def test_consistency_for_boxed(box, int_frame_const_col): def test_agg_transform(axis, float_frame): axis = float_frame._get_axis_number(axis) - if axis == 0: - other_axis = 1 - else: - other_axis = 0 + other_axis = 1 if axis == 0 else 0 with np.errstate(all="ignore"): @@ -1107,10 +1104,7 @@ def test_agg_multiple_mixed_no_warning(): def test_agg_reduce(axis, float_frame): axis = float_frame._get_axis_number(axis) - if axis == 0: - other_axis = 1 - else: - other_axis = 0 + other_axis = 1 if axis == 0 else 0 name1, name2 = float_frame.axes[other_axis].unique()[:2].sort_values() # all reducers @@ -1123,10 +1117,7 @@ def test_agg_reduce(axis, float_frame): axis=1, ) expected.columns = ["mean", "max", "sum"] - if axis == 0: - expected = expected.T - else: - expected = expected + expected = expected.T if axis == 0 else expected result = float_frame.agg(["mean", "max", "sum"], axis=axis) tm.assert_frame_equal(result, expected) @@ -1152,10 +1143,7 @@ def test_agg_reduce(axis, float_frame): name2: Series([float_frame.loc(other_axis)[name2].sum()], index=["sum"]), } ) - if axis == 1: - expected = expected.T - else: - expected = expected + expected = expected.T if axis == 1 else expected tm.assert_frame_equal(result, expected) # dict input with lists with multiple @@ -1180,10 +1168,7 @@ def test_agg_reduce(axis, float_frame): }, axis=1, ) - if axis == 1: - expected = expected.T - else: - expected = expected + expected = expected.T if axis == 1 else expected tm.assert_frame_equal(result, expected) From 1a2f207afd527eff75fef1e8e3e7eb08198c8fbd Mon Sep 17 00:00:00 2001 From: Hardev Khandhar Date: Sun, 22 Aug 2021 08:28:02 +0530 Subject: [PATCH 3/5] Reverted changes for test function --- pandas/tests/apply/test_frame_apply.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index 234bad87b30d1..4a222fecfab95 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -995,8 +995,7 @@ def test_consistency_for_boxed(box, int_frame_const_col): def test_agg_transform(axis, float_frame): - axis = float_frame._get_axis_number(axis) - other_axis = 1 if axis == 0 else 0 + other_axis = 1 if axis in {0, "index"} else 0 with np.errstate(all="ignore"): From 9437a11b65a62f42f6fbfc21756f61201066ac38 Mon Sep 17 00:00:00 2001 From: Hardev Khandhar Date: Sun, 22 Aug 2021 08:40:57 +0530 Subject: [PATCH 4/5] Updated Changes to Entire Function --- pandas/tests/apply/test_frame_apply.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index 4a222fecfab95..c3f3c972fb614 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -1010,7 +1010,7 @@ def test_agg_transform(axis, float_frame): # list-like result = float_frame.apply([np.sqrt], axis=axis) expected = f_sqrt.copy() - if axis == 0: + if axis in {0, "index"}: expected.columns = MultiIndex.from_product([float_frame.columns, ["sqrt"]]) else: expected.index = MultiIndex.from_product([float_frame.index, ["sqrt"]]) @@ -1021,7 +1021,7 @@ def test_agg_transform(axis, float_frame): # functions per series and then concatting result = float_frame.apply([np.abs, np.sqrt], axis=axis) expected = zip_frames([f_abs, f_sqrt], axis=other_axis) - if axis == 0: + if axis in {0, "index"}: expected.columns = MultiIndex.from_product( [float_frame.columns, ["absolute", "sqrt"]] ) From 830a7600da4d8d9094380a8a81a22df22a0ffafa Mon Sep 17 00:00:00 2001 From: Hardev Khandhar Date: Sun, 22 Aug 2021 09:30:58 +0530 Subject: [PATCH 5/5] Reverted to Original --- pandas/tests/apply/test_frame_apply.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index c3f3c972fb614..62983b5327a26 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -1102,8 +1102,7 @@ def test_agg_multiple_mixed_no_warning(): def test_agg_reduce(axis, float_frame): - axis = float_frame._get_axis_number(axis) - other_axis = 1 if axis == 0 else 0 + other_axis = 1 if axis in {0, "index"} else 0 name1, name2 = float_frame.axes[other_axis].unique()[:2].sort_values() # all reducers @@ -1116,7 +1115,7 @@ def test_agg_reduce(axis, float_frame): axis=1, ) expected.columns = ["mean", "max", "sum"] - expected = expected.T if axis == 0 else expected + expected = expected.T if axis in {0, "index"} else expected result = float_frame.agg(["mean", "max", "sum"], axis=axis) tm.assert_frame_equal(result, expected) @@ -1142,7 +1141,7 @@ def test_agg_reduce(axis, float_frame): name2: Series([float_frame.loc(other_axis)[name2].sum()], index=["sum"]), } ) - expected = expected.T if axis == 1 else expected + expected = expected.T if axis in {1, "columns"} else expected tm.assert_frame_equal(result, expected) # dict input with lists with multiple @@ -1167,7 +1166,7 @@ def test_agg_reduce(axis, float_frame): }, axis=1, ) - expected = expected.T if axis == 1 else expected + expected = expected.T if axis in {1, "columns"} else expected tm.assert_frame_equal(result, expected)