Skip to content

TST: Add tests for sparse quantile/where #17568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions pandas/tests/sparse/test_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1398,3 +1398,139 @@ def test_numpy_func_call(self):
'std', 'min', 'max']
for func in funcs:
getattr(np, func)(self.frame)

@pytest.mark.parametrize('data', [
[[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]],
[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [nan, nan]],
[
[1.0, 1.0 + 1.0j],
[2.0 + 2.0j, 2.0],
[3.0, 3.0 + 3.0j],
[4.0 + 4.0j, 4.0],
[nan, nan]
]
])
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
'(GH 17386)')
def test_where_with_numeric_data(self, data):
# GH 17386
lower_bound = 1.5

sparse = SparseDataFrame(data)
result = sparse.where(sparse > lower_bound)

dense = DataFrame(data)
dense_expected = dense.where(dense > lower_bound)
sparse_expected = SparseDataFrame(dense_expected)

tm.assert_frame_equal(result, dense_expected)
tm.assert_sp_frame_equal(result, sparse_expected)

@pytest.mark.parametrize('data', [
[[1, 1], [2, 2], [3, 3], [4, 4], [0, 0]],
[[1.0, 1.0], [2.0, 2.0], [3.0, 3.0], [4.0, 4.0], [nan, nan]],
[
[1.0, 1.0 + 1.0j],
[2.0 + 2.0j, 2.0],
[3.0, 3.0 + 3.0j],
[4.0 + 4.0j, 4.0],
[nan, nan]
]
])
@pytest.mark.parametrize('other', [
True,
-100,
0.1,
100.0 + 100.0j
])
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
'(GH 17386)')
def test_where_with_numeric_data_and_other(self, data, other):
# GH 17386
lower_bound = 1.5

sparse = SparseDataFrame(data)
result = sparse.where(sparse > lower_bound, other)

dense = DataFrame(data)
dense_expected = dense.where(dense > lower_bound, other)
sparse_expected = SparseDataFrame(dense_expected,
default_fill_value=other)

tm.assert_frame_equal(result, dense_expected)
tm.assert_sp_frame_equal(result, sparse_expected)

@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
'(GH 17386)')
def test_where_with_bool_data(self):
# GH 17386
data = [[False, False], [True, True], [False, False]]
cond = True

sparse = SparseDataFrame(data)
result = sparse.where(sparse == cond)

dense = DataFrame(data)
dense_expected = dense.where(dense == cond)
sparse_expected = SparseDataFrame(dense_expected)

tm.assert_frame_equal(result, dense_expected)
tm.assert_sp_frame_equal(result, sparse_expected)

@pytest.mark.parametrize('other', [
True,
0,
0.1,
100.0 + 100.0j
])
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
'(GH 17386)')
def test_where_with_bool_data_and_other(self, other):
# GH 17386
data = [[False, False], [True, True], [False, False]]
cond = True

sparse = SparseDataFrame(data)
result = sparse.where(sparse == cond, other)

dense = DataFrame(data)
dense_expected = dense.where(dense == cond, other)
sparse_expected = SparseDataFrame(dense_expected,
default_fill_value=other)

tm.assert_frame_equal(result, dense_expected)
tm.assert_sp_frame_equal(result, sparse_expected)

@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
'(GH 17386)')
def test_quantile(self):
# GH 17386
data = [[1, 1], [2, 10], [3, 100], [nan, nan]]
q = 0.1

sparse_df = SparseDataFrame(data)
result = sparse_df.quantile(q)

dense_df = DataFrame(data)
dense_expected = dense_df.quantile(q)
sparse_expected = SparseSeries(dense_expected)

tm.assert_series_equal(result, dense_expected)
tm.assert_sp_series_equal(result, sparse_expected)

@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
'(GH 17386)')
def test_quantile_multi(self):
# GH 17386
data = [[1, 1], [2, 10], [3, 100], [nan, nan]]
q = [0.1, 0.5]

sparse_df = SparseDataFrame(data)
result = sparse_df.quantile(q)

dense_df = DataFrame(data)
dense_expected = dense_df.quantile(q)
sparse_expected = SparseDataFrame(dense_expected)

tm.assert_frame_equal(result, dense_expected)
tm.assert_sp_frame_equal(result, sparse_expected)
102 changes: 102 additions & 0 deletions pandas/tests/sparse/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,6 +1419,108 @@ def test_deprecated_reindex_axis(self):
self.bseries.reindex_axis([0, 1, 2])
assert 'reindex' in str(m[0].message)

@pytest.mark.parametrize('data', [
[1, 1, 2, 2, 3, 3, 4, 4, 0, 0],
[1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, nan, nan],
[
1.0, 1.0 + 1.0j,
2.0 + 2.0j, 2.0,
3.0, 3.0 + 3.0j,
4.0 + 4.0j, 4.0,
nan, nan
]
])
@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
'(GH 17386)')
def test_where_with_numeric_data(self, data):
# GH 17386
lower_bound = 1.5

sparse = SparseSeries(data)
result = sparse.where(sparse > lower_bound)

dense = Series(data)
dense_expected = dense.where(dense > lower_bound)
sparse_expected = SparseSeries(dense_expected)

tm.assert_series_equal(result, dense_expected)
tm.assert_sp_series_equal(result, sparse_expected)

@pytest.mark.parametrize('data', [
[1, 1, 2, 2, 3, 3, 4, 4, 0, 0],
[1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0, nan, nan],
[
1.0, 1.0 + 1.0j,
2.0 + 2.0j, 2.0,
3.0, 3.0 + 3.0j,
4.0 + 4.0j, 4.0,
nan, nan
]
])
@pytest.mark.parametrize('other', [
True,
-100,
0.1,
100.0 + 100.0j
])
@pytest.mark.skip(reason='Wrong SparseBlock initialization '
'(Segfault) '
'(GH 17386)')
def test_where_with_numeric_data_and_other(self, data, other):
# GH 17386
lower_bound = 1.5

sparse = SparseSeries(data)
result = sparse.where(sparse > lower_bound, other)

dense = Series(data)
dense_expected = dense.where(dense > lower_bound, other)
sparse_expected = SparseSeries(dense_expected, fill_value=other)

tm.assert_series_equal(result, dense_expected)
tm.assert_sp_series_equal(result, sparse_expected)

@pytest.mark.xfail(reason='Wrong SparseBlock initialization '
'(GH 17386)')
def test_where_with_bool_data(self):
# GH 17386
data = [False, False, True, True, False, False]
cond = True

sparse = SparseSeries(data)
result = sparse.where(sparse == cond)

dense = Series(data)
dense_expected = dense.where(dense == cond)
sparse_expected = SparseSeries(dense_expected)

tm.assert_series_equal(result, dense_expected)
tm.assert_sp_series_equal(result, sparse_expected)

@pytest.mark.parametrize('other', [
True,
0,
0.1,
100.0 + 100.0j
])
@pytest.mark.skip(reason='Wrong SparseBlock initialization '
'(Segfault) '
'(GH 17386)')
def test_where_with_bool_data_and_other(self, other):
# GH 17386
data = [False, False, True, True, False, False]
cond = True

sparse = SparseSeries(data)
result = sparse.where(sparse == cond, other)

dense = Series(data)
dense_expected = dense.where(dense == cond, other)
sparse_expected = SparseSeries(dense_expected, fill_value=other)

tm.assert_series_equal(result, dense_expected)
tm.assert_sp_series_equal(result, sparse_expected)


@pytest.mark.parametrize(
'datetime_type', (np.datetime64,
Expand Down