diff --git a/doc/source/whatsnew/v1.1.0.rst b/doc/source/whatsnew/v1.1.0.rst index 6e8cbc34be062..afeee24dfe290 100644 --- a/doc/source/whatsnew/v1.1.0.rst +++ b/doc/source/whatsnew/v1.1.0.rst @@ -960,6 +960,7 @@ Reshaping - Bug in :func:`concat` was not allowing for concatenation of ``DataFrame`` and ``Series`` with duplicate keys (:issue:`33654`) - Bug in :func:`cut` raised an error when non-unique labels (:issue:`33141`) - Ensure only named functions can be used in :func:`eval()` (:issue:`32460`) +- Bug in :func:`Dataframe.aggregate` and :func:`Series.aggregate` was causing recursive loop in some cases (:issue:`34224`) - Fixed bug in :func:`melt` where melting MultiIndex columns with ``col_level`` > 0 would raise a ``KeyError`` on ``id_vars`` (:issue:`34129`) Sparse diff --git a/pandas/core/frame.py b/pandas/core/frame.py index dfc938918f03a..d48fc9aeddca5 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -7333,8 +7333,12 @@ def aggregate(self, func, axis=0, *args, **kwargs): result = None try: result, how = self._aggregate(func, axis=axis, *args, **kwargs) - except TypeError: - pass + except TypeError as err: + exc = TypeError( + "DataFrame constructor called with " + f"incompatible data and dtype: {err}" + ) + raise exc from err if result is None: return self.apply(func, axis=axis, args=args, **kwargs) return result diff --git a/pandas/tests/series/test_apply.py b/pandas/tests/series/test_apply.py index 589f8933efa96..e6f86dda05893 100644 --- a/pandas/tests/series/test_apply.py +++ b/pandas/tests/series/test_apply.py @@ -42,7 +42,9 @@ def test_apply(self, datetime_series): def test_apply_same_length_inference_bug(self): s = Series([1, 2]) - f = lambda x: (x, x + 1) + + def f(x): + return (x, x + 1) result = s.apply(f) expected = s.map(f) @@ -56,7 +58,9 @@ def test_apply_same_length_inference_bug(self): def test_apply_dont_convert_dtype(self): s = Series(np.random.randn(10)) - f = lambda x: x if x > 0 else np.nan + def f(x): + return x if x > 0 else np.nan + result = s.apply(f, convert_dtype=False) assert result.dtype == object @@ -459,6 +463,14 @@ def test_agg_cython_table_raises(self, series, func, expected): # e.g. Series('a b'.split()).cumprod() will raise series.agg(func) + def test_transform_none_to_type(self): + # 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}) + class TestSeriesMap: def test_map(self, datetime_series):