From 5bf5ae80f4292d16c0ad90318ee5d41a1870fa62 Mon Sep 17 00:00:00 2001 From: Richard Shadrach <45562402+rhshadrach@users.noreply.github.com> Date: Wed, 17 Feb 2021 21:18:14 -0500 Subject: [PATCH] Revert previous commit (#39879) --- pandas/core/apply.py | 88 ++++++++++---------------- pandas/tests/apply/test_frame_apply.py | 14 +--- 2 files changed, 36 insertions(+), 66 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index e091eec4e2c41..b41c432dff172 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -225,66 +225,51 @@ def agg_list_like(self, _axis: int) -> FrameOrSeriesUnion: results = [] keys = [] - ndims = [] # degenerate case - # if selected_obj.ndim == 1: - for a in arg: - # colg = obj._gotitem(selected_obj.name, ndim=1, subset=selected_obj) - try: - # new_res = colg.aggregate(a) - print('selected_obj:', type(selected_obj)) - print(selected_obj) - print('###') - new_res = selected_obj.aggregate(a) - print(new_res) - - except TypeError: - pass - else: - results.append(new_res) - if isinstance(new_res, ABCNDFrame): - ndims.append(new_res.ndim) + if selected_obj.ndim == 1: + for a in arg: + colg = obj._gotitem(selected_obj.name, ndim=1, subset=selected_obj) + try: + new_res = colg.aggregate(a) + + except TypeError: + pass else: - ndims.append(0) + results.append(new_res) - # make sure we find a good name - name = com.get_callable_name(a) or a - keys.append(name) + # make sure we find a good name + name = com.get_callable_name(a) or a + keys.append(name) # multiples - # else: - # for index, col in enumerate(selected_obj): - # colg = obj._gotitem(col, ndim=1, subset=selected_obj.iloc[:, index]) - # try: - # new_res = colg.aggregate(arg) - # except (TypeError, DataError): - # pass - # except ValueError as err: - # # cannot aggregate - # if "Must produce aggregated value" in str(err): - # # raised directly in _aggregate_named - # pass - # elif "no results" in str(err): - # # raised directly in _aggregate_multiple_funcs - # pass - # else: - # raise - # else: - # results.append(new_res) - # keys.append(col) + else: + for index, col in enumerate(selected_obj): + colg = obj._gotitem(col, ndim=1, subset=selected_obj.iloc[:, index]) + try: + new_res = colg.aggregate(arg) + except (TypeError, DataError): + pass + except ValueError as err: + # cannot aggregate + if "Must produce aggregated value" in str(err): + # raised directly in _aggregate_named + pass + elif "no results" in str(err): + # raised directly in _aggregate_multiple_funcs + pass + else: + raise + else: + results.append(new_res) + keys.append(col) # if we are empty if not len(results): raise ValueError("no results") try: - # if len(results) == 0: - # result = results[0] - # else: - result = concat(results, keys=keys, axis=1, sort=False) - if all([e == 1 for e in ndims]): - result = result.T.infer_objects() + return concat(results, keys=keys, axis=1, sort=False) except TypeError as err: # we are concatting non-NDFrame objects, @@ -297,12 +282,7 @@ def agg_list_like(self, _axis: int) -> FrameOrSeriesUnion: raise ValueError( "cannot combine transform and aggregation operations" ) from err - else: - if result.columns.nlevels > 1: - new_order = [-1] + list(range(result.columns.nlevels - 1)) - result = result.reorder_levels(new_order, axis='columns') - result = result[selected_obj.columns] - return result + return result def agg_dict_like(self, _axis: int) -> FrameOrSeriesUnion: """ diff --git a/pandas/tests/apply/test_frame_apply.py b/pandas/tests/apply/test_frame_apply.py index 70b4c8890ea43..3ac9d98874f86 100644 --- a/pandas/tests/apply/test_frame_apply.py +++ b/pandas/tests/apply/test_frame_apply.py @@ -1101,7 +1101,6 @@ def test_consistency_for_boxed(self, box, int_frame_const_col): class TestDataFrameAggregate: def test_agg_transform(self, axis, float_frame): - float_frame = float_frame.head() other_axis = 1 if axis in {0, "index"} else 0 with np.errstate(all="ignore"): @@ -1125,16 +1124,11 @@ def test_agg_transform(self, axis, float_frame): expected.index = pd.MultiIndex.from_product( [float_frame.index, ["sqrt"]] ) - print("result") - print(result) - print('expected') - print(expected) 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 - print('marker') 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"}: @@ -1145,10 +1139,6 @@ def test_agg_transform(self, axis, float_frame): expected.index = pd.MultiIndex.from_product( [float_frame.index, ["absolute", "sqrt"]] ) - print() - print(result) - print() - print(expected) tm.assert_frame_equal(result, expected) def test_transform_and_agg_err(self, axis, float_frame): @@ -1156,13 +1146,13 @@ def test_transform_and_agg_err(self, axis, float_frame): msg = "cannot combine transform and aggregation operations" with pytest.raises(ValueError, match=msg): with np.errstate(all="ignore"): - print(float_frame.agg(["max", "sqrt"], axis=axis)) + float_frame.agg(["max", "sqrt"], axis=axis) df = DataFrame({"A": range(5), "B": 5}) def f(): with np.errstate(all="ignore"): - print(df.agg({"A": ["abs", "sum"], "B": ["mean", "max"]}, axis=axis)) + df.agg({"A": ["abs", "sum"], "B": ["mean", "max"]}, axis=axis) def test_demo(self): # demonstration tests