Skip to content

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

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

Closed
shoyer opened this issue Feb 11, 2015 · 0 comments
Closed

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

shoyer opened this issue Feb 11, 2015 · 0 comments
Labels
Bug Sparse Sparse Data Type
Milestone

Comments

@shoyer
Copy link
Member

shoyer commented Feb 11, 2015

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Sparse Sparse Data Type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants