-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
ENH: Implement Keyword Aggregation for DataFrame.agg and Series.agg #29116
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
Merged
jreback
merged 148 commits into
pandas-dev:master
from
charlesdong1991:nested_renaming_agg
Jul 10, 2020
Merged
Changes from 144 commits
Commits
Show all changes
148 commits
Select commit
Hold shift + click to select a range
7e461a1
remove \n from docstring
charlesdong1991 1314059
fix conflicts
charlesdong1991 8bcb313
Merge remote-tracking branch 'upstream/master'
charlesdong1991 7bc368d
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 cf5c6c3
Implement agg for DataFrame
charlesdong1991 5298331
fix conflict
charlesdong1991 4fb74b5
remove unused import
charlesdong1991 97209be
remove print
charlesdong1991 ca273ff
fix test
charlesdong1991 1d2ab15
fix typo
charlesdong1991 3ca193c
add keyword agg for series
charlesdong1991 c8f80ed
fix linting
charlesdong1991 8c738e9
fix PY35 issue
charlesdong1991 d4d9ea4
try to fix py35 order issue
charlesdong1991 2a6de27
test if fixed
charlesdong1991 21e09f9
test again
charlesdong1991 058a8e9
simpler code
charlesdong1991 0da68d8
test py35
charlesdong1991 15e3659
fix conflict
charlesdong1991 438398d
test PY35
charlesdong1991 d47b790
try to fix py35
charlesdong1991 832b8d9
find py35 output
charlesdong1991 5a3b690
test py35
charlesdong1991 4fb86f0
retest py35
charlesdong1991 a1369bf
retest py35
charlesdong1991 ef981a3
try to fix py35
charlesdong1991 82c8960
try to fix py35
charlesdong1991 c610391
try one more time
charlesdong1991 679ba59
fix typo
charlesdong1991 2ee2628
py35
charlesdong1991 31f7033
skip PY35
charlesdong1991 2acb244
skip py35
charlesdong1991 dfbd67a
fix typo
charlesdong1991 ff5e60f
skip all py35
charlesdong1991 7c6c891
skip py35 for series
charlesdong1991 3e55fcb
fix test
charlesdong1991 6d74b29
skip series py35
charlesdong1991 532337e
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 400ff3e
merge master and remove helper
charlesdong1991 05af2de
remove helper
charlesdong1991 6206fa4
remove py36
charlesdong1991 34199ad
put back imports
charlesdong1991 15d099c
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 c56f05f
avoid circular dependency
charlesdong1991 d3f0620
fix linting
charlesdong1991 20ecfda
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 89b8e6b
code change based on review
charlesdong1991 8aa1cc9
remove util
charlesdong1991 091ca75
Add docstring
charlesdong1991 c2d5104
fix circular import
charlesdong1991 50ebdaf
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 0484f5e
reorg and deduplicate
charlesdong1991 425c802
remove used imports
charlesdong1991 d5c2c6c
fix linting
charlesdong1991 8bb9714
fix wrong import
charlesdong1991 2607c5d
fix conflict
charlesdong1991 0545231
isort
charlesdong1991 0a27889
fix mypy
charlesdong1991 a66053e
Code change based on review
charlesdong1991 7311ef0
dropna
charlesdong1991 da2ff37
fix logic
charlesdong1991 bcc5bc3
fix logic
charlesdong1991 0825027
remove unused
charlesdong1991 d3c35f5
fix linting
charlesdong1991 cef2b50
simpler python
charlesdong1991 b96a942
fix conflicts
charlesdong1991 3123284
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 7bb3bd0
fix conflicts
charlesdong1991 3da2e2a
fix merge error
charlesdong1991 3ce91fc
fixup
charlesdong1991 1426ee2
fix annotation
charlesdong1991 5893a0e
fix annotation
charlesdong1991 cc85db4
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 0f55073
move code
charlesdong1991 90d52ba
move it back
charlesdong1991 381a697
fixup
charlesdong1991 238b4cc
add docstring
charlesdong1991 f8e1891
add func
charlesdong1991 66e9b38
isort
charlesdong1991 f4d8a4f
fix linting
charlesdong1991 c3e34a0
fix linting
charlesdong1991 0c0dbad
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 61f6201
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 88c7751
code change on JR reviews
charlesdong1991 e2b957a
move
charlesdong1991 99f75b2
linting
charlesdong1991 30b7296
isort
charlesdong1991 baea583
code change
charlesdong1991 04bffe6
add docstring
charlesdong1991 42091c3
add None back
charlesdong1991 fc13e19
fix annotation
charlesdong1991 1403426
better annotation
charlesdong1991 3d9655e
fix annotation
charlesdong1991 d78c57c
fix annotation
charlesdong1991 0487928
fix linting
charlesdong1991 7435ac5
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 f1cd16c
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 469691c
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 1bb35b5
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 7a6f496
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 3730f7d
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 cd8d00f
simpler python
charlesdong1991 6dddd55
simpler python
charlesdong1991 96dc3ed
fixup
charlesdong1991 075b85b
simplification
charlesdong1991 a44471c
better docs
charlesdong1991 0e2eae4
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 2fb4b27
add docs
charlesdong1991 5e04185
focs
charlesdong1991 56d0f89
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 7f4839e
fix doc
charlesdong1991 65d578b
fixup
charlesdong1991 3e6a06c
fix up
charlesdong1991 8651447
fix doctest
charlesdong1991 a7439fe
doctest
charlesdong1991 449d40f
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 9fd8ec5
rebuild
charlesdong1991 736bea2
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 d20be20
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 35b2b17
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 74d6169
fixup and resolve conflict and merge master
charlesdong1991 0546224
cleaner code
charlesdong1991 f5f0e68
rename
charlesdong1991 54ff962
linting
charlesdong1991 ac57023
init
charlesdong1991 484e42c
better doc
charlesdong1991 47e6598
complex case
charlesdong1991 f28b452
linting
charlesdong1991 81b4186
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 1fd4b5b
resolve conflict and merge master
charlesdong1991 47dc5fe
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 9190f7f
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 89de59e
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 8493383
add typing
charlesdong1991 9a9dd7f
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 c75c882
black
charlesdong1991 fa61db7
remove line
charlesdong1991 00a1ccf
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 26b380a
simplify annotation
charlesdong1991 165ea83
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 d6923f2
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 f6a5cc1
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 a747ab6
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 44405e8
deprivate relabel_result
charlesdong1991 faea906
cleaner annotations
charlesdong1991 7e30a61
Merge remote-tracking branch 'upstream/master' into nested_renaming_agg
charlesdong1991 3d20524
fix import sorting
charlesdong1991 05921af
move defined annotation inside aggregation.py
charlesdong1991 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
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
Empty file.
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 |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import pandas as pd | ||
import pandas._testing as tm | ||
|
||
|
||
class TestDataFrameNamedAggregate: | ||
def test_agg_relabel(self): | ||
# GH 26513 | ||
df = pd.DataFrame({"A": [1, 2, 1, 2], "B": [1, 2, 3, 4], "C": [3, 4, 5, 6]}) | ||
|
||
# simplest case with one column, one func | ||
result = df.agg(foo=("B", "sum")) | ||
expected = pd.DataFrame({"B": [10]}, index=pd.Index(["foo"])) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
# test on same column with different methods | ||
result = df.agg(foo=("B", "sum"), bar=("B", "min")) | ||
expected = pd.DataFrame({"B": [10, 1]}, index=pd.Index(["foo", "bar"])) | ||
|
||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_agg_relabel_multi_columns_multi_methods(self): | ||
# GH 26513, test on multiple columns with multiple methods | ||
df = pd.DataFrame({"A": [1, 2, 1, 2], "B": [1, 2, 3, 4], "C": [3, 4, 5, 6]}) | ||
result = df.agg( | ||
foo=("A", "sum"), | ||
bar=("B", "mean"), | ||
cat=("A", "min"), | ||
dat=("B", "max"), | ||
f=("A", "max"), | ||
g=("C", "min"), | ||
) | ||
expected = pd.DataFrame( | ||
{ | ||
"A": [6.0, np.nan, 1.0, np.nan, 2.0, np.nan], | ||
"B": [np.nan, 2.5, np.nan, 4.0, np.nan, np.nan], | ||
"C": [np.nan, np.nan, np.nan, np.nan, np.nan, 3.0], | ||
}, | ||
index=pd.Index(["foo", "bar", "cat", "dat", "f", "g"]), | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_agg_relabel_partial_functions(self): | ||
# GH 26513, test on partial, functools or more complex cases | ||
df = pd.DataFrame({"A": [1, 2, 1, 2], "B": [1, 2, 3, 4], "C": [3, 4, 5, 6]}) | ||
result = df.agg(foo=("A", np.mean), bar=("A", "mean"), cat=("A", min)) | ||
expected = pd.DataFrame( | ||
{"A": [1.5, 1.5, 1.0]}, index=pd.Index(["foo", "bar", "cat"]) | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = df.agg( | ||
foo=("A", min), | ||
bar=("A", np.min), | ||
cat=("B", max), | ||
dat=("C", "min"), | ||
f=("B", np.sum), | ||
kk=("B", lambda x: min(x)), | ||
) | ||
expected = pd.DataFrame( | ||
{ | ||
"A": [1.0, 1.0, np.nan, np.nan, np.nan, np.nan], | ||
"B": [np.nan, np.nan, 4.0, np.nan, 10.0, 1.0], | ||
"C": [np.nan, np.nan, np.nan, 3.0, np.nan, np.nan], | ||
}, | ||
index=pd.Index(["foo", "bar", "cat", "dat", "f", "kk"]), | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_agg_namedtuple(self): | ||
# GH 26513 | ||
df = pd.DataFrame({"A": [0, 1], "B": [1, 2]}) | ||
result = df.agg( | ||
foo=pd.NamedAgg("B", "sum"), | ||
bar=pd.NamedAgg("B", min), | ||
cat=pd.NamedAgg(column="B", aggfunc="count"), | ||
fft=pd.NamedAgg("B", aggfunc="max"), | ||
) | ||
|
||
expected = pd.DataFrame( | ||
{"B": [3, 1, 2, 2]}, index=pd.Index(["foo", "bar", "cat", "fft"]) | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = df.agg( | ||
foo=pd.NamedAgg("A", "min"), | ||
bar=pd.NamedAgg(column="B", aggfunc="max"), | ||
cat=pd.NamedAgg(column="A", aggfunc="max"), | ||
) | ||
expected = pd.DataFrame( | ||
{"A": [0.0, np.nan, 1.0], "B": [np.nan, 2.0, np.nan]}, | ||
index=pd.Index(["foo", "bar", "cat"]), | ||
) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
def test_agg_raises(self): | ||
# GH 26513 | ||
df = pd.DataFrame({"A": [0, 1], "B": [1, 2]}) | ||
msg = "Must provide" | ||
|
||
with pytest.raises(TypeError, match=msg): | ||
df.agg() |
File renamed without changes.
Empty file.
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.
crazy signatures, can you create an alias to make this easier to read, maybe some of this is Label?