Skip to content

Commit c32a441

Browse files
Revert "REF: move mixed-dtype frame_apply check outside of _reduce try/except (pandas-dev#32950)"
This reverts commit b838508.
1 parent a4e19fa commit c32a441

File tree

3 files changed

+23
-38
lines changed

3 files changed

+23
-38
lines changed

pandas/core/frame.py

+20-29
Original file line numberDiff line numberDiff line change
@@ -8539,51 +8539,42 @@ def blk_func(values):
85398539
out[:] = coerce_to_dtypes(out.values, df.dtypes)
85408540
return out
85418541

8542-
if not self._is_homogeneous_type:
8543-
# try to avoid self.values call
8544-
8545-
if filter_type is None and axis == 0 and len(self) > 0:
8546-
# operate column-wise
8547-
8548-
# numeric_only must be None here, as other cases caught above
8549-
# require len(self) > 0 bc frame_apply messes up empty prod/sum
8550-
8551-
# this can end up with a non-reduction
8552-
# but not always. if the types are mixed
8553-
# with datelike then need to make sure a series
8554-
8555-
# we only end up here if we have not specified
8556-
# numeric_only and yet we have tried a
8557-
# column-by-column reduction, where we have mixed type.
8558-
# So let's just do what we can
8559-
from pandas.core.apply import frame_apply
8560-
8561-
opa = frame_apply(
8562-
self, func=f, result_type="expand", ignore_failures=True
8563-
)
8564-
result = opa.get_result()
8565-
if result.ndim == self.ndim:
8566-
result = result.iloc[0].rename(None)
8567-
return result
8568-
85698542
if numeric_only is None:
85708543
data = self
85718544
values = data.values
8572-
85738545
try:
85748546
result = f(values)
85758547

85768548
except TypeError:
85778549
# e.g. in nanops trying to convert strs to float
85788550

8551+
# try by-column first
8552+
if filter_type is None and axis == 0:
8553+
# this can end up with a non-reduction
8554+
# but not always. if the types are mixed
8555+
# with datelike then need to make sure a series
8556+
8557+
# we only end up here if we have not specified
8558+
# numeric_only and yet we have tried a
8559+
# column-by-column reduction, where we have mixed type.
8560+
# So let's just do what we can
8561+
from pandas.core.apply import frame_apply
8562+
8563+
opa = frame_apply(
8564+
self, func=f, result_type="expand", ignore_failures=True
8565+
)
8566+
result = opa.get_result()
8567+
if result.ndim == self.ndim:
8568+
result = result.iloc[0]
8569+
return result
8570+
85798571
# TODO: why doesnt axis matter here?
85808572
data = _get_data(axis_matters=False)
85818573
labels = data._get_agg_axis(axis)
85828574

85838575
values = data.values
85848576
with np.errstate(all="ignore"):
85858577
result = f(values)
8586-
85878578
else:
85888579
if numeric_only:
85898580
data = _get_data(axis_matters=True)

pandas/tests/frame/test_analytics.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,7 @@ def kurt(x):
351351
"sum", np.sum, float_frame_with_na, skipna_alternative=np.nansum
352352
)
353353
assert_stat_op_calc("mean", np.mean, float_frame_with_na, check_dates=True)
354-
assert_stat_op_calc(
355-
"product", np.prod, float_frame_with_na, skipna_alternative=np.nanprod
356-
)
354+
assert_stat_op_calc("product", np.prod, float_frame_with_na)
357355

358356
assert_stat_op_calc("mad", mad, float_frame_with_na)
359357
assert_stat_op_calc("var", var, float_frame_with_na)

pandas/tests/frame/test_missing.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -372,12 +372,8 @@ 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-
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)
375+
with tm.assert_produces_warning(RuntimeWarning):
376+
res = df.fillna(df.median())
381377
v_exp = [np.nan, np.nan, np.nan]
382378
df_exp = DataFrame({"cats": [2, 2, 2], "vals": v_exp}, dtype="category")
383379
tm.assert_frame_equal(res, df_exp)

0 commit comments

Comments
 (0)