-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
BUG: Fix/test SparseSeries/SparseDataFrame stack/unstack #16616
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d4618d3
BUG: Fix/test SparseSeries/SparseDataFrame stack/unstack
kernc 9ae1c10
fix test_reshape.TestDataFrameReshape.test_unstack_preserve_dtypes
kernc f621ea6
Move .unstack() logic onto BlockManager and Block
kernc 369e315
replace tests with pytest style
kernc a27c047
TST: move sparse stack/unstacking tests to sparse/test_reshape
kernc 0acb6f3
Move more code from BlockManager.unstack to Block._unstack
kernc 8aad74c
Extend docstrings
kernc cef5f47
Mark _maybe_to_categorical() as internal
kernc 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 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 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 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 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 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import pytest | ||
import numpy as np | ||
|
||
import pandas as pd | ||
import pandas.util.testing as tm | ||
|
||
|
||
@pytest.fixture | ||
def sparse_df(): | ||
return pd.SparseDataFrame({0: {0: 1}, 1: {1: 1}, 2: {2: 1}}) # eye | ||
|
||
|
||
@pytest.fixture | ||
def multi_index3(): | ||
return pd.MultiIndex.from_tuples([(0, 0), (1, 1), (2, 2)]) | ||
|
||
|
||
def test_sparse_frame_stack(sparse_df, multi_index3): | ||
ss = sparse_df.stack() | ||
expected = pd.SparseSeries(np.ones(3), index=multi_index3) | ||
tm.assert_sp_series_equal(ss, expected) | ||
|
||
|
||
def test_sparse_frame_unstack(sparse_df): | ||
mi = pd.MultiIndex.from_tuples([(0, 0), (1, 0), (1, 2)]) | ||
sparse_df.index = mi | ||
arr = np.array([[1, np.nan, np.nan], | ||
[np.nan, 1, np.nan], | ||
[np.nan, np.nan, 1]]) | ||
unstacked_df = pd.DataFrame(arr, index=mi).unstack() | ||
unstacked_sdf = sparse_df.unstack() | ||
|
||
tm.assert_numpy_array_equal(unstacked_df.values, unstacked_sdf.values) | ||
|
||
|
||
def test_sparse_series_unstack(sparse_df, multi_index3): | ||
frame = pd.SparseSeries(np.ones(3), index=multi_index3).unstack() | ||
tm.assert_sp_frame_equal(frame, sparse_df) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a TODO? or a comment?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment with a mild TODO hint at whomever will be refactoring the whole thing eventually to take the note into consideration.