-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Closed
Pivot nans fix #28540
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
79a08d7
Merge pull request #1 from pandas-dev/master
endremborza 114e2d0
keep nan cols test
endremborza b9c237a
add keep nan cols functionality
endremborza 07b3df7
black formatting
endremborza 39a6427
extend pivot nancol test
endremborza 8c5fd32
restructure keep nan cols in tests
endremborza 2bfdf2b
restructure keep nan cols implementation
endremborza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you construct the expected output and use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.