Skip to content

Commit 1cec0f3

Browse files
committed
TST: move sparse stack/unstacking tests to sparse/test_reshape
1 parent 8a75621 commit 1cec0f3

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
@@ -2381,39 +2381,6 @@ def test_iloc_mi(self):
23812381
tm.assert_frame_equal(result, expected)
23822382

23832383

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

0 commit comments

Comments
 (0)