Skip to content

stopped numeric_only=True from dropping columns #46830

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 3 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: 0 additions & 2 deletions pandas/core/groupby/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -1610,8 +1610,6 @@ def _cython_agg_general(
raise NotImplementedError(
f"{type(self).__name__}.{how} does not implement {kwd_name}."
)
elif not is_ser:
data = data.get_numeric_data(copy=False)

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

tm.assert_frame_equal(result, expected_result)


@pytest.mark.parametrize(
"columns, index, numeric_only",
[ # the only element changed between tests is numeric_only, but all are parameterized
pytest.param(
['a', 'b'],
['a'],
False,
),
pytest.param(
['a', 'b'],
['a'],
None,
),
pytest.param(
['a', 'b'],
['a'],
True,
),
],
)
def test_empty_frame_groupby_numeric_only(columns, index, numeric_only):
# GH 46375 - certain values of numeric_only are causing the df to drop columns
df = pd.DataFrame(columns=columns)
expected = pd.DataFrame(columns=columns).set_index(index)
result = df.groupby(index).first(numeric_only=numeric_only)
tm.assert_frame_equal(result, expected)


@pytest.mark.parametrize(
"data, index, numeric_only, expected_data",
[ # the only element changed between tests is numeric_only, but all are parameterized
pytest.param(
{ "a": [0, 0, 1, 1], "b": [1, "x", 2, "y"], "c": [1, 1, 2, 2],},
['a'],
False,
{"a": [0, 1], "b": [1, 2], "c": [1, 2],}
),
pytest.param(
{ "a": [0, 0, 1, 1], "b": [1, "x", 2, "y"], "c": [1, 1, 2, 2],},
['a'],
None,
{"a": [0, 1], "b": [1, 2], "c": [1, 2],}
),
pytest.param(
{ "a": [0, 0, 1, 1], "b": [1, "x", 2, "y"], "c": [1, 1, 2, 2],},
['a'],
True,
{"a": [0, 1], "b": [1, 2], "c": [1, 2],}
),
],
)
def test_heterogeneous_frame_groupby_numeric_only(data, index, numeric_only, expected_data):
# GH 46375 - certain values of numeric_only are causing the df to drop columns
df = pd.DataFrame(data)
result = df.groupby(index).first(numeric_only=numeric_only)
expected = pd.DataFrame(expected_data).set_index(index)
tm.assert_frame_equal(result, expected)