-
-
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
BUG: pivot_table sometimes returns Series (#4386) #14534
Conversation
Current coverage is 85.28% (diff: 100%)@@ master #14534 diff @@
==========================================
Files 140 140
Lines 50693 50695 +2
Methods 0 0
Messages 0 0
Branches 0 0
==========================================
+ Hits 43235 43236 +1
- Misses 7458 7459 +1
Partials 0 0
|
@@ -170,6 +170,9 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean', | |||
if len(index) == 0 and len(columns) > 0: | |||
table = table.T | |||
|
|||
if isinstance(table, Series): |
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.
can you see if you can find the actual root cause here?
e.g. may be as simple as making sure that the column/index seletors are list-like and not scalars
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.
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.
actually I think a you can simply always make aggfunc
a list (if its not None nor a list-like) and this will just work. use is_list_like
to test
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.
Your solution works for the specific scenario in #4386, but a Series
is still returned regardless when pivoting with only the columns argument or pivoting with no values or cols passed due to the groupby
and unstack
operations respectively. In these cases, I think to_frame()
might be necessary if pivot_table
should always return a DataFrame
.
Or should I just fix the specific scenario in #4386 and change the docs to mention that pivot_table
can return a Series
?
@@ -350,7 +350,7 @@ def _check_output(result, values_col, index=['A', 'B'], | |||
# no rows | |||
rtable = self.data.pivot_table(columns=['AA', 'BB'], margins=True, | |||
aggfunc=np.mean) | |||
tm.assertIsInstance(rtable, Series) | |||
tm.assertIsInstance(rtable, DataFrame) |
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.
can you compare this with an expected result.
@@ -485,7 +485,7 @@ def test_margins_no_values_no_cols(self): | |||
# Regression test on pivot table: no values or cols passed. | |||
result = self.data[['A', 'B']].pivot_table( | |||
index=['A', 'B'], aggfunc=len, margins=True) | |||
result_list = result.tolist() | |||
result_list = result.values.flatten().tolist() | |||
self.assertEqual(sum(result_list[:-1]), result_list[-1]) |
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.
same here (I know this is old / not yours) .....
Hi @jreback: I added a fix to address the original bug. However, I believe |
BUG: pivot_table someitmes returns Series (pandas-dev#4386) BUG: pivot_table sometimes returns Series (pandas-dev#4386) BUG: pivot_table sometimes returns Series (pandas-dev#4386) pep 8 fixes Restructure condional and update whatsnew
this looks like its duplicating #13554. Can you see if you tests are any different (maybe there is another case). |
can you rebase |
git diff upstream/master | flake8 --diff
The
pivot_table
docs mentions that aDataFrame
should be returned. In some cases, aSeries
is returned. I had to also alter some existing tests that expectedpivot_table
to return aSeries
Any feedback welcomed, thanks!