-
-
Notifications
You must be signed in to change notification settings - Fork 18.7k
[BUG] Fixed behavior of DataFrameGroupBy.apply to respect _group_selection_context #29131
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
christopherzimmerman
wants to merge
19
commits into
pandas-dev:master
from
christopherzimmerman:apply_context
Closed
Changes from 5 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
78de38c
Modifed tests and fixed bug in groupby/apply
christopherzimmerman 63677e7
fixed resample docstring
christopherzimmerman 1d99d9c
Added fixture and cleaned up tests
christopherzimmerman 98bc673
Whatsnew and added match to test
christopherzimmerman fbf3202
conftest docstring and whatsnew example
christopherzimmerman 947a5bd
Changes to tests and whatsnew
christopherzimmerman 8c3efb0
Merge branch 'master' into apply_context
christopherzimmerman fa21e29
Had to parameterize the test because of group keys
christopherzimmerman a0a9aa5
Merge branch 'apply_context' of https://github.com/christopherzimmerm…
christopherzimmerman 7070169
Merge branch 'master' into apply_context
christopherzimmerman 76815f1
Update test_transform.py
christopherzimmerman 6c49a16
Update test_groupby.py
christopherzimmerman 8a4c1f8
Updated syntax in ipython block
christopherzimmerman ccf940d
Merge branch 'apply_context' of https://github.com/christopherzimmerm…
christopherzimmerman b7d056d
Merged from master
christopherzimmerman cfacfc1
Indent
christopherzimmerman c384c09
Merge remote-tracking branch 'upstream/master' into apply_context
christopherzimmerman 91d1931
Merged upstream
christopherzimmerman 83be029
More tests changed with bad apply behavior
christopherzimmerman 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 hidden or 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 |
---|---|---|
|
@@ -174,6 +174,71 @@ Backwards incompatible API changes | |
|
||
pd.arrays.IntervalArray.from_tuples([(0, 1), (2, 3)]) | ||
|
||
.. _whatsnew_1000.api_breaking.GroupBy.apply: | ||
|
||
``GroupBy.apply`` behaves consistently with `as_index` | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
- The result of :meth:`GroupBy.apply` sometimes contained the grouper column(s), | ||
WillAyd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
in both the index, and in the `DataFrame`. From Pandas 1.0, :meth:`GroupBy.apply` | ||
christopherzimmerman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
will respect the `as_index` parameter, and only return the grouper column(s) in | ||
christopherzimmerman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
the result if `as_index` is set to `False`. | ||
|
||
*pandas 0.25.x* | ||
christopherzimmerman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: ipython | ||
|
||
In [1]: df = pd.DataFrame({"a": [1, 1, 2, 2, 3, 3], "b": [1, 2, 3, 4, 5, 6]}) | ||
|
||
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. show df here |
||
In [2]: df.groupby("a").apply(lambda x: x.sum()) | ||
Out[2]: | ||
a b | ||
a | ||
1 2 3 | ||
2 4 7 | ||
3 6 11 | ||
|
||
*pandas 1.0.0* | ||
|
||
.. code-block:: ipython | ||
|
||
In [1]: df = pd.DataFrame({"a": [1, 1, 2, 2, 3, 3], "b": [1, 2, 3, 4, 5, 6]}) | ||
|
||
In [2]: df.groupby("a").apply(lambda x: x.sum()) | ||
Out[2]: | ||
b | ||
a | ||
1 3 | ||
2 7 | ||
3 11 | ||
|
||
*pandas 0.25.x* | ||
christopherzimmerman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: ipython | ||
|
||
In [1]: df = pd.DataFrame({"a": [1, 1, 2, 2, 3, 3], "b": [1, 2, 3, 4, 5, 6]}) | ||
|
||
In [2]: df.groupby("a").apply(lambda x: x.iloc[0]) | ||
Out[2]: | ||
a b | ||
a | ||
1 1 1 | ||
2 2 3 | ||
3 3 5 | ||
|
||
*pandas 1.0.0* | ||
christopherzimmerman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. code-block:: ipython | ||
|
||
In [1]: df = pd.DataFrame({"a": [1, 1, 2, 2, 3, 3], "b": [1, 2, 3, 4, 5, 6]}) | ||
christopherzimmerman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In [2]: df.groupby("a").apply(lambda x: x.iloc[0]) | ||
Out[2]: | ||
b | ||
a | ||
1 1 | ||
2 3 | ||
3 5 | ||
|
||
.. _whatsnew_1000.api.other: | ||
|
||
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -95,9 +95,16 @@ def f(x): | |
return x.drop_duplicates("person_name").iloc[0] | ||
|
||
result = g.apply(f) | ||
expected = x.iloc[[0, 1]].copy() | ||
|
||
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. so if the tests change a lot like this, make a new test |
||
# GH 28549 | ||
# grouper key should not be present after apply | ||
# with as_index=True. | ||
# TODO split this into multiple tests | ||
dropped = x.drop("person_id", 1) | ||
|
||
expected = dropped.iloc[[0, 1]].copy() | ||
expected.index = Index([1, 2], name="person_id") | ||
expected["person_name"] = expected["person_name"].astype("object") | ||
expected["person_name"] = expected["person_name"] | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# GH 9921 | ||
|
@@ -1218,7 +1225,10 @@ def test_get_nonexistent_category(): | |
# Accessing a Category that is not in the dataframe | ||
df = pd.DataFrame({"var": ["a", "a", "b", "b"], "val": range(4)}) | ||
with pytest.raises(KeyError, match="'vau'"): | ||
df.groupby("var").apply( | ||
# GH2849 This needs to use as_index=False so that | ||
christopherzimmerman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# var is still present when grouping or else another key error | ||
# will raise about var. | ||
df.groupby("var", as_index=False).apply( | ||
lambda rows: pd.DataFrame( | ||
{"var": [rows.iloc[-1]["var"]], "val": [rows.iloc[-1]["vau"]]} | ||
) | ||
|
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.
need to move to 1.1