Skip to content

Commit 7e4d306

Browse files
authored
DOC: Add details of dropna in DataFrame.pivot_table (#61184)
1 parent d4045f0 commit 7e4d306

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

pandas/core/frame.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -9347,8 +9347,12 @@ def pivot(
93479347
on the rows and columns.
93489348
dropna : bool, default True
93499349
Do not include columns whose entries are all NaN. If True,
9350-
rows with a NaN value in any column will be omitted before
9351-
computing margins.
9350+
9351+
* rows with an NA value in any column will be omitted before computing
9352+
margins,
9353+
* index/column keys containing NA values will be dropped (see ``dropna``
9354+
parameter in :meth:`DataFrame.groupby`).
9355+
93529356
margins_name : str, default 'All'
93539357
Name of the row / column that will contain the totals
93549358
when margins is True.

pandas/core/reshape/pivot.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,11 @@ def pivot_table(
102102
on the rows and columns.
103103
dropna : bool, default True
104104
Do not include columns whose entries are all NaN. If True,
105-
rows with a NaN value in any column will be omitted before
106-
computing margins.
105+
106+
* rows with an NA value in any column will be omitted before computing margins,
107+
* index/column keys containing NA values will be dropped (see ``dropna``
108+
parameter in :meth:``DataFrame.groupby``).
109+
107110
margins_name : str, default 'All'
108111
Name of the row / column that will contain the totals
109112
when margins is True.

pandas/tests/reshape/test_pivot.py

+24
Original file line numberDiff line numberDiff line change
@@ -2529,6 +2529,30 @@ def test_pivot_table_aggfunc_nunique_with_different_values(self):
25292529

25302530
tm.assert_frame_equal(result, expected)
25312531

2532+
def test_pivot_table_index_and_column_keys_with_nan(self, dropna):
2533+
# GH#61113
2534+
data = {"row": [None, *range(4)], "col": [*range(4), None], "val": range(5)}
2535+
df = DataFrame(data)
2536+
result = df.pivot_table(values="val", index="row", columns="col", dropna=dropna)
2537+
e_axis = [*range(4), None]
2538+
nan = np.nan
2539+
e_data = [
2540+
[nan, 1.0, nan, nan, nan],
2541+
[nan, nan, 2.0, nan, nan],
2542+
[nan, nan, nan, 3.0, nan],
2543+
[nan, nan, nan, nan, 4.0],
2544+
[0.0, nan, nan, nan, nan],
2545+
]
2546+
expected = DataFrame(
2547+
data=e_data,
2548+
index=Index(data=e_axis, name="row"),
2549+
columns=Index(data=e_axis, name="col"),
2550+
)
2551+
if dropna:
2552+
expected = expected.loc[[0, 1, 2], [1, 2, 3]]
2553+
2554+
tm.assert_frame_equal(left=result, right=expected)
2555+
25322556

25332557
class TestPivot:
25342558
def test_pivot(self):

0 commit comments

Comments
 (0)