8000 PERF: perform reductions block-wise by jbrockmendel · Pull Request #29847 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

PERF: perform reductions block-wise #29847

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 15 commits into from
Jan 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
consolidate+simplify
  • Loading branch information
jbrockmendel committed Dec 22, 2019
commit ebb33c1bc93e88b62fc3d88ef5107c493b67cedc
37 changes: 16 additions & 21 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7593,31 +7593,26 @@ def _get_data(axis_matters):
raise NotImplementedError(msg)
return data

if self.size == 0:
pass
if numeric_only is not None and axis in [0, 1]:
df = self
if numeric_only is True:
df = _get_data(axis_matters=True)
if axis == 1:
df = df.T
axis = 0

out_dtype = "bool" if filter_type == "bool" else None

elif numeric_only is False:
res = self._data.reduce(op)
# After possibly _get_data and transposing, we are now in the
# simple case where we can use BlockManager._reduce
res = df._data.reduce(op, axis=1, skipna=skipna, **kwds)
assert isinstance(res, dict)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Planning to keep these asserts in?

assert len(res) == max(list(res.keys())) + 1, res.keys()
out = self._constructor_sliced(res, index=range(len(res)))
out.index = self.columns
if len(res):
assert len(res) == max(list(res.keys())) + 1, res.keys()
out = df._constructor_sliced(res, index=range(len(res)), dtype=out_dtype)
out.index = df.columns
return out

elif numeric_only is True and axis in [0, 1]:
data = _get_data(axis_matters=True)
if axis == 1:
data = data.T
return data._reduce(
op,
name,
axis=0,
skipna=skipna,
numeric_only=False,
filter_type=filter_type,
**kwds,
)

if numeric_only is None:
values = self.values
try:
Expand Down
11 changes: 3 additions & 8 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,19 +350,14 @@ def reduce(self, func, *args, **kwargs):
res = {}
for blk in self.blocks:
bres = func(blk.values, *args, **kwargs)
if np.ndim(bres) == 0 and blk.shape[0] != 1:
# i.e. we reduced over all axes and not just one; re-do column-wise
new_res = {
blk.mgr_locs.as_array[i]: func(blk.values[i], *args, **kwargs)
for i in range(len(blk.values))
}
elif np.ndim(bres) == 0:

if np.ndim(bres) == 0:
# EA
assert blk.shape[0] == 1
new_res = zip(blk.mgr_locs.as_array, [bres])
else:
assert bres.ndim == 1, bres.shape
assert blk.shape[0] == len(bres)
assert blk.shape[0] == len(bres), (blk.shape, bres.shape, args, kwargs)
new_res = zip(blk.mgr_locs.as_array, bres)

nr = dict(new_res)
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ def reduction(values, axis=None, skipna=True, mask=None):
try:
result = getattr(values, meth)(axis, dtype=dtype_max)
result.fill(np.nan)
except (AttributeError, TypeError, ValueError, np.core._internal.AxisError):
except (AttributeError, TypeError, ValueError):
result = np.nan
else:
result = getattr(values, meth)(axis)
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/frame/test_reductions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
Tests for DataFrame reductions that are DataFrame-specific, i.e. cannot
be shared in tests.reductions.
"""
import pandas as pd
import numpy as np


if True:#def test_blockwise_reduction():
arr = np.arange(10)
tdarr = arr.astype("m8[s]")
df = pd.DataFrame({"A": arr, "B": tdarr})
0