Skip to content

Commit 0ccda51

Browse files
committed
Add test
1 parent 4bfe322 commit 0ccda51

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

pandas/_libs/groupby.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ def group_nth(rank_t[:, :] out,
985985
int64_t[:] counts,
986986
ndarray[rank_t, ndim=2] values,
987987
const int64_t[:] labels,
988-
int64_t min_count=0, int64_t rank=1
988+
int64_t min_count=1, int64_t rank=1
989989
):
990990
"""
991991
Only aggregates on axis=0

pandas/core/groupby/groupby.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1654,19 +1654,19 @@ def prod(self, numeric_only: bool = True, min_count: int = 0):
16541654
numeric_only=numeric_only, min_count=min_count, alias="prod", npfunc=np.prod
16551655
)
16561656

1657-
@doc(_groupby_agg_method_template, fname="min", no=False, mc=-1)
1657+
@doc(_groupby_agg_method_template, fname="min", no=False, mc=1)
16581658
def min(self, numeric_only: bool = False, min_count: int = 1):
16591659
return self._agg_general(
16601660
numeric_only=numeric_only, min_count=min_count, alias="min", npfunc=np.min
16611661
)
16621662

1663-
@doc(_groupby_agg_method_template, fname="max", no=False, mc=-1)
1663+
@doc(_groupby_agg_method_template, fname="max", no=False, mc=1)
16641664
def max(self, numeric_only: bool = False, min_count: int = 1):
16651665
return self._agg_general(
16661666
numeric_only=numeric_only, min_count=min_count, alias="max", npfunc=np.max
16671667
)
16681668

1669-
@doc(_groupby_agg_method_template, fname="first", no=False, mc=-1)
1669+
@doc(_groupby_agg_method_template, fname="first", no=False, mc=1)
16701670
def first(self, numeric_only: bool = False, min_count: int = 1):
16711671
def first_compat(obj: FrameOrSeries, axis: int = 0):
16721672
def first(x: Series):
@@ -1690,7 +1690,7 @@ def first(x: Series):
16901690
npfunc=first_compat,
16911691
)
16921692

1693-
@doc(_groupby_agg_method_template, fname="last", no=False, mc=-1)
1693+
@doc(_groupby_agg_method_template, fname="last", no=False, mc=1)
16941694
def last(self, numeric_only: bool = False, min_count: int = 1):
16951695
def last_compat(obj: FrameOrSeries, axis: int = 0):
16961696
def last(x: Series):

pandas/tests/groupby/aggregate/test_cython.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -166,23 +166,23 @@ def test__cython_agg_general(op, targop):
166166

167167

168168
@pytest.mark.parametrize(
169-
"op, targop",
169+
"op, targop, kwargs",
170170
[
171-
("mean", np.mean),
172-
("median", lambda x: np.median(x) if len(x) > 0 else np.nan),
173-
("var", lambda x: np.var(x, ddof=1)),
174-
("min", np.min),
175-
("max", np.max),
171+
("mean", np.mean, {}),
172+
("median", lambda x: np.median(x) if len(x) > 0 else np.nan, {}),
173+
("var", lambda x: np.var(x, ddof=1), {}),
174+
("min", np.min, {"min_count": 1}),
175+
("max", np.max, {"min_count": 1}),
176176
],
177177
)
178-
def test_cython_agg_empty_buckets(op, targop, observed):
178+
def test_cython_agg_empty_buckets(op, targop, kwargs, observed):
179179
df = DataFrame([11, 12, 13])
180180
grps = range(0, 55, 5)
181181

182182
# calling _cython_agg_general directly, instead of via the user API
183183
# which sets different values for min_count, so do that here.
184184
g = df.groupby(pd.cut(df[0], grps), observed=observed)
185-
result = g._cython_agg_general(op)
185+
result = g._cython_agg_general(op, **kwargs)
186186

187187
g = df.groupby(pd.cut(df[0], grps), observed=observed)
188188
expected = g.agg(lambda x: targop(x))

pandas/tests/groupby/aggregate/test_other.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,12 @@ def weird_func(x):
638638

639639
result = df["decimals"].groupby(df["id1"]).agg(weird_func)
640640
tm.assert_series_equal(result, expected, check_names=False)
641+
642+
643+
@pytest.mark.parametrize("func", ["first", "last", "max", "min"])
644+
def test_min_count_implementation_min_max_first_last(func):
645+
# GH#37821
646+
df = DataFrame({"a": [1] * 3, "b": [np.nan] * 3})
647+
result = getattr(df.groupby("a"), func)(min_count=2)
648+
expected = DataFrame({"b": [np.nan]}, index=Index([1], name="a"))
649+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)