Skip to content

Commit 7945cb4

Browse files
committed
Tests for .loc() and .reindex() on sparse series with MultiIndex
1 parent 55b99f8 commit 7945cb4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

pandas/tests/sparse/test_indexing.py

+27
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,11 @@ def test_loc(self):
504504
exp = orig.loc[[1, 3, 4, 5]].to_sparse()
505505
tm.assert_sp_series_equal(result, exp)
506506

507+
# single element list (GH 15447)
508+
result = sparse.loc[['A']]
509+
exp = orig.loc[['A']].to_sparse()
510+
tm.assert_sp_series_equal(result, exp)
511+
507512
# dense array
508513
result = sparse.loc[orig % 2 == 1]
509514
exp = orig.loc[orig % 2 == 1].to_sparse()
@@ -537,6 +542,28 @@ def test_loc_slice(self):
537542
orig.loc['A':'B'].to_sparse())
538543
tm.assert_sp_series_equal(sparse.loc[:'B'], orig.loc[:'B'].to_sparse())
539544

545+
def test_reindex(self):
546+
orig = self.orig
547+
sparse = self.sparse
548+
549+
res = sparse.reindex([('A', 0), ('C', 1)])
550+
exp = orig.reindex([('A', 0), ('C', 1)]).to_sparse()
551+
tm.assert_sp_series_equal(res, exp)
552+
553+
# On specific level:
554+
res = sparse.reindex(['A', 'C', 'B'], level=0)
555+
exp = orig.reindex(['A', 'C', 'B'], level=0).to_sparse()
556+
tm.assert_sp_series_equal(res, exp)
557+
558+
# single element list (GH 15447)
559+
res = sparse.reindex(['A'], level=0)
560+
exp = orig.reindex(['A'], level=0).to_sparse()
561+
tm.assert_sp_series_equal(res, exp)
562+
563+
with tm.assertRaises(TypeError):
564+
# Incomplete keys are not accepted for reindexing:
565+
sparse.reindex(['A', 'C'])
566+
540567

541568
class TestSparseDataFrameIndexing(tm.TestCase):
542569

0 commit comments

Comments
 (0)