Skip to content

Commit 1dde356

Browse files
author
Nick Eubank
committed
Fixes groupby.apply() error when no returns pandas-dev#9684
1 parent f0ac930 commit 1dde356

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

doc/source/whatsnew/v0.16.1.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,10 @@ Bug Fixes
9999

100100

101101
- Bug in ``transform`` causing length mismatch when null entries were present and a fast aggregator was being used (:issue:`9697`)
102-
103102
- Bug in ``equals`` causing false negatives when block order differed (:issue:`9330`)
104-
105103
- Bug in ``DataFrame`` slicing may not retain metadata (:issue:`9776`)
106104
- Bug where ``TimdeltaIndex`` were not properly serialized in fixed ``HDFStore`` (:issue:`9635`)
105+
- Fixed bug in ``groupby.apply()`` that would raise an error the user passed function either returned nothing or only returned values of ``None``. (:issue:`9685`)
107106

108107
- Bug in plotting continuously using ``secondary_y`` may not show legend properly. (:issue:`9610`, :issue:`9779`)
109108

@@ -121,3 +120,5 @@ Bug Fixes
121120
- Bug in which ``SparseDataFrame`` could not take `nan` as a column name (:issue:`8822`)
122121

123122
- Bug in unequal comparisons between a ``Series`` of dtype `"category"` and a scalar (e.g. ``Series(Categorical(list("abc"), categories=list("cba"), ordered=True)) > "b"``, which wouldn't use the order of the categories but use the lexicographical order. (:issue:`9848`)
123+
124+
- Bug in ``Series.quantile`` on empty Series of type ``Datetime`` or ``Timedelta`` (:issue:`9675`)

pandas/core/groupby.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -2808,7 +2808,12 @@ def _wrap_applied_output(self, keys, values, not_indexed_same=False):
28082808

28092809
# make Nones an empty object
28102810
if com._count_not_none(*values) != len(values):
2811-
v = next(v for v in values if v is not None)
2811+
try:
2812+
v = next(v for v in values if v is not None)
2813+
except StopIteration:
2814+
# If all values are None, then this will throw an error.
2815+
# We'd prefer it return an empty dataframe.
2816+
return DataFrame()
28122817
if v is None:
28132818
return DataFrame()
28142819
elif isinstance(v, NDFrame):

pandas/tests/test_groupby.py

+11
Original file line numberDiff line numberDiff line change
@@ -5053,6 +5053,17 @@ def test_groupby_categorical_two_columns(self):
50535053
"C3":[nan,nan,nan,nan, 10,100,nan,nan, nan,nan,200,34]}, index=idx)
50545054
tm.assert_frame_equal(res, exp)
50555055

5056+
def test_groupby_apply_all_none(self):
5057+
# Tests to make sure no errors if apply function returns all None
5058+
# values. Issue 9684.
5059+
test_df = DataFrame({'groups': [0,0,1,1], 'random_vars': [8,7,4,5]})
5060+
5061+
def test_func(x):
5062+
pass
5063+
result = test_df.groupby('groups').apply(test_func)
5064+
expected = DataFrame()
5065+
tm.assert_frame_equal(result, expected)
5066+
50565067

50575068
def assert_fp_equal(a, b):
50585069
assert (np.abs(a - b) < 1e-12).all()

0 commit comments

Comments
 (0)