Skip to content

Commit a27c047

Browse files
committed
TST: move sparse stack/unstacking tests to sparse/test_reshape
1 parent 369e315 commit a27c047

File tree

2 files changed

+38
-33
lines changed

2 files changed

+38
-33
lines changed

pandas/tests/sparse/test_reshape.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
import numpy as np
3+
4+
import pandas as pd
5+
import pandas.util.testing as tm
6+
7+
8+
@pytest.fixture
9+
def sparse_df():
10+
return pd.SparseDataFrame({0: {0: 1}, 1: {1: 1}, 2: {2: 1}}) # eye
11+
12+
13+
@pytest.fixture
14+
def multi_index3():
15+
return pd.MultiIndex.from_tuples([(0, 0), (1, 1), (2, 2)])
16+
17+
18+
def test_sparse_frame_stack(sparse_df, multi_index3):
19+
ss = sparse_df.stack()
20+
expected = pd.SparseSeries(np.ones(3), index=multi_index3)
21+
tm.assert_sp_series_equal(ss, expected)
22+
23+
24+
def test_sparse_frame_unstack(sparse_df):
25+
mi = pd.MultiIndex.from_tuples([(0, 0), (1, 0), (1, 2)])
26+
sparse_df.index = mi
27+
arr = np.array([[1, np.nan, np.nan],
28+
[np.nan, 1, np.nan],
29+
[np.nan, np.nan, 1]])
30+
unstacked_df = pd.DataFrame(arr, index=mi).unstack()
31+
unstacked_sdf = sparse_df.unstack()
32+
33+
tm.assert_numpy_array_equal(unstacked_df.values, unstacked_sdf.values)
34+
35+
36+
def test_sparse_series_unstack(sparse_df, multi_index3):
37+
frame = pd.SparseSeries(np.ones(3), index=multi_index3).unstack()
38+
tm.assert_sp_frame_equal(frame, sparse_df)

pandas/tests/test_multilevel.py

-33
Original file line numberDiff line numberDiff line change
@@ -2388,39 +2388,6 @@ def test_iloc_mi(self):
23882388
tm.assert_frame_equal(result, expected)
23892389

23902390

2391-
@pytest.fixture
2392-
def sparse_df():
2393-
return pd.SparseDataFrame({0: {0: 1}, 1: {1: 1}, 2: {2: 1}}) # eye
2394-
2395-
2396-
@pytest.fixture
2397-
def multi_index3():
2398-
return MultiIndex.from_tuples([(0, 0), (1, 1), (2, 2)])
2399-
2400-
2401-
def test_sparse_frame_stack(sparse_df, multi_index3):
2402-
ss = sparse_df.stack()
2403-
expected = pd.SparseSeries(np.ones(3), index=multi_index3)
2404-
tm.assert_sp_series_equal(ss, expected)
2405-
2406-
2407-
def test_sparse_frame_unstack(sparse_df):
2408-
mi = MultiIndex.from_tuples([(0, 0), (1, 0), (1, 2)])
2409-
sparse_df.index = mi
2410-
arr = np.array([[1, np.nan, np.nan],
2411-
[np.nan, 1, np.nan],
2412-
[np.nan, np.nan, 1]])
2413-
unstacked_df = pd.DataFrame(arr, index=mi).unstack()
2414-
unstacked_sdf = sparse_df.unstack()
2415-
2416-
tm.assert_numpy_array_equal(unstacked_df.values, unstacked_sdf.values)
2417-
2418-
2419-
def test_sparse_series_unstack(sparse_df, multi_index3):
2420-
frame = pd.SparseSeries(np.ones(3), index=multi_index3).unstack()
2421-
tm.assert_sp_frame_equal(frame, sparse_df)
2422-
2423-
24242391
class TestSorted(Base):
24252392
""" everthing you wanted to test about sorting """
24262393

0 commit comments

Comments
 (0)