Pandas not throwing exception when using setitem

Viewed 161

Here's a minimal, complete and verifiable example of a piece of code that surprises (and bothers) me:

>>> import pandas as pd
>>> df = pd.DataFrame([[1,2], [3,4]], columns = ['a','b'])
>>> df['a'].iloc[0,0] = 10 # df has not changed, and no exception is thrown
>>> df 
   a  b
0  1  2
1  3  4
>>> df['a'].iloc[0,0] # this throws an exception
IndexingError                             Traceback (most recent call last)

So when I call setitem with iloc[0,0] no exception is raised, but nothing is set. When I call getitem with iloc[0,0] an exception is raised. This seems fairly inconsistent to me. I would have expected either both getitem and setitem to raise an exception, or both to return me a scalar within the dataframe.

1 Answers
Related