-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: pivot_table strings as aggfunc #18810
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
Changes from 6 commits
70a45d7
5b68ae1
c4d4731
54b80af
00f2b44
0decc05
54644f3
e24d232
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1109,6 +1109,51 @@ def test_pivot_margins_name_unicode(self): | |
expected = pd.DataFrame(index=index) | ||
tm.assert_frame_equal(table, expected) | ||
|
||
def test_pivot_string_as_func(self): | ||
# GH #18713 | ||
# for correctness purposes | ||
data = DataFrame({'A': ['foo', 'foo', 'foo', 'foo', 'bar', 'bar', | ||
'bar', 'bar', 'foo', 'foo', 'foo'], | ||
'B': ['one', 'one', 'one', 'two', 'one', 'one', | ||
'one', 'two', 'two', 'two', 'one'], | ||
'C': range(11)}) | ||
|
||
result = pivot_table(data, index='A', columns='B', aggfunc='sum') | ||
mi = MultiIndex(levels=[['C'], ['one', 'two']], | ||
labels=[[0, 0], [0, 1]], names=[None, 'B']) | ||
expected = DataFrame({('C', 'one'): {'bar': 15, 'foo': 13}, | ||
('C', 'two'): {'bar': 7, 'foo': 20}}, | ||
columns=mi).rename_axis('A') | ||
tm.assert_frame_equal(result, expected) | ||
|
||
result = pivot_table(data, index='A', columns='B', | ||
aggfunc=['sum', 'mean']) | ||
mi = MultiIndex(levels=[['sum', 'mean'], ['C'], ['one', 'two']], | ||
labels=[[0, 0, 1, 1], [0, 0, 0, 0], [0, 1, 0, 1]], | ||
names=[None, None, 'B']) | ||
expected = DataFrame({('mean', 'C', 'one'): {'bar': 5.0, 'foo': 3.25}, | ||
('mean', 'C', 'two'): {'bar': 7.0, | ||
'foo': 6.666666666666667}, | ||
('sum', 'C', 'one'): {'bar': 15, 'foo': 13}, | ||
('sum', 'C', 'two'): {'bar': 7, 'foo': 20}}, | ||
columns=mi).rename_axis('A') | ||
tm.assert_frame_equal(result, expected) | ||
|
||
@pytest.mark.parametrize('f, f_numpy', | ||
[('sum', np.sum), | ||
('mean', np.mean), | ||
('std', np.std), | ||
(['sum', 'mean'], [np.sum, np.mean]), | ||
(['sum', 'std'], [np.sum, np.std]), | ||
(['std', 'mean'], [np.std, np.mean])]) | ||
def test_pivot_string_func_vs_func(self, f, f_numpy): | ||
# GH #18713 | ||
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. Add that this test is for consistency purposes. |
||
# for consistency purposes | ||
result = pivot_table(self.data, index='A', columns='B', aggfunc=f) | ||
expected = pivot_table(self.data, index='A', columns='B', | ||
aggfunc=f_numpy) | ||
tm.assert_frame_equal(result, expected) | ||
|
||
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. I like what you're testing here, but I would modify a couple of things:
...
result = f('sum')
func_result = f(np.sum)
expected = DataFrame(...)
tm.assert_frame_equal(result, func_result) # consistency
tm.assert_frame_equal(result, expected) # correctness 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. you can parametrize this test by passing these functions in as well something like
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. Thank you, both, I'll incorporate these changes |
||
|
||
class TestCrosstab(object): | ||
|
||
|
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.
Add that this test is for correctness.