Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
d1d07ff
REF: implement DataFrame reductions blockwsie
jbrockmendel Nov 25, 2019
9469400
handle axis==1 with numeric_only
jbrockmendel Nov 25, 2019
038697a
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel Nov 25, 2019
94a2ee1
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel Nov 26, 2019
9e21d77
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel Nov 26, 2019
237253a
clean up assertions
jbrockmendel Nov 26, 2019
8757c8a
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel Nov 27, 2019
a1b653d
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel Dec 8, 2019
f8c3d24
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel Dec 20, 2019
ebb33c1
consolidate+simplify
jbrockmendel Dec 22, 2019
c0eb05c
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel Dec 22, 2019
4a16663
revert file not intended
jbrockmendel Dec 22, 2019
e4c0466
'Merge branch 'master' of https://github.com/pandas-dev/pandas into p…
jbrockmendel Dec 26, 2019
3e7da1e
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel Dec 27, 2019
9370b1a
Merge branch 'master' of https://github.com/pandas-dev/pandas into pe…
jbrockmendel Dec 27, 2019
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
25 changes: 25 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -7623,6 +7623,31 @@ def _get_data(axis_matters):
raise NotImplementedError(msg)
return data

if self.size == 0:
pass

elif numeric_only is False:
res = self._data.reduce(op)
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
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
31 changes: 31 additions & 0 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,37 @@ def _verify_integrity(self):
"tot_items: {1}".format(len(self.items), tot_items)
)

def reduce(self, func, *args, **kwargs):
# If 2D, we assume that we're operating column-wise
if self.ndim == 1:
# we'll be returning a scalar
blk = self.blocks[0]
return func(blk.values, *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
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't quite understand this case. How do we get here? re-calling func doesn't seem ideal.

Copy link
Member Author

Choose a reason for hiding this comment

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

IIRC it was when we have axis=None

Copy link
Member Author

Choose a reason for hiding this comment

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

after some digging, this appears to be coming from nanops funcs that are either not getting axis passed or are not handling it correctly.

Copy link
Member Author

Choose a reason for hiding this comment

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

updated to make this check unnecessary

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:
# 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)
new_res = zip(blk.mgr_locs.as_array, bres)

nr = dict(new_res)
assert not any(key in res for key in nr)
res.update(nr)

return res

def apply(
self,
f,
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@ def test_omit_nuisance(df):

# won't work with axis = 1
grouped = df.groupby({"A": 0, "C": 0, "D": 1, "E": 1}, axis=1)
msg = r"unsupported operand type\(s\) for \+: 'Timestamp'"
msg = "reduction operation 'sum' not allowed for this dtype"
with pytest.raises(TypeError, match=msg):
grouped.agg(lambda x: x.sum(0, numeric_only=False))

Expand Down