Skip to content

Commit 433c900

Browse files
authored
BUG: pd.DataFrame.transform recursively loops in some cases #34224 (#34377)
1 parent a7d7d18 commit 433c900

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,7 @@ Reshaping
10031003
- Bug in :func:`concat` was not allowing for concatenation of ``DataFrame`` and ``Series`` with duplicate keys (:issue:`33654`)
10041004
- Bug in :func:`cut` raised an error when non-unique labels (:issue:`33141`)
10051005
- Ensure only named functions can be used in :func:`eval()` (:issue:`32460`)
1006+
- Bug in :func:`Dataframe.aggregate` and :func:`Series.aggregate` was causing recursive loop in some cases (:issue:`34224`)
10061007
- Fixed bug in :func:`melt` where melting MultiIndex columns with ``col_level`` > 0 would raise a ``KeyError`` on ``id_vars`` (:issue:`34129`)
10071008

10081009
Sparse

pandas/core/frame.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -7333,8 +7333,12 @@ def aggregate(self, func, axis=0, *args, **kwargs):
73337333
result = None
73347334
try:
73357335
result, how = self._aggregate(func, axis=axis, *args, **kwargs)
7336-
except TypeError:
7337-
pass
7336+
except TypeError as err:
7337+
exc = TypeError(
7338+
"DataFrame constructor called with "
7339+
f"incompatible data and dtype: {err}"
7340+
)
7341+
raise exc from err
73387342
if result is None:
73397343
return self.apply(func, axis=axis, args=args, **kwargs)
73407344
return result

pandas/tests/series/test_apply.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ def test_apply(self, datetime_series):
4242

4343
def test_apply_same_length_inference_bug(self):
4444
s = Series([1, 2])
45-
f = lambda x: (x, x + 1)
45+
46+
def f(x):
47+
return (x, x + 1)
4648

4749
result = s.apply(f)
4850
expected = s.map(f)
@@ -56,7 +58,9 @@ def test_apply_same_length_inference_bug(self):
5658
def test_apply_dont_convert_dtype(self):
5759
s = Series(np.random.randn(10))
5860

59-
f = lambda x: x if x > 0 else np.nan
61+
def f(x):
62+
return x if x > 0 else np.nan
63+
6064
result = s.apply(f, convert_dtype=False)
6165
assert result.dtype == object
6266

@@ -459,6 +463,14 @@ def test_agg_cython_table_raises(self, series, func, expected):
459463
# e.g. Series('a b'.split()).cumprod() will raise
460464
series.agg(func)
461465

466+
def test_transform_none_to_type(self):
467+
# GH34377
468+
df = pd.DataFrame({"a": [None]})
469+
470+
msg = "DataFrame constructor called with incompatible data and dtype"
471+
with pytest.raises(TypeError, match=msg):
472+
df.transform({"a": int})
473+
462474

463475
class TestSeriesMap:
464476
def test_map(self, datetime_series):

0 commit comments

Comments
 (0)