Skip to content

Commit 5a15069

Browse files
alexander-ponomaroffjreback
authored andcommitted
Dropna argument is now respected when false in pivot_table (pandas-dev#25738)
1 parent b134082 commit 5a15069

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ Reshaping
394394
- Bug in :func:`merge` when merging by index name would sometimes result in an incorrectly numbered index (:issue:`24212`)
395395
- :func:`to_records` now accepts dtypes to its `column_dtypes` parameter (:issue:`24895`)
396396
- 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`)
397+
- 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`)
397398
- Bug in :func:`concat` where the resulting ``freq`` of two :class:`DatetimeIndex` with the same ``freq`` would be dropped (:issue:`3232`).
398399
- Bug in :func:`merge` where merging with equivalent Categorical dtypes was raising an error (:issue:`22501`)
399400
- Bug in :class:`DataFrame` constructor when passing non-empty tuples would cause a segmentation fault (:issue:`25691`)

pandas/core/reshape/pivot.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ def pivot_table(data, values=None, index=None, columns=None, aggfunc='mean',
3434
table = pivot_table(data, values=values, index=index,
3535
columns=columns,
3636
fill_value=fill_value, aggfunc=func,
37-
margins=margins, margins_name=margins_name)
37+
margins=margins, dropna=dropna,
38+
margins_name=margins_name)
3839
pieces.append(table)
3940
keys.append(getattr(func, '__name__', func))
4041

pandas/tests/reshape/test_pivot.py

+49
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,55 @@ def test_pivot_number_of_levels_larger_than_int32(self):
12881288
df.pivot_table(index='ind1', columns='ind2',
12891289
values='count', aggfunc='count')
12901290

1291+
def test_pivot_table_aggfunc_dropna(self, dropna):
1292+
# GH 22159
1293+
df = pd.DataFrame({'fruit': ['apple', 'peach', 'apple'],
1294+
'size': [1, 1, 2],
1295+
'taste': [7, 6, 6]})
1296+
1297+
def ret_one(x):
1298+
return 1
1299+
1300+
def ret_sum(x):
1301+
return sum(x)
1302+
1303+
def ret_none(x):
1304+
return np.nan
1305+
1306+
result = pd.pivot_table(df, columns='fruit',
1307+
aggfunc=[ret_sum, ret_none, ret_one],
1308+
dropna=dropna)
1309+
1310+
data = [[3, 1, np.nan, np.nan, 1, 1], [13, 6, np.nan, np.nan, 1, 1]]
1311+
col = pd.MultiIndex.from_product([['ret_sum', 'ret_none', 'ret_one'],
1312+
['apple', 'peach']],
1313+
names=[None, 'fruit'])
1314+
expected = pd.DataFrame(data, index=['size', 'taste'], columns=col)
1315+
1316+
if dropna:
1317+
expected = expected.dropna(axis='columns')
1318+
1319+
tm.assert_frame_equal(result, expected)
1320+
1321+
def test_pivot_table_aggfunc_scalar_dropna(self, dropna):
1322+
# GH 22159
1323+
df = pd.DataFrame({'A': ['one', 'two', 'one'],
1324+
'x': [3, np.nan, 2],
1325+
'y': [1, np.nan, np.nan]})
1326+
1327+
result = pd.pivot_table(df, columns='A',
1328+
aggfunc=np.mean,
1329+
dropna=dropna)
1330+
1331+
data = [[2.5, np.nan], [1, np.nan]]
1332+
col = pd.Index(['one', 'two'], name='A')
1333+
expected = pd.DataFrame(data, index=['x', 'y'], columns=col)
1334+
1335+
if dropna:
1336+
expected = expected.dropna(axis='columns')
1337+
1338+
tm.assert_frame_equal(result, expected)
1339+
12911340

12921341
class TestCrosstab(object):
12931342

0 commit comments

Comments
 (0)