Skip to content

REF: de-duplicate piece of DataFrame._reduce #29830

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7606,6 +7606,23 @@ def _reduce(
def f(x):
return op(x, axis=axis, skipna=skipna, **kwds)

def _get_data(axis_matters):
if filter_type is None or filter_type == "numeric":
data = self._get_numeric_data()
elif filter_type == "bool":
if axis_matters:
# GH#25101, GH#24434
data = self._get_bool_data() if axis == 0 else self
else:
data = self._get_bool_data()
else: # pragma: no cover
msg = (
"Generating numeric_only data with filter_type {f}"
"not supported.".format(f=filter_type)
)
raise NotImplementedError(msg)
return data

if numeric_only is None:
values = self.values
try:
Expand All @@ -7616,7 +7633,7 @@ def f(x):
# TODO: combine with hasattr(result, 'dtype') further down
# hard since we don't have `values` down there.
result = np.bool_(result)
except TypeError as err:
except TypeError:
# e.g. in nanops trying to convert strs to float

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

if filter_type is None or filter_type == "numeric":
data = self._get_numeric_data()
elif filter_type == "bool":
data = self._get_bool_data()
else: # pragma: no cover
raise NotImplementedError(
"Handling exception with filter_type {f} not"
"implemented.".format(f=filter_type)
) from err
# TODO: why doesnt axis matter here?
data = _get_data(axis_matters=False)
with np.errstate(all="ignore"):
result = f(data.values)
labels = data._get_agg_axis(axis)
else:
if numeric_only:
if filter_type is None or filter_type == "numeric":
data = self._get_numeric_data()
elif filter_type == "bool":
# GH 25101, # GH 24434
data = self._get_bool_data() if axis == 0 else self
else: # pragma: no cover
msg = (
"Generating numeric_only data with filter_type {f}"
"not supported.".format(f=filter_type)
)
raise NotImplementedError(msg)
data = _get_data(axis_matters=True)

values = data.values
labels = data._get_agg_axis(axis)
else:
Expand Down