Skip to content

Commit 7f95c00

Browse files
committed
Test for Group By - Apply Key Error
1 parent e6091f5 commit 7f95c00

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

pandas/tests/groupby/test_apply.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,3 +1177,34 @@ def test_apply_empty_string_nan_coerce_bug():
11771177
index=MultiIndex.from_tuples([(1, ""), (2, "")], names=["a", "b"]),
11781178
)
11791179
tm.assert_frame_equal(result, expected)
1180+
1181+
1182+
def test_apply_index_key_error_bug():
1183+
# GH 44310
1184+
# Tests for a key error while applying in a grouped DF
1185+
1186+
result = DataFrame(
1187+
{
1188+
"a": ["aa", "a2", "a3"],
1189+
"b": [1, 2, 3],
1190+
},
1191+
index=Index([1.0, 2.0, 3.0]),
1192+
)
1193+
1194+
expected = DataFrame(
1195+
{
1196+
"a": ["aa", "a2", "a3"],
1197+
"b": [1, 2, 3],
1198+
},
1199+
index=Index([1, 2, 3]),
1200+
)
1201+
try:
1202+
result = result.groupby("a").apply(
1203+
lambda df: Series([df["b"].mean()], index=["b_mean"])
1204+
)
1205+
expected = expected.groupby("a").apply(
1206+
lambda df: Series([df["b"].mean()], index=["b_mean"])
1207+
)
1208+
tm.assert_frame_equal(result, expected)
1209+
except KeyError:
1210+
assert False

0 commit comments

Comments
 (0)