Skip to content

Commit 00b1d34

Browse files
jbrockmendeljreback
authored andcommitted
REF: de-duplicate piece of DataFrame._reduce (pandas-dev#29830)
1 parent 06790d7 commit 00b1d34

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

pandas/core/frame.py

+22-21
Original file line numberDiff line numberDiff line change
@@ -7606,6 +7606,23 @@ def _reduce(
76067606
def f(x):
76077607
return op(x, axis=axis, skipna=skipna, **kwds)
76087608

7609+
def _get_data(axis_matters):
7610+
if filter_type is None or filter_type == "numeric":
7611+
data = self._get_numeric_data()
7612+
elif filter_type == "bool":
7613+
if axis_matters:
7614+
# GH#25101, GH#24434
7615+
data = self._get_bool_data() if axis == 0 else self
7616+
else:
7617+
data = self._get_bool_data()
7618+
else: # pragma: no cover
7619+
msg = (
7620+
"Generating numeric_only data with filter_type {f}"
7621+
"not supported.".format(f=filter_type)
7622+
)
7623+
raise NotImplementedError(msg)
7624+
return data
7625+
76097626
if numeric_only is None:
76107627
values = self.values
76117628
try:
@@ -7616,7 +7633,7 @@ def f(x):
76167633
# TODO: combine with hasattr(result, 'dtype') further down
76177634
# hard since we don't have `values` down there.
76187635
result = np.bool_(result)
7619-
except TypeError as err:
7636+
except TypeError:
76207637
# e.g. in nanops trying to convert strs to float
76217638

76227639
# try by-column first
@@ -7639,31 +7656,15 @@ def f(x):
76397656
result = result.iloc[0]
76407657
return result
76417658

7642-
if filter_type is None or filter_type == "numeric":
7643-
data = self._get_numeric_data()
7644-
elif filter_type == "bool":
7645-
data = self._get_bool_data()
7646-
else: # pragma: no cover
7647-
raise NotImplementedError(
7648-
"Handling exception with filter_type {f} not"
7649-
"implemented.".format(f=filter_type)
7650-
) from err
7659+
# TODO: why doesnt axis matter here?
7660+
data = _get_data(axis_matters=False)
76517661
with np.errstate(all="ignore"):
76527662
result = f(data.values)
76537663
labels = data._get_agg_axis(axis)
76547664
else:
76557665
if numeric_only:
7656-
if filter_type is None or filter_type == "numeric":
7657-
data = self._get_numeric_data()
7658-
elif filter_type == "bool":
7659-
# GH 25101, # GH 24434
7660-
data = self._get_bool_data() if axis == 0 else self
7661-
else: # pragma: no cover
7662-
msg = (
7663-
"Generating numeric_only data with filter_type {f}"
7664-
"not supported.".format(f=filter_type)
7665-
)
7666-
raise NotImplementedError(msg)
7666+
data = _get_data(axis_matters=True)
7667+
76677668
values = data.values
76687669
labels = data._get_agg_axis(axis)
76697670
else:

0 commit comments

Comments
 (0)