Skip to content

Data frame groupby sum drop #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1668,7 +1668,7 @@ def _cython_agg_general(
raise NotImplementedError(
f"{type(self).__name__}.{how} does not implement {kwd_name}."
)
elif not is_ser:
elif not is_ser and data.shape[1] != 0:
data = data.get_numeric_data(copy=False)

def array_func(values: ArrayLike) -> ArrayLike:
Expand Down
48 changes: 48 additions & 0 deletions pandas/tests/groupby/test_groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -2776,3 +2776,51 @@ def test_by_column_values_with_same_starting_value():
).set_index("Name")

tm.assert_frame_equal(result, expected_result)


@pytest.mark.parametrize(
"numeric_only",
[False, None, True],
)
def test_empty_frame_groupby_numeric_only(numeric_only):
# GH 46375
df = DataFrame(columns=["a", "b"])
expected = DataFrame(columns=["a", "b"]).set_index(["a"])
result = df.groupby(["a"]).first(numeric_only=numeric_only)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"numeric_only, expected_data",
[
(
False,
{
"a": [0, 1],
"b": [1, 2],
"c": [1, 2],
},
),
(
None,
{
"a": [0, 1],
"b": [1, 2],
"c": [1, 2],
},
),
(
True,
{
"a": [0, 1],
"c": [1, 2],
},
),
],
)
def test_frame_groupby_numeric_only(numeric_only, expected_data):
# GH 46375
df = DataFrame({"a": [0, 0, 1, 1], "b": [1, "x", 2, "y"], "c": [1, 1, 2, 2]})
result = df.groupby(["a"]).first(numeric_only=numeric_only)
expected = DataFrame(expected_data).set_index(["a"])
tm.assert_frame_equal(result, expected)