-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUG: Fix/test SparseSeries/SparseDataFrame stack/unstack (#16616)
- Loading branch information
Showing
5 changed files
with
159 additions
and
37 deletions.
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) |