Skip to content

Commit 0a7f0b4

Browse files
jbrockmendelMateusz Górski
authored and
Mateusz Górski
committed
CLN: reachable cases in Reducer (pandas-dev#29610)
1 parent 60b6df6 commit 0a7f0b4

File tree

2 files changed

+21
-33
lines changed

2 files changed

+21
-33
lines changed

pandas/_libs/reduction.pyx

+12-25
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ cdef class Reducer:
8181

8282
else:
8383

84-
# we passed a series-like
84+
# we passed a Series
8585
typ = type(dummy)
8686
index = dummy.index
8787
dummy = dummy.values
@@ -99,7 +99,6 @@ cdef class Reducer:
9999
ndarray arr, result, chunk
100100
Py_ssize_t i
101101
flatiter it
102-
bint has_labels
103102
object res, name, labels
104103
object cached_typ = None
105104

@@ -108,42 +107,26 @@ cdef class Reducer:
108107
dummy_buf = chunk.data
109108
chunk.data = arr.data
110109
labels = self.labels
111-
has_labels = labels is not None
112-
has_index = self.index is not None
113110

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

117114
try:
118115
for i in range(self.nresults):
119116

120-
if has_labels:
121-
name = labels[i]
122-
else:
123-
name = None
124-
125117
# create the cached type
126118
# each time just reassign the data
127119
if i == 0:
128120

129121
if self.typ is not None:
130-
131-
# recreate with the index if supplied
132-
if has_index:
133-
134-
cached_typ = self.typ(
135-
chunk, index=self.index, name=name)
136-
137-
else:
138-
139-
# use the passsed typ, sans index
140-
cached_typ = self.typ(chunk, name=name)
122+
# In this case, we also have self.index
123+
name = labels[i]
124+
cached_typ = self.typ(chunk, index=self.index, name=name)
141125

142126
# use the cached_typ if possible
143127
if cached_typ is not None:
144-
145-
if has_index:
146-
object.__setattr__(cached_typ, 'index', self.index)
128+
# In this case, we also have non-None labels
129+
name = labels[i]
147130

148131
object.__setattr__(
149132
cached_typ._data._block, 'values', chunk)
@@ -607,18 +590,22 @@ cdef class BlockSlider:
607590
arr.shape[1] = 0
608591

609592

610-
def compute_reduction(arr, f, axis=0, dummy=None, labels=None):
593+
def compute_reduction(arr: np.ndarray, f, axis: int = 0, dummy=None, labels=None):
611594
"""
612595
613596
Parameters
614597
-----------
615-
arr : NDFrame object
598+
arr : np.ndarray
616599
f : function
617600
axis : integer axis
618601
dummy : type of reduced output (series)
619602
labels : Index or None
620603
"""
621604

605+
# We either have both dummy and labels, or neither of them
606+
if (labels is None) ^ (dummy is None):
607+
raise ValueError("Must pass either dummy and labels, or neither")
608+
622609
if labels is not None:
623610
# Caller is responsible for ensuring we don't have MultiIndex
624611
assert labels.nlevels == 1

pandas/tests/groupby/test_bin_groupby.py

+9-8
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,16 @@ class TestMoments:
116116
class TestReducer:
117117
def test_int_index(self):
118118
arr = np.random.randn(100, 4)
119-
result = libreduction.compute_reduction(arr, np.sum, labels=Index(np.arange(4)))
120-
expected = arr.sum(0)
121-
tm.assert_almost_equal(result, expected)
122119

123-
result = libreduction.compute_reduction(
124-
arr, np.sum, axis=1, labels=Index(np.arange(100))
125-
)
126-
expected = arr.sum(1)
127-
tm.assert_almost_equal(result, expected)
120+
msg = "Must pass either dummy and labels, or neither"
121+
# we must pass either both labels and dummy, or neither
122+
with pytest.raises(ValueError, match=msg):
123+
libreduction.compute_reduction(arr, np.sum, labels=Index(np.arange(4)))
124+
125+
with pytest.raises(ValueError, match=msg):
126+
libreduction.compute_reduction(
127+
arr, np.sum, axis=1, labels=Index(np.arange(100))
128+
)
128129

129130
dummy = Series(0.0, index=np.arange(100))
130131
result = libreduction.compute_reduction(

0 commit comments

Comments
 (0)