Skip to content

Commit b838508

Browse files
authored
REF: move mixed-dtype frame_apply check outside of _reduce try/except (#32950)
1 parent 914c390 commit b838508

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

pandas/core/frame.py

+30-20
Original file line numberDiff line numberDiff line change
@@ -7996,42 +7996,52 @@ def blk_func(values):
79967996
out[:] = coerce_to_dtypes(out.values, df.dtypes)
79977997
return out
79987998

7999+
if not self._is_homogeneous_type:
8000+
# try to avoid self.values call
8001+
8002+
if filter_type is None and axis == 0 and len(self) > 0:
8003+
# operate column-wise
8004+
8005+
# numeric_only must be None here, as other cases caught above
8006+
# require len(self) > 0 bc frame_apply messes up empty prod/sum
8007+
8008+
# this can end up with a non-reduction
8009+
# but not always. if the types are mixed
8010+
# with datelike then need to make sure a series
8011+
8012+
# we only end up here if we have not specified
8013+
# numeric_only and yet we have tried a
8014+
# column-by-column reduction, where we have mixed type.
8015+
# So let's just do what we can
8016+
from pandas.core.apply import frame_apply
8017+
8018+
opa = frame_apply(
8019+
self, func=f, result_type="expand", ignore_failures=True
8020+
)
8021+
result = opa.get_result()
8022+
if result.ndim == self.ndim:
8023+
result = result.iloc[0].rename(None)
8024+
return result
8025+
8026+
data = self
79998027
if numeric_only is None:
80008028
data = self
80018029
values = data.values
8030+
80028031
try:
80038032
result = f(values)
80048033

80058034
except TypeError:
80068035
# e.g. in nanops trying to convert strs to float
80078036

8008-
# try by-column first
8009-
if filter_type is None and axis == 0:
8010-
# this can end up with a non-reduction
8011-
# but not always. if the types are mixed
8012-
# with datelike then need to make sure a series
8013-
8014-
# we only end up here if we have not specified
8015-
# numeric_only and yet we have tried a
8016-
# column-by-column reduction, where we have mixed type.
8017-
# So let's just do what we can
8018-
from pandas.core.apply import frame_apply
8019-
8020-
opa = frame_apply(
8021-
self, func=f, result_type="expand", ignore_failures=True
8022-
)
8023-
result = opa.get_result()
8024-
if result.ndim == self.ndim:
8025-
result = result.iloc[0]
8026-
return result
8027-
80288037
# TODO: why doesnt axis matter here?
80298038
data = _get_data(axis_matters=False)
80308039
labels = data._get_agg_axis(axis)
80318040

80328041
values = data.values
80338042
with np.errstate(all="ignore"):
80348043
result = f(values)
8044+
80358045
else:
80368046
if numeric_only:
80378047
data = _get_data(axis_matters=True)

pandas/tests/frame/test_analytics.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,9 @@ def kurt(x):
346346
"sum", np.sum, float_frame_with_na, skipna_alternative=np.nansum
347347
)
348348
assert_stat_op_calc("mean", np.mean, float_frame_with_na, check_dates=True)
349-
assert_stat_op_calc("product", np.prod, float_frame_with_na)
349+
assert_stat_op_calc(
350+
"product", np.prod, float_frame_with_na, skipna_alternative=np.nanprod
351+
)
350352

351353
assert_stat_op_calc("mad", mad, float_frame_with_na)
352354
assert_stat_op_calc("var", var, float_frame_with_na)

pandas/tests/frame/test_missing.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,12 @@ def test_fillna_categorical_nan(self):
372372
cat = Categorical([np.nan, 2, np.nan])
373373
val = Categorical([np.nan, np.nan, np.nan])
374374
df = DataFrame({"cats": cat, "vals": val})
375-
with tm.assert_produces_warning(RuntimeWarning):
376-
res = df.fillna(df.median())
375+
376+
# GH#32950 df.median() is poorly behaved because there is no
377+
# Categorical.median
378+
median = Series({"cats": 2.0, "vals": np.nan})
379+
380+
res = df.fillna(median)
377381
v_exp = [np.nan, np.nan, np.nan]
378382
df_exp = DataFrame({"cats": [2, 2, 2], "vals": v_exp}, dtype="category")
379383
tm.assert_frame_equal(res, df_exp)

0 commit comments

Comments
 (0)