Skip to content

BUG/CLN: SparseSeries __getitem__ uses questionable, buggy logic #9467

Closed
@shoyer

Description

@shoyer

SparseSeries currently relies on the assumption that all non-scalar input will raise a TypeError (because it won't be hashable) when used as input to Index.get_loc:
https://github.com/pydata/pandas/blob/bb9c311f36fbf508d11f2eabe3fdbdd010abf8c0/pandas/sparse/series.py#L361

However, some non-scalar input is hashable (e.g., Ellipsis):

In [2]: import pandas as pd

In [3]: ss = pd.Series(range(10)).to_sparse()

In [4]: ss[...]
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-4-f0ccf654123b> in <module>()
----> 1 ss[...]

/Users/shoyer/dev/pandas/pandas/sparse/series.pyc in __getitem__(self, key)
    369             if isinstance(key, (int, np.integer)):
    370                 return self._get_val_at(key)
--> 371             raise Exception('Requested index not in this series!')
    372
    373         except TypeError:

Exception: Requested index not in this series!

(this should return the entire sparse series)

This logic is also problematic because it relies on get_loc always throwing TypeError for invalid input. So this could use some cleanup, ideally checking directly if the input is hashable. And it probably shouldn't be raising a generic Exception, either.

xref tslib.pyx change/discussion in #9258

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions