Skip to content
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

DOC: Add details of dropna in DataFrame.pivot_table #61184

Merged
merged 20 commits into from
Apr 2, 2025
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
8 changes: 6 additions & 2 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -9347,8 +9347,12 @@ def pivot(
on the rows and columns.
dropna : bool, default True
Do not include columns whose entries are all NaN. If True,
rows with a NaN value in any column will be omitted before
computing margins.

* rows with an NA value in any column will be omitted before computing
margins,
* index/column keys containing NA values will be dropped (see ``dropna``
parameter in :meth:`DataFrame.groupby`).

margins_name : str, default 'All'
Name of the row / column that will contain the totals
when margins is True.
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ def pivot_table(
on the rows and columns.
dropna : bool, default True
Do not include columns whose entries are all NaN. If True,
rows with a NaN value in any column will be omitted before
computing margins.

* rows with an NA value in any column will be omitted before computing margins,
* index/column keys containing NA values will be dropped (see ``dropna``
parameter in :meth:``DataFrame.groupby``).

margins_name : str, default 'All'
Name of the row / column that will contain the totals
when margins is True.
Expand Down
24 changes: 24 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2529,6 +2529,30 @@ def test_pivot_table_aggfunc_nunique_with_different_values(self):

tm.assert_frame_equal(result, expected)

def test_pivot_table_index_and_column_keys_with_nan(self, dropna):
# GH#61113
data = {"row": [None, *range(4)], "col": [*range(4), None], "val": range(5)}
df = DataFrame(data)
result = df.pivot_table(values="val", index="row", columns="col", dropna=dropna)
e_axis = [*range(4), None]
nan = np.nan
e_data = [
[nan, 1.0, nan, nan, nan],
[nan, nan, 2.0, nan, nan],
[nan, nan, nan, 3.0, nan],
[nan, nan, nan, nan, 4.0],
[0.0, nan, nan, nan, nan],
]
expected = DataFrame(
data=e_data,
index=Index(data=e_axis, name="row"),
columns=Index(data=e_axis, name="col"),
)
if dropna:
expected = expected.loc[[0, 1, 2], [1, 2, 3]]

tm.assert_frame_equal(left=result, right=expected)


class TestPivot:
def test_pivot(self):
Expand Down