Skip to content

CLN: reachable cases in Reducer #29610

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 3 commits into from
Nov 14, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
48 changes: 16 additions & 32 deletions pandas/_libs/reduction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ cdef class Reducer:

else:

# we passed a series-like
if hasattr(dummy, 'values'):

typ = type(dummy)
index = getattr(dummy, 'index', None)
dummy = dummy.values
# we passed a Series
typ = type(dummy)
index = dummy.index
dummy = dummy.values

if dummy.dtype != self.arr.dtype:
raise ValueError('Dummy array must be same dtype')
Expand All @@ -99,9 +97,8 @@ cdef class Reducer:
cdef:
char* dummy_buf
ndarray arr, result, chunk
Py_ssize_t i, incr
Py_ssize_t i
flatiter it
bint has_labels
object res, name, labels, index
object cached_typ = None

Expand All @@ -110,43 +107,26 @@ cdef class Reducer:
dummy_buf = chunk.data
chunk.data = arr.data
labels = self.labels
has_labels = labels is not None
has_index = self.index is not None
incr = self.increment

result = np.empty(self.nresults, dtype='O')
it = <flatiter>PyArray_IterNew(result)

try:
for i in range(self.nresults):

if has_labels:
name = labels[i]
else:
name = None

# create the cached type
# each time just reassign the data
if i == 0:

if self.typ is not None:

# recreate with the index if supplied
if has_index:

cached_typ = self.typ(
chunk, index=self.index, name=name)

else:

# use the passsed typ, sans index
cached_typ = self.typ(chunk, name=name)
# In this case, we also have self.index
name = labels[i]
cached_typ = self.typ(chunk, index=self.index, name=name)

# use the cached_typ if possible
if cached_typ is not None:

if has_index:
object.__setattr__(cached_typ, 'index', self.index)
# In this case, we also have non-None labels
name = labels[i]

object.__setattr__(
cached_typ._data._block, 'values', chunk)
Expand Down Expand Up @@ -611,18 +591,22 @@ cdef class BlockSlider:
arr.shape[1] = 0


def compute_reduction(arr, f, axis=0, dummy=None, labels=None):
def compute_reduction(arr: np.ndarray, f, axis: int = 0, dummy=None, labels=None):
"""

Parameters
-----------
arr : NDFrame object
arr : np.ndarray
f : function
axis : integer axis
dummy : type of reduced output (series)
labels : Index or None
"""

# We either have both dummy and labels, or neither of them
if (labels is None) ^ (dummy is None):
raise ValueError("Must pass either dummy and labels, or neither")

if labels is not None:
# Caller is responsible for ensuring we don't have MultiIndex
assert labels.nlevels == 1
Expand Down
17 changes: 9 additions & 8 deletions pandas/tests/groupby/test_bin_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,16 @@ class TestMoments:
class TestReducer:
def test_int_index(self):
arr = np.random.randn(100, 4)
result = libreduction.compute_reduction(arr, np.sum, labels=Index(np.arange(4)))
expected = arr.sum(0)
tm.assert_almost_equal(result, expected)

result = libreduction.compute_reduction(
arr, np.sum, axis=1, labels=Index(np.arange(100))
)
expected = arr.sum(1)
tm.assert_almost_equal(result, expected)
msg = "Must pass either dummy and labels, or neither"
# we must pass either both labels and dummy, or neither
with pytest.raises(ValueError, match=msg):
libreduction.compute_reduction(arr, np.sum, labels=Index(np.arange(4)))

with pytest.raises(ValueError, match=msg):
libreduction.compute_reduction(
arr, np.sum, axis=1, labels=Index(np.arange(100))
)

dummy = Series(0.0, index=np.arange(100))
result = libreduction.compute_reduction(
Expand Down