Skip to content
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

.iloc with 1-element integer array behaves badly #5006

Closed
hmeine opened this issue Sep 27, 2013 · 8 comments · Fixed by #5134
Closed

.iloc with 1-element integer array behaves badly #5006

hmeine opened this issue Sep 27, 2013 · 8 comments · Fixed by #5134
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@hmeine
Copy link

hmeine commented Sep 27, 2013

There is an inconsistency in the returned (python) types when indexing .iloc with an integer array. The result should always be a sequence (Series), but if the array contains exactly one index, it returns a scalar value:

>>> import pandas, numpy
>>> column = pandas.Series(numpy.arange(10))
>>> indices, = (column > 4).nonzero()
>>> column.iloc[indices]
5    5
6    6
7    7
8    8
9    9
dtype: int64
>>> indices, = (column == 5).nonzero()
>>> column.iloc[indices]
5
>>> indices, = (column == 99).nonzero()
>>> column.iloc[indices]
Series([], dtype: int64)

I am working around that using numpy.atleast_1d, but it's ugly.

@jreback
Copy link
Contributor

jreback commented Sep 27, 2013

looks like a buglet, not treating the np.ndarray input the same as a list, marking as a bug

In [21]: indices
Out[21]: array([5])

In [22]: type(indices)
Out[22]: numpy.ndarray

In [23]: column.iloc[list(indices)]
Out[23]: 
5    5
dtype: int64

fyi....this would be the preferred usage of what you are doing in any event

In [26]: column.loc[column==5]
Out[26]: 
5    5
dtype: int64

@ghost ghost assigned jreback Sep 27, 2013
@hmeine
Copy link
Author

hmeine commented Sep 27, 2013

Thanks, but I have different use cases: I am often using .diff().nonzero() in order to find indices where relevant parameters change (e.g. an error code, depending on some input parameters). Otherwise, I am using boolean selections where possible.

@jreback
Copy link
Contributor

jreback commented Sep 27, 2013

np.....pretty trivial fix, want to do a PR?

@hmeine
Copy link
Author

hmeine commented Sep 27, 2013

I might have a look later (Monday), but I have to hurry and leave in < 5 minutes. ;-)

@jreback
Copy link
Contributor

jreback commented Sep 27, 2013

gr8.....will leave open for you!

@jreback
Copy link
Contributor

jreback commented Oct 2, 2013

@hmeine doing a PR for this?

@hmeine
Copy link
Author

hmeine commented Oct 3, 2013

Did not forget about it, but did not yet get around to it, either. Is pandas/tests/test_indexing.py the right place for a regression test? Here's a holiday today, so I have four family days. Can't say yet whether that makes it harder or easier to find time for hacking. ;-)

@jreback
Copy link
Contributor

jreback commented Oct 3, 2013

test_indexing is good for this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Dtype Conversions Unexpected or buggy dtype conversions Indexing Related to indexing on series/frames, not to indexes themselves
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants