Skip to content

Commit 10ffdde

Browse files
author
Thomas Lautenschlaeger
committed
applied patch
1 parent 85be2ec commit 10ffdde

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

pandas/core/apply.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@
7373
from pandas.core.resample import Resampler
7474
from pandas.core.window.rolling import BaseWindow
7575

76-
7776
ResType = Dict[int, Any]
7877

7978

@@ -294,10 +293,9 @@ def transform_str_or_callable(self, func) -> DataFrame | Series:
294293
return func(obj, *args, **kwargs)
295294

296295
def _filter_numeric_only(self) -> list[Any]:
297-
if "numeric_only" in self.kwargs and self.kwargs["numeric_only"] is True:
296+
if "numeric_only" in self.kwargs and bool(self.kwargs["numeric_only"]) is True:
298297
obj = self.obj._get_numeric_data()
299-
filtered_cols = list(set(self.obj) - set(obj))
300-
self.obj = obj
298+
filtered_cols = list(obj)
301299
return filtered_cols
302300
return []
303301

@@ -311,8 +309,9 @@ def agg_list_like(self) -> DataFrame | Series:
311309
"""
312310
from pandas.core.reshape.concat import concat
313311

314-
self._filter_numeric_only()
315-
obj = self.obj
312+
filtered_cols = self._filter_numeric_only()
313+
n = len(filtered_cols)
314+
obj = self.obj if n == 0 else self.obj[filtered_cols].astype("O")
316315
arg = cast(List[AggFuncTypeBase], self.f)
317316

318317
if getattr(obj, "axis", 0) == 1:
@@ -446,11 +445,8 @@ def agg_dict_like(self) -> DataFrame | Series:
446445
from pandas import Index
447446
from pandas.core.reshape.concat import concat
448447

449-
filtered_col = self._filter_numeric_only()
450-
451448
obj = self.obj
452449
arg = cast(AggFuncTypeDict, self.f)
453-
arg = {k: arg[k] for k in arg.keys() if k not in filtered_col}
454450

455451
if getattr(obj, "axis", 0) == 1:
456452
raise NotImplementedError("axis other than 0 is not supported")
@@ -1257,7 +1253,6 @@ def reconstruct_func(
12571253

12581254
if not relabeling:
12591255
if isinstance(func, list) and len(func) > len(set(func)):
1260-
12611256
# GH 28426 will raise error if duplicated function names are used and
12621257
# there is no reassigned name
12631258
raise SpecificationError(

pandas/core/frame.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9282,10 +9282,10 @@ def _gotitem(
92829282
... ['c', 3, 6]],
92839283
... columns=['A', 'B', 'C'])
92849284
9285-
Works equivalently as above. Add argument `numeric_only=True` to avoid
9286-
exceptions or warnings.
9285+
Works equivalently as above. Add argument `numeric_only=True` to
9286+
aggregate only numeric columns.
92879287
9288-
>>> df.agg({'A': 'mean', 'B': [pd.DataFrame.mean, 'std'], 'C': ['sum', 'mean']},
9288+
>>> df.agg({'B': ['mean', 'std'], 'C': ['sum', 'mean']},
92899289
... numeric_only=True)
92909290
B C
92919291
mean 2.0 5.0
@@ -9454,6 +9454,9 @@ def apply(
94549454
Functions that mutate the passed object can produce unexpected
94559455
behavior or errors and are not supported. See :ref:`gotchas.udf-mutation`
94569456
for more details.
9457+
Use the keyword argument `numeric_only=True` to apply functions
9458+
only to numeric columns and to skip the non-numeric columns.
9459+
e.g. the column contains a string.
94579460
94589461
Examples
94599462
--------

pandas/core/shared_docs.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
for more details.
4747
4848
A passed user-defined-function will be passed a Series for evaluation.
49+
50+
Use the keyword argument `numeric_only=True` to apply functions
51+
only to numeric columns and to skip the non-numeric columns.
52+
e.g. the column contains a string.
4953
{examples}"""
5054

5155
_shared_docs[

0 commit comments

Comments
 (0)