8000 Pivot nans fix by endremborza · Pull Request #28540 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
keep nan cols test
  • Loading branch information
endremborza committed Sep 19, 2019
commit 114e2d02b4c8e8d8c1e5d34a0dad49726b767dce
33 changes: 33 additions & 0 deletions pandas/tests/reshape/test_pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,39 @@ 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):
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=False,
keep_only_observed_nancols=True
)
pv_ind = df.pivot_table(
"metric_value", ["measurer", "product"], "metric_name", dropna=False,
keep_only_observed_nancols=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)

def test_pivot_table_categorical(self):

cat1 = Categorical(
Expand Down
0