Skip to content

Commit dab4d88

Browse files
authored
TST: groupby apply called multiple times (#34897)
* ✅ * ✅ * reformatted accordingly, for linting issues * Fixed as per the review comments
1 parent 8eb1048 commit dab4d88

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

pandas/tests/groupby/test_apply.py

+21
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,27 @@ def f_constant_df(group):
190190
assert names == group_names
191191

192192

193+
def test_group_apply_once_per_group2(capsys):
194+
# GH: 31111
195+
# groupby-apply need to execute len(set(group_by_columns)) times
196+
197+
expected = 2 # Number of times `apply` should call a function for the current test
198+
199+
df = pd.DataFrame(
200+
{
201+
"group_by_column": [0, 0, 0, 0, 1, 1, 1, 1],
202+
"test_column": ["0", "2", "4", "6", "8", "10", "12", "14"],
203+
},
204+
index=["0", "2", "4", "6", "8", "10", "12", "14"],
205+
)
206+
207+
df.groupby("group_by_column").apply(lambda df: print("function_called"))
208+
209+
result = capsys.readouterr().out.count("function_called")
210+
# If `groupby` behaves unexpectedly, this test will break
211+
assert result == expected
212+
213+
193214
def test_apply_fast_slow_identical():
194215
# GH 31613
195216

0 commit comments

Comments
 (0)