Skip to content

BUG: Float64Index getitem raises error with tuple-like values #13509

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
jorisvandenbossche opened this issue Jun 24, 2016 · 3 comments
Closed
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@jorisvandenbossche
Copy link
Member

So when you have a Series with the combination of a Float64Index and tuple values, getitem/[] gives an error:

In [46]: s = pd.Series([1,2,3], index=[0.0,0.1,0.2])

In [47]: s[0.0]
Out[47]: 1

In [48]: s = pd.Series([(1,1),(2,2),(3,3)], index=[0.0,0.1,0.2])

In [49]: s[0.0]
...
TypeError: len() of unsized object

When the values are not tuples, or when you have a regular index, or when using ix/loc/iloc this issue does not appear.

This issue is coming from geopandas (geopandas/geopandas#351), where a Series of Point geometries give the same error:

In [66]: import shapely.geometry

In [67]: s = pd.Series([(1,1),(2,2),(3,3)], index=[0.0,0.1,0.2])

In [68]: s = s.map(shapely.geometry.Point)

In [69]: s
Out[69]:
0.0    POINT (1 1)
0.1    POINT (2 2)
0.2    POINT (3 3)
dtype: object

In [70]: s[0.0]
...
TypeError: len() of unsized object
@jorisvandenbossche jorisvandenbossche added Bug Indexing Related to indexing on series/frames, not to indexes themselves labels Jun 24, 2016
@bkandel
Copy link
Contributor

bkandel commented Aug 19, 2016

I did a bit of digging and I think I found the issue. Float64Index.get_value tries to construct a Series with its result if the result is not scalar. A scalar cannot be given as the index for a new Series, so the construction of the series fails. This switch in particular seems problematic: https://github.com/pydata/pandas/blob/master/pandas/indexes/numeric.py#L303

I can't figure out what that switch is trying to do. Even if the resulting value is not a scalar, it seems to me like it should still be treated like any other value that would be retrieved from a Series and simply returned, not used to construct a new Series. Codecov reports that this switch is not tested: https://codecov.io/gh/pydata/pandas/src/8131b5151e10f1a5863ccadef739c21320830f54/pandas/indexes/numeric.py#L306 So it's difficult for me to figure out what the intended function of this code is.

I would propose to simply take out the switch and always return the retrieved value. Any objections?

@jreback
Copy link
Contributor

jreback commented Aug 19, 2016

generally if the indexer returns a single value then it IS the result and you won't contruct a new series

@bkandel
Copy link
Contributor

bkandel commented Aug 19, 2016

That's what I thought too. I'll change it and add tests.

@jreback jreback added this to the 0.19.0 milestone Aug 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants