Skip to content

Series.loc/iloc[x, y] does not raise exception #13831

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
pkch opened this issue Jul 28, 2016 · 5 comments · Fixed by #44656
Closed

Series.loc/iloc[x, y] does not raise exception #13831

pkch opened this issue Jul 28, 2016 · 5 comments · Fixed by #44656
Assignees
Labels
Bug Indexing Related to indexing on series/frames, not to indexes themselves
Milestone

Comments

@pkch
Copy link

pkch commented Jul 28, 2016

Code Sample, a copy-pastable example if possible

df = pd.DataFrame({'a': [10]})
# df['a'] is a series, can be indexed with 1 index only

# will raise IndexingError, as expected
df['a'].iloc[0, 0]
df['a'].loc[0, 0]

# will raise nothing, not as expected
df['a'].iloc[0, 0] = 1000 # equivalent to pass
df['a'].loc[0, 0] = 1000 # equivalent to df['a'].loc[0] = 1000

See this SO post

Expected Output

Expected an IndexingError exception in every case where two values are used to index a Series.

output of pd.show_versions()

pandas version 0.18.1, python 3.5

@jreback
Copy link
Contributor

jreback commented Jul 28, 2016

this is a bug for a non-MultiIndex. (for .loc). .iloc should raise as well.

BUT this IS valid for a MI.

In [10]: s = Series([1,2,3], index=pd.MultiIndex.from_tuples([(0,'a'),(0,'b'),(1,'a')]))

In [11]: s
Out[11]: 
0  a    1
   b    2
1  a    3
dtype: int64

In [12]: s.loc[0,'d'] = 4

In [13]: s
Out[13]: 
0  a    1
   b    2
1  a    3
0  d    4
dtype: int64

@jreback
Copy link
Contributor

jreback commented Jul 28, 2016

pull requests are welcome, keep in mind that there are hundreds of existing test cases that still must pass, so careful changing things.

Further note that you ARE chained indexing, and SETTING, which in this particular case is ok (as its a single dtype), but in general is NOT recommended, nor can you always detect it.

@jreback
Copy link
Contributor

jreback commented Jul 28, 2016

This does raise for a non-integer index already (for .loc)

In [22]: s = Series([1,2,3], index=list('abc'))

In [23]: s.loc[0,1] = 5
KeyError: '[0 1] not in index'

so this could use some more comprehensive testing.

@jreback jreback added Bug Indexing Related to indexing on series/frames, not to indexes themselves Difficulty Intermediate labels Jul 28, 2016
@jreback jreback added this to the Next Major Release milestone Jul 28, 2016
@simonjayhawkins
Copy link
Member

the iloc setitem case now raises as expected. on 0.25.3 gave IndexingError: Too many indexers same as the getitem cases. but on master now gives IndexError: too many indices for array

the loc setitem still does not raise.

@DriesSchaumont
Copy link
Member

take

@jreback jreback modified the milestones: Contributions Welcome, 1.4 Nov 28, 2021
@jreback jreback modified the milestones: 1.4, 1.5 Jan 10, 2022
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.

5 participants