Skip to content

Pivot nans fix #28540

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

Closed
wants to merge 7 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5980,7 +5980,7 @@ def pivot_table(
margins=False,
dropna=True,
margins_name="All",
observed=False,
observed=True,
):
from pandas.core.reshape.pivot import pivot_table

Expand Down
13 changes: 5 additions & 8 deletions pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def pivot_table(
margins=False,
dropna=True,
margins_name="All",
observed=False,
observed=True,
):
index = _convert_by(index)
columns = _convert_by(columns)
Expand Down Expand Up @@ -92,11 +92,12 @@ def pivot_table(
pass
values = list(values)

grouped = data.groupby(keys, observed=observed)
if dropna:
grouped = data.groupby(keys, observed=observed)
else:
grouped = data.groupby(keys, observed=False)
agged = grouped.agg(aggfunc)
if dropna and isinstance(agged, ABCDataFrame) and len(agged.columns):
agged = agged.dropna(how="all")

# gh-21133
# we want to down cast if
# the original values are ints
Expand Down Expand Up @@ -172,10 +173,6 @@ def pivot_table(
if len(index) == 0 and len(columns) > 0:
table = table.T

# GH 15193 Make sure empty columns are removed if dropna=True
if isinstance(table, ABCDataFrame) and dropna:
table = table.dropna(how="all", axis=1)

return table


Expand Down
53 changes: 53 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,59 @@ def test_pivot_table_dropna(self):
tm.assert_index_equal(pv_col.columns, m)
tm.assert_index_equal(pv_ind.index, m)

def test_pivot_table_keep_nancols(self): # GH18030
df = pd.DataFrame(
{
"metric_value": [10, 11, 0, 3, np.nan, np.nan, 100, 20],
"metric_name": ["m", "n", "m", "x", "n", "x", "m", "n"],
"product": ["A", "A", "B", "B", "C", "C", "D", "D"],
"measurer": ["Tom", "Tom", "Bill", "Tom", "Bill", "Tom", "Bill", "Tom"],
}
)
pv_col = df.pivot_table(
"metric_value",
"metric_name",
["measurer", "product"],
dropna=True,
)
pv_ind = df.pivot_table(
"metric_value",
["measurer", "product"],
"metric_name",
dropna=True,
)

m = MultiIndex.from_tuples(
[
("Bill", "B"),
("Bill", "C"),
("Bill", "D"),
("Tom", "A"),
("Tom", "B"),
("Tom", "C"),
("Tom", "D"),
],
names=["measurer", "product"],
)
tm.assert_index_equal(pv_col.columns, m)
Copy link
Member

Choose a reason for hiding this comment

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

Can you construct the expected output and use tm.assert_frame_equal(result, expected instead? You should see that in a lot of other tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

did one way

tm.assert_index_equal(pv_ind.index, m)

expected_pv_col = pd.DataFrame(
{
("Bill", "B"): {"m": 0.0, "n": np.nan, "x": np.nan},
("Bill", "C"): {"m": np.nan, "n": np.nan, "x": np.nan},
("Bill", "D"): {"m": 100.0, "n": np.nan, "x": np.nan},
("Tom", "A"): {"m": 10.0, "n": 11.0, "x": np.nan},
("Tom", "B"): {"m": np.nan, "n": np.nan, "x": 3.0},
("Tom", "C"): {"m": np.nan, "n": np.nan, "x": np.nan},
("Tom", "D"): {"m": np.nan, "n": 20.0, "x": np.nan},
}
)
expected_pv_col.index.name = "metric_name"
expected_pv_col.columns.names = ["measurer", "product"]

tm.assert_frame_equal(pv_col, expected_pv_col)

def test_pivot_table_categorical(self):

cat1 = Categorical(
Expand Down