Skip to content

Commit adb6553

Browse files
committed
BUG: SparseSeries slicing with Ellipsis raises KeyError
1 parent f813425 commit adb6553

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

doc/source/whatsnew/v0.18.1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ These changes conform sparse handling to return the correct types and work to ma
8282
s.take(0)
8383
s.take([1, 2, 3])
8484

85+
- Bug in ``SparseSeries.__getitem__`` with ``Ellipsis`` raises ``KeyError`` (:issue:`9467`)
8586
- Bug in ``SparseSeries.loc[]`` with list-like input raises ``TypeError`` (:issue:`10560`)
8687
- Bug in ``SparseSeries.iloc[]`` with scalar input may raise ``IndexError`` (:issue:`10560`)
8788
- Bug in ``SparseSeries.loc[]``, ``.iloc[]`` with ``slice`` returns ``SparseArray``, rather than ``SparseSeries`` (:issue:`10560`)

pandas/sparse/series.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,14 @@ def _get_val_at(self, loc):
382382
return self.block.values._get_val_at(loc)
383383

384384
def __getitem__(self, key):
385-
"""
386-
387-
"""
388385
try:
389386
return self._get_val_at(self.index.get_loc(key))
390387

391388
except KeyError:
392389
if isinstance(key, (int, np.integer)):
393390
return self._get_val_at(key)
391+
elif key is Ellipsis:
392+
return self
394393
raise Exception('Requested index not in this series!')
395394

396395
except TypeError:

pandas/sparse/tests/test_indexing.py

+8
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ def test_getitem_fill_value(self):
5555
exp = orig[orig % 2 == 1].to_sparse(fill_value=0)
5656
tm.assert_sp_series_equal(result, exp)
5757

58+
def test_getitem_ellipsis(self):
59+
# GH 9467
60+
s = pd.SparseSeries([1, np.nan, 2, 0, np.nan])
61+
tm.assert_sp_series_equal(s[...], s)
62+
63+
s = pd.SparseSeries([1, np.nan, 2, 0, np.nan], fill_value=0)
64+
tm.assert_sp_series_equal(s[...], s)
65+
5866
def test_loc(self):
5967
orig = pd.Series([1, np.nan, np.nan, 3, np.nan])
6068
sparse = orig.to_sparse()

0 commit comments

Comments
 (0)