-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
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
TST: Remove groupby/test_function.py #56338
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" | ||
Tests that apply to all groupby operation methods. | ||
|
||
The only tests that should appear here are those that use the `groupby_func` fixture. | ||
Even if it does use that fixture, prefer a more specific test file if it available | ||
such as: | ||
|
||
- test_categorical | ||
- test_groupby_dropna | ||
- test_groupby_subclass | ||
- test_raises | ||
""" | ||
|
||
import pytest | ||
|
||
import pandas as pd | ||
from pandas import DataFrame | ||
import pandas._testing as tm | ||
from pandas.tests.groupby import get_groupby_method_args | ||
|
||
|
||
def test_multiindex_group_all_columns_when_empty(groupby_func): | ||
# GH 32464 | ||
df = DataFrame({"a": [], "b": [], "c": []}).set_index(["a", "b", "c"]) | ||
gb = df.groupby(["a", "b", "c"], group_keys=False) | ||
method = getattr(gb, groupby_func) | ||
args = get_groupby_method_args(groupby_func, df) | ||
|
||
warn = FutureWarning if groupby_func == "fillna" else None | ||
warn_msg = "DataFrameGroupBy.fillna is deprecated" | ||
with tm.assert_produces_warning(warn, match=warn_msg): | ||
result = method(*args).index | ||
expected = df.index | ||
tm.assert_index_equal(result, expected) | ||
|
||
|
||
def test_duplicate_columns(request, groupby_func, as_index): | ||
# GH#50806 | ||
if groupby_func == "corrwith": | ||
msg = "GH#50845 - corrwith fails when there are duplicate columns" | ||
request.applymarker(pytest.mark.xfail(reason=msg)) | ||
df = DataFrame([[1, 3, 6], [1, 4, 7], [2, 5, 8]], columns=list("abb")) | ||
args = get_groupby_method_args(groupby_func, df) | ||
gb = df.groupby("a", as_index=as_index) | ||
warn = FutureWarning if groupby_func == "fillna" else None | ||
warn_msg = "DataFrameGroupBy.fillna is deprecated" | ||
with tm.assert_produces_warning(warn, match=warn_msg): | ||
result = getattr(gb, groupby_func)(*args) | ||
|
||
expected_df = df.set_axis(["a", "b", "c"], axis=1) | ||
expected_args = get_groupby_method_args(groupby_func, expected_df) | ||
expected_gb = expected_df.groupby("a", as_index=as_index) | ||
warn = FutureWarning if groupby_func == "fillna" else None | ||
warn_msg = "DataFrameGroupBy.fillna is deprecated" | ||
with tm.assert_produces_warning(warn, match=warn_msg): | ||
expected = getattr(expected_gb, groupby_func)(*expected_args) | ||
if groupby_func not in ("size", "ngroup", "cumcount"): | ||
expected = expected.rename(columns={"c": "b"}) | ||
tm.assert_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"idx", | ||
[ | ||
pd.Index(["a", "a"], name="foo"), | ||
pd.MultiIndex.from_tuples((("a", "a"), ("a", "a")), names=["foo", "bar"]), | ||
], | ||
) | ||
def test_dup_labels_output_shape(groupby_func, idx): | ||
if groupby_func in {"size", "ngroup", "cumcount"}: | ||
pytest.skip(f"Not applicable for {groupby_func}") | ||
|
||
df = DataFrame([[1, 1]], columns=idx) | ||
grp_by = df.groupby([0]) | ||
|
||
args = get_groupby_method_args(groupby_func, df) | ||
warn = FutureWarning if groupby_func == "fillna" else None | ||
warn_msg = "DataFrameGroupBy.fillna is deprecated" | ||
with tm.assert_produces_warning(warn, match=warn_msg): | ||
result = getattr(grp_by, groupby_func)(*args) | ||
|
||
assert result.shape == (1, 2) | ||
tm.assert_index_equal(result.columns, idx) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,6 +235,36 @@ def test_idxmin_idxmax_returns_int_types(func, values, numeric_only): | |
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"data", | ||
[ | ||
( | ||
Timestamp("2011-01-15 12:50:28.502376"), | ||
Timestamp("2011-01-20 12:50:28.593448"), | ||
), | ||
(24650000000000001, 24650000000000002), | ||
], | ||
) | ||
@pytest.mark.parametrize("method", ["count", "min", "max", "first", "last"]) | ||
def test_groupby_non_arithmetic_agg_int_like_precision(method, data): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This tests has been cleaned up and not just moved. |
||
# GH#6620, GH#9311 | ||
df = DataFrame({"a": [1, 1], "b": data}) | ||
|
||
grouped = df.groupby("a") | ||
result = getattr(grouped, method)() | ||
if method == "count": | ||
expected_value = 2 | ||
elif method == "first": | ||
expected_value = data[0] | ||
elif method == "last": | ||
expected_value = data[1] | ||
else: | ||
expected_value = getattr(df["b"], method)() | ||
expected = DataFrame({"b": [expected_value]}, index=pd.Index([1], name="a")) | ||
|
||
tm.assert_frame_equal(result, expected) | ||
|
||
|
||
def test_idxmin_idxmax_axis1(): | ||
df = DataFrame( | ||
np.random.default_rng(2).standard_normal((10, 4)), columns=["A", "B", "C", "D"] | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tests has been cleaned up and not just moved.