Skip to content

Commit

Permalink
Remove warning handling
Browse files Browse the repository at this point in the history
  • Loading branch information
rhshadrach committed Sep 26, 2021
1 parent 9f47591 commit 4c06173
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 43 deletions.
30 changes: 4 additions & 26 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from collections import defaultdict
from functools import partial
import inspect
import re
from typing import (
TYPE_CHECKING,
Any,
Expand Down Expand Up @@ -339,12 +338,6 @@ def agg_list_like(self) -> DataFrame | Series:
keys = []
failed_names = []

depr_nuisance_columns_msg = (
"{} did not aggregate successfully. If any error is "
"raised this will raise in a future version of pandas. "
"Drop these columns/ops to avoid this warning."
)

# degenerate case
if selected_obj.ndim == 1:
for a in arg:
Expand All @@ -367,24 +360,7 @@ def agg_list_like(self) -> DataFrame | Series:
for index, col in enumerate(selected_obj):
colg = obj._gotitem(col, ndim=1, subset=selected_obj.iloc[:, index])
try:
# Capture and suppress any warnings emitted by us in the call
# to agg below, but pass through any warnings that were
# generated otherwise.
with warnings.catch_warnings(record=True) as record:
new_res = colg.aggregate(arg)
if len(record) > 0:
match = re.compile(depr_nuisance_columns_msg.format(".*"))
for warning in record:
if re.match(match, str(warning.message)):
failed_names.append(col)
else:
warnings.warn_explicit(
message=warning.message,
category=warning.category,
filename=warning.filename,
lineno=warning.lineno,
)

new_res = colg.aggregate(arg)
except (TypeError, DataError):
failed_names.append(col)
except ValueError as err:
Expand Down Expand Up @@ -412,7 +388,9 @@ def agg_list_like(self) -> DataFrame | Series:

if len(failed_names) > 0:
warnings.warn(
depr_nuisance_columns_msg.format(failed_names),
f"{failed_names} did not aggregate successfully. If any error is "
"raised this will raise in a future version of pandas. "
"Drop these columns/ops to avoid this warning.",
FutureWarning,
stacklevel=find_stack_level(),
)
Expand Down
17 changes: 0 additions & 17 deletions pandas/tests/apply/test_frame_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -1462,20 +1462,3 @@ def test_apply_getitem_axis_1():
result = df[["a", "a"]].apply(lambda x: x[0] + x[1], axis=1)
expected = Series([0, 2, 4])
tm.assert_series_equal(result, expected)


def test_nuisance_depr_passes_through_warnings():
# GH 43740
# DataFrame.agg with list-likes may emit warnings for both individual
# args and for entire columns, but we only want to emit once. We
# catch and suppress the warnings for individual args, but need to make
# sure if some other warnings were raised, they get passed through to
# the user.

def foo(x):
warnings.warn("Hello, World!")
return x.sum()

df = DataFrame({"a": [1, 2, 3]})
with tm.assert_produces_warning(UserWarning, match="Hello, World!"):
df.agg([foo])

0 comments on commit 4c06173

Please sign in to comment.