Skip to content

Commit

Permalink
replace tests with pytest style
Browse files Browse the repository at this point in the history
  • Loading branch information
kernc committed Jul 11, 2017
1 parent e0e19a3 commit b7b76fd
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions pandas/tests/test_multilevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2381,27 +2381,37 @@ def test_iloc_mi(self):
tm.assert_frame_equal(result, expected)


class TestSparse(object):
def setup_method(self, method):
self.sdf = pd.SparseDataFrame({0: {0: 1}, 1: {1: 1}, 2: {2: 1}}) # eye
self.mi = MultiIndex.from_tuples([(0, 0), (1, 1), (2, 2)])
@pytest.fixture
def sparse_df():
return pd.SparseDataFrame({0: {0: 1}, 1: {1: 1}, 2: {2: 1}}) # eye


@pytest.fixture
def multi_index3():
return 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_stack(self):
ss = self.sdf.stack()
expected = pd.SparseSeries(np.ones(3), index=self.mi)
tm.assert_sp_series_equal(ss, expected)
def test_sparse_frame_unstack(sparse_df):
mi = 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()

def test_sparse_frame_unstack(self):
mi = pd.MultiIndex.from_tuples([(0, 0), (1, 0), (1, 2)])
sdf = self.sdf
sdf.index = mi
df = pd.DataFrame(np.eye(3), index=mi).replace(0, np.nan)
tm.assert_numpy_array_equal(unstacked_df.values, unstacked_sdf.values)

tm.assert_numpy_array_equal(df.unstack().values, sdf.unstack().values)

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


class TestSorted(Base):
Expand Down

0 comments on commit b7b76fd

Please sign in to comment.