Skip to content

Commit c0d8959

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

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+
@pytest.mark.parametrize("is_float", [True, False])
1183+
def test_apply_index_key_error_bug(is_float):
1184+
# GH 44310
1185+
if is_float:
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+
else:
1194+
result = DataFrame(
1195+
{
1196+
"a": ["aa", "a2", "a3"],
1197+
"b": [1, 2, 3],
1198+
},
1199+
index=Index([1, 2, 3]),
1200+
)
1201+
expected = DataFrame(
1202+
{
1203+
"b_mean": [2.0, 3.0, 1.0],
1204+
},
1205+
index=Index(["a2", "a3", "aa"], name="a"),
1206+
)
1207+
result = result.groupby("a").apply(
1208+
lambda df: Series([df["b"].mean()], index=["b_mean"])
1209+
)
1210+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)