Skip to content

Dropna argument is now respected when false in pivot_table #25738

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
merged 11 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ Reshaping
- Bug in :func:`merge` when merging by index name would sometimes result in an incorrectly numbered index (:issue:`24212`)
- :func:`to_records` now accepts dtypes to its `column_dtypes` parameter (:issue:`24895`)
- Bug in :func:`concat` where order of ``OrderedDict`` (and ``dict`` in Python 3.6+) is not respected, when passed in as ``objs`` argument (:issue:`21510`)
- Bug in :func:`pivot_table` where columns with ``NaN`` values are dropped even if ``dropna`` argument is ``False``, when the ``aggfunc`` argument contains a ``list`` (:issue:`22159`)
- Bug in :func:`concat` where the resulting ``freq`` of two :class:`DatetimeIndex` with the same ``freq`` would be dropped (:issue:`3232`).
- Bug in :func:`merge` where merging with equivalent Categorical dtypes was raising an error (:issue:`22501`)
- Bug in :class:`DataFrame` constructor when passing non-empty tuples would cause a segmentation fault (:issue:`25691`)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
table = pivot_table(data, values=values, index=index,
columns=columns,
fill_value=fill_value, aggfunc=func,
margins=margins, margins_name=margins_name)
margins=margins, dropna=dropna,
margins_name=margins_name)
pieces.append(table)
keys.append(getattr(func, '__name__', func))

Expand Down
49 changes: 49 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,55 @@ def test_pivot_number_of_levels_larger_than_int32(self):
df.pivot_table(index='ind1', columns='ind2',
values='count', aggfunc='count')

def test_pivot_table_aggfunc_dropna(self, dropna):
# GH 22159
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we have a sufficient test when aggfunc is just a scalar? if not can you add

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jreback I'm not sure what you mean by this. How can aggfunc be a scalar? Could you please elaborate on this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexander-ponomaroff for example when aggfunc=np.sum

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WillAyd Thank you, it doesn't seem like there is a test with dropna and scalar aggfunc together. So I will add one right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a second test, where aggfunc=np.mean. Please take a look and let me know if it's sufficient.

df = pd.DataFrame({'fruit': ['apple', 'peach', 'apple'],
'size': [1, 1, 2],
'taste': [7, 6, 6]})

def ret_one(x):
return 1

def ret_sum(x):
return sum(x)

def ret_none(x):
return np.nan

result = pd.pivot_table(df, columns='fruit',
aggfunc=[ret_sum, ret_none, ret_one],
dropna=dropna)

data = [[3, 1, np.nan, np.nan, 1, 1], [13, 6, np.nan, np.nan, 1, 1]]
col = pd.MultiIndex.from_product([['ret_sum', 'ret_none', 'ret_one'],
['apple', 'peach']],
names=[None, 'fruit'])
expected = pd.DataFrame(data, index=['size', 'taste'], columns=col)

if dropna:
expected = expected.dropna(axis='columns')

tm.assert_frame_equal(result, expected)

def test_pivot_table_aggfunc_scalar_dropna(self, dropna):
# GH 22159
df = pd.DataFrame({'A': ['one', 'two', 'one'],
'x': [3, np.nan, 2],
'y': [1, np.nan, np.nan]})

result = pd.pivot_table(df, columns='A',
aggfunc=np.mean,
dropna=dropna)

data = [[2.5, np.nan], [1, np.nan]]
col = pd.Index(['one', 'two'], name='A')
expected = pd.DataFrame(data, index=['x', 'y'], columns=col)

if dropna:
expected = expected.dropna(axis='columns')

tm.assert_frame_equal(result, expected)


class TestCrosstab(object):

Expand Down