From fb4ec2f4ac0ddbae25899ae3acb58f04a261c378 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 14 Mar 2021 07:15:53 -0400 Subject: [PATCH 1/4] CLN: Don't modify state in FrameApply.agg --- pandas/core/apply.py | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 57147461284fb..3a2c2d7124963 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -633,28 +633,28 @@ def agg(self): obj = self.obj axis = self.axis - # TODO: Avoid having to change state - self.obj = self.obj if self.axis == 0 else self.obj.T - self.axis = 0 - - result = None try: - result = super().agg() + if axis == 1: + result = FrameRowApply( + obj.T, + self.orig_f, + self.raw, + self.result_type, + self.args, + self.kwargs, + ).agg() + result = result.T if result is not None else result + else: + result = super().agg() except TypeError as err: exc = TypeError( "DataFrame constructor called with " f"incompatible data and dtype: {err}" ) raise exc from err - finally: - self.obj = obj - self.axis = axis - - if axis == 1: - result = result.T if result is not None else result if result is None: - result = self.obj.apply(self.orig_f, axis, args=self.args, **self.kwargs) + result = obj.apply(self.orig_f, axis, args=self.args, **self.kwargs) return result From 255c2dc16c8e32847b220bf6f9c2ec8bd92dc1ad Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sun, 14 Mar 2021 07:27:27 -0400 Subject: [PATCH 2/4] CLN: Don't catch TypeError in FrameApply.agg --- pandas/core/apply.py | 31 ++++++++++---------------- pandas/tests/apply/test_invalid_arg.py | 8 +++++++ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 3a2c2d7124963..cb20f6c69e6f1 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -633,25 +633,18 @@ def agg(self): obj = self.obj axis = self.axis - try: - if axis == 1: - result = FrameRowApply( - obj.T, - self.orig_f, - self.raw, - self.result_type, - self.args, - self.kwargs, - ).agg() - result = result.T if result is not None else result - else: - result = super().agg() - except TypeError as err: - exc = TypeError( - "DataFrame constructor called with " - f"incompatible data and dtype: {err}" - ) - raise exc from err + if axis == 1: + result = FrameRowApply( + obj.T, + self.orig_f, + self.raw, + self.result_type, + self.args, + self.kwargs, + ).agg() + result = result.T if result is not None else result + else: + result = super().agg() if result is None: result = obj.apply(self.orig_f, axis, args=self.args, **self.kwargs) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index 2499c90535469..e10486c17fcdd 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -292,6 +292,14 @@ def test_agg_cython_table_raises_series(series, func, expected): series.agg(func) +def test_agg_none_to_type(): + # GH PR#? + df = DataFrame({"a": [None]}) + msg = re.escape("int() argument must be a string") + with pytest.raises(TypeError, match=msg): + df.agg({"a": int}) + + def test_transform_none_to_type(): # GH#34377 df = DataFrame({"a": [None]}) From 44822efec6beb1facc5e488daa0f2199f294f35f Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sat, 20 Mar 2021 13:46:38 -0400 Subject: [PATCH 3/4] CLN: Don't catch TypeError in FrameApply.agg --- pandas/core/apply.py | 31 ++++++++++---------------- pandas/tests/apply/test_invalid_arg.py | 8 +++++++ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index 3a2c2d7124963..cb20f6c69e6f1 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -633,25 +633,18 @@ def agg(self): obj = self.obj axis = self.axis - try: - if axis == 1: - result = FrameRowApply( - obj.T, - self.orig_f, - self.raw, - self.result_type, - self.args, - self.kwargs, - ).agg() - result = result.T if result is not None else result - else: - result = super().agg() - except TypeError as err: - exc = TypeError( - "DataFrame constructor called with " - f"incompatible data and dtype: {err}" - ) - raise exc from err + if axis == 1: + result = FrameRowApply( + obj.T, + self.orig_f, + self.raw, + self.result_type, + self.args, + self.kwargs, + ).agg() + result = result.T if result is not None else result + else: + result = super().agg() if result is None: result = obj.apply(self.orig_f, axis, args=self.args, **self.kwargs) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index 690d6bed0cb9b..fe6fdf8157f6a 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -272,6 +272,14 @@ def test_agg_cython_table_raises_series(series, func, expected): series.agg(func) +def test_agg_none_to_type(): + # GH PR#? + df = DataFrame({"a": [None]}) + msg = re.escape("int() argument must be a string") + with pytest.raises(TypeError, match=msg): + df.agg({"a": int}) + + def test_transform_none_to_type(): # GH#34377 df = DataFrame({"a": [None]}) From d8e5f27705f5987823db063fce81cc87003dbd77 Mon Sep 17 00:00:00 2001 From: Richard Shadrach Date: Sat, 20 Mar 2021 13:51:01 -0400 Subject: [PATCH 4/4] GH# --- pandas/tests/apply/test_invalid_arg.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/tests/apply/test_invalid_arg.py b/pandas/tests/apply/test_invalid_arg.py index fe6fdf8157f6a..73bc5b14335d4 100644 --- a/pandas/tests/apply/test_invalid_arg.py +++ b/pandas/tests/apply/test_invalid_arg.py @@ -273,7 +273,7 @@ def test_agg_cython_table_raises_series(series, func, expected): def test_agg_none_to_type(): - # GH PR#? + # GH 40543 df = DataFrame({"a": [None]}) msg = re.escape("int() argument must be a string") with pytest.raises(TypeError, match=msg):