Skip to content

Commit b7b76fd

Browse files
committed
replace tests with pytest style
1 parent e0e19a3 commit b7b76fd

File tree

1 file changed

+27
-17
lines changed

1 file changed

+27
-17
lines changed

pandas/tests/test_multilevel.py

+27-17
Original file line numberDiff line numberDiff line change
@@ -2381,27 +2381,37 @@ def test_iloc_mi(self):
23812381
tm.assert_frame_equal(result, expected)
23822382

23832383

2384-
class TestSparse(object):
2385-
def setup_method(self, method):
2386-
self.sdf = pd.SparseDataFrame({0: {0: 1}, 1: {1: 1}, 2: {2: 1}}) # eye
2387-
self.mi = MultiIndex.from_tuples([(0, 0), (1, 1), (2, 2)])
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+
23882399

2389-
def test_sparse_frame_stack(self):
2390-
ss = self.sdf.stack()
2391-
expected = pd.SparseSeries(np.ones(3), index=self.mi)
2392-
tm.assert_sp_series_equal(ss, expected)
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()
23932408

2394-
def test_sparse_frame_unstack(self):
2395-
mi = pd.MultiIndex.from_tuples([(0, 0), (1, 0), (1, 2)])
2396-
sdf = self.sdf
2397-
sdf.index = mi
2398-
df = pd.DataFrame(np.eye(3), index=mi).replace(0, np.nan)
2409+
tm.assert_numpy_array_equal(unstacked_df.values, unstacked_sdf.values)
23992410

2400-
tm.assert_numpy_array_equal(df.unstack().values, sdf.unstack().values)
24012411

2402-
def test_sparse_series_unstack(self):
2403-
frame = pd.SparseSeries(np.ones(3), index=self.mi).unstack()
2404-
tm.assert_sp_frame_equal(frame, self.sdf)
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)
24052415

24062416

24072417
class TestSorted(Base):

0 commit comments

Comments
 (0)