Skip to content

BUG: pd.DataFrame.transform recursively loops in some cases #34224 #34377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7219,8 +7219,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
Expand Down
16 changes: 14 additions & 2 deletions pandas/tests/series/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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):
Expand Down