Skip to content

Commit

Permalink
applied patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Lautenschlaeger authored and thlautenschlaeger committed Nov 26, 2022
1 parent 435347f commit bf86a8d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
15 changes: 5 additions & 10 deletions pandas/core/apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
from pandas.core.resample import Resampler
from pandas.core.window.rolling import BaseWindow


ResType = Dict[int, Any]


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

def _filter_numeric_only(self) -> list[Any]:
if "numeric_only" in self.kwargs and self.kwargs["numeric_only"] is True:
if "numeric_only" in self.kwargs and bool(self.kwargs["numeric_only"]) is True:
obj = self.obj._get_numeric_data()
filtered_cols = list(set(self.obj) - set(obj))
self.obj = obj
filtered_cols = list(obj)
return filtered_cols
return []

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

self._filter_numeric_only()
obj = self.obj
filtered_cols = self._filter_numeric_only()
n = len(filtered_cols)
obj = self.obj if n == 0 else self.obj[filtered_cols].astype("O")
arg = cast(List[AggFuncTypeBase], self.f)

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

filtered_col = self._filter_numeric_only()

obj = self.obj
arg = cast(AggFuncTypeDict, self.f)
arg = {k: arg[k] for k in arg.keys() if k not in filtered_col}

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

if not relabeling:
if isinstance(func, list) and len(func) > len(set(func)):

# GH 28426 will raise error if duplicated function names are used and
# there is no reassigned name
raise SpecificationError(
Expand Down
9 changes: 6 additions & 3 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9221,10 +9221,10 @@ def _gotitem(
... ['c', 3, 6]],
... columns=['A', 'B', 'C'])
Works equivalently as above. Add argument `numeric_only=True` to avoid
exceptions or warnings.
Works equivalently as above. Add argument `numeric_only=True` to
aggregate only numeric columns.
>>> df.agg({'A': 'mean', 'B': [pd.DataFrame.mean, 'std'], 'C': ['sum', 'mean']},
>>> df.agg({'B': ['mean', 'std'], 'C': ['sum', 'mean']},
... numeric_only=True)
B C
mean 2.0 5.0
Expand Down Expand Up @@ -9394,6 +9394,9 @@ def apply(
Functions that mutate the passed object can produce unexpected
behavior or errors and are not supported. See :ref:`gotchas.udf-mutation`
for more details.
Use the keyword argument `numeric_only=True` to apply functions
only to numeric columns and to skip the non-numeric columns.
e.g. the column contains a string.
Examples
--------
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/shared_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
for more details.
A passed user-defined-function will be passed a Series for evaluation.
Use the keyword argument `numeric_only=True` to apply functions
only to numeric columns and to skip the non-numeric columns.
e.g. the column contains a string.
{examples}"""

_shared_docs[
Expand Down

0 comments on commit bf86a8d

Please sign in to comment.