Skip to content

Commit 990972b

Browse files
Nick Eubankjreback
Nick Eubank
authored andcommitted
Fixes groupby.apply() error when no returns #9684
1 parent 2997e70 commit 990972b

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/source/whatsnew/v0.16.1.txt

+4
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,13 @@ Bug Fixes
185185
- Fixed bug in ``StataWriter`` resulting in changes to input ``DataFrame`` upon save (:issue:`9795`).
186186
- Bug in ``transform`` causing length mismatch when null entries were present and a fast aggregator was being used (:issue:`9697`)
187187
- Bug in ``equals`` causing false negatives when block order differed (:issue:`9330`)
188+
188189
- Bug in ``read_sql_table`` error when reading postgres table with timezone (:issue:`7139`)
189190
- Bug in ``DataFrame`` slicing may not retain metadata (:issue:`9776`)
190191
- Bug where ``TimdeltaIndex`` were not properly serialized in fixed ``HDFStore`` (:issue:`9635`)
192+
193+
- Bug in ``groupby.apply()`` that would raise if a passed user defined function either returned only ``None`` (for all input). (:issue:`9685`)
194+
191195
- Bug in plotting continuously using ``secondary_y`` may not show legend properly. (:issue:`9610`, :issue:`9779`)
192196
- Bug in ``DataFrame.plot(kind="hist")`` results in ``TypeError`` when ``DataFrame`` contains non-numeric columns (:issue:`9853`)
193197
- Bug where repeated plotting of ``DataFrame`` with a ``DatetimeIndex`` may raise ``TypeError`` (:issue:`9852`)

pandas/core/groupby.py

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

28232823
# make Nones an empty object
28242824
if com._count_not_none(*values) != len(values):
2825-
v = next(v for v in values if v is not None)
2825+
try:
2826+
v = next(v for v in values if v is not None)
2827+
except StopIteration:
2828+
# If all values are None, then this will throw an error.
2829+
# We'd prefer it return an empty dataframe.
2830+
return DataFrame()
28262831
if v is None:
28272832
return DataFrame()
28282833
elif isinstance(v, NDFrame):

pandas/tests/test_groupby.py

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

5086+
def test_groupby_apply_all_none(self):
5087+
# Tests to make sure no errors if apply function returns all None
5088+
# values. Issue 9684.
5089+
test_df = DataFrame({'groups': [0,0,1,1], 'random_vars': [8,7,4,5]})
5090+
5091+
def test_func(x):
5092+
pass
5093+
result = test_df.groupby('groups').apply(test_func)
5094+
expected = DataFrame()
5095+
tm.assert_frame_equal(result, expected)
5096+
50865097

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

0 commit comments

Comments
 (0)