Skip to content

Commit bf86a8d

Browse files
Thomas Lautenschlaegerthlautenschlaeger
Thomas Lautenschlaeger
authored andcommitted
applied patch
1 parent 435347f commit bf86a8d

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
@@ -64,7 +64,6 @@
6464
from pandas.core.resample import Resampler
6565
from pandas.core.window.rolling import BaseWindow
6666

67-
6867
ResType = Dict[int, Any]
6968

7069

@@ -285,10 +284,9 @@ def transform_str_or_callable(self, func) -> DataFrame | Series:
285284
return func(obj, *args, **kwargs)
286285

287286
def _filter_numeric_only(self) -> list[Any]:
288-
if "numeric_only" in self.kwargs and self.kwargs["numeric_only"] is True:
287+
if "numeric_only" in self.kwargs and bool(self.kwargs["numeric_only"]) is True:
289288
obj = self.obj._get_numeric_data()
290-
filtered_cols = list(set(self.obj) - set(obj))
291-
self.obj = obj
289+
filtered_cols = list(obj)
292290
return filtered_cols
293291
return []
294292

@@ -302,8 +300,9 @@ def agg_list_like(self) -> DataFrame | Series:
302300
"""
303301
from pandas.core.reshape.concat import concat
304302

305-
self._filter_numeric_only()
306-
obj = self.obj
303+
filtered_cols = self._filter_numeric_only()
304+
n = len(filtered_cols)
305+
obj = self.obj if n == 0 else self.obj[filtered_cols].astype("O")
307306
arg = cast(List[AggFuncTypeBase], self.f)
308307

309308
if getattr(obj, "axis", 0) == 1:
@@ -377,11 +376,8 @@ def agg_dict_like(self) -> DataFrame | Series:
377376
from pandas import Index
378377
from pandas.core.reshape.concat import concat
379378

380-
filtered_col = self._filter_numeric_only()
381-
382379
obj = self.obj
383380
arg = cast(AggFuncTypeDict, self.f)
384-
arg = {k: arg[k] for k in arg.keys() if k not in filtered_col}
385381

386382
if getattr(obj, "axis", 0) == 1:
387383
raise NotImplementedError("axis other than 0 is not supported")
@@ -1181,7 +1177,6 @@ def reconstruct_func(
11811177

11821178
if not relabeling:
11831179
if isinstance(func, list) and len(func) > len(set(func)):
1184-
11851180
# GH 28426 will raise error if duplicated function names are used and
11861181
# there is no reassigned name
11871182
raise SpecificationError(

pandas/core/frame.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9221,10 +9221,10 @@ def _gotitem(
92219221
... ['c', 3, 6]],
92229222
... columns=['A', 'B', 'C'])
92239223
9224-
Works equivalently as above. Add argument `numeric_only=True` to avoid
9225-
exceptions or warnings.
9224+
Works equivalently as above. Add argument `numeric_only=True` to
9225+
aggregate only numeric columns.
92269226
9227-
>>> df.agg({'A': 'mean', 'B': [pd.DataFrame.mean, 'std'], 'C': ['sum', 'mean']},
9227+
>>> df.agg({'B': ['mean', 'std'], 'C': ['sum', 'mean']},
92289228
... numeric_only=True)
92299229
B C
92309230
mean 2.0 5.0
@@ -9394,6 +9394,9 @@ def apply(
93949394
Functions that mutate the passed object can produce unexpected
93959395
behavior or errors and are not supported. See :ref:`gotchas.udf-mutation`
93969396
for more details.
9397+
Use the keyword argument `numeric_only=True` to apply functions
9398+
only to numeric columns and to skip the non-numeric columns.
9399+
e.g. the column contains a string.
93979400
93989401
Examples
93999402
--------

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)