Code Sample, a copy-pastable example if possible
When the index is set from 0, df.ix[0,0] works fine.
>>> df = pd.DataFrame({'A':[1,2,3], 'B':[10,20,30]},index=[0,1,2])
>>> df
A B
0 1 10
1 2 20
2 3 30
>>> df.ix[0,1]
10
When the index is set from other 0, df.ix[0,0] cases exception.
>>> df2 = pd.DataFrame({'A':[1,2,3], 'B':[10,20,30]},index=[2,3,4])
>>> df2
A B
2 1 10
3 2 20
4 3 30
>>> df2.ix[1,0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "c:\python27\lib\site-packages\pandas\core\indexing.py", line 75, in __ge
titem__
return self._getitem_tuple(key)
File "c:\python27\lib\site-packages\pandas\core\indexing.py", line 784, in _ge
titem_tuple
return self._getitem_lowerdim(tup)
File "c:\python27\lib\site-packages\pandas\core\indexing.py", line 908, in _ge
titem_lowerdim
section = self._getitem_axis(key, axis=i)
File "c:\python27\lib\site-packages\pandas\core\indexing.py", line 1018, in _g
etitem_axis
return self._get_label(key, axis=axis)
File "c:\python27\lib\site-packages\pandas\core\indexing.py", line 93, in _get
_label
return self.obj._xs(label, axis=axis)
File "c:\python27\lib\site-packages\pandas\core\generic.py", line 1749, in xs
loc = self.index.get_loc(key)
File "c:\python27\lib\site-packages\pandas\indexes\base.py", line 1947, in get
_loc
return self._engine.get_loc(self._maybe_cast_indexer(key))
File "pandas\index.pyx", line 137, in pandas.index.IndexEngine.get_loc (pandas
\index.c:4066)
File "pandas\index.pyx", line 159, in pandas.index.IndexEngine.get_loc (pandas
\index.c:3930)
File "pandas\hashtable.pyx", line 303, in pandas.hashtable.Int64HashTable.get_
item (pandas\hashtable.c:6527)
File "pandas\hashtable.pyx", line 309, in pandas.hashtable.Int64HashTable.get_
item (pandas\hashtable.c:6465)
KeyError: 1L
output of pd.show_versions()
pd.show_versions()
INSTALLED VERSIONS
commit: None python: 2.7.12.final.0 python-bits: 32 OS: Windows OS-release: 7 machine: AMD64 processor: Intel64 Family 6 Model 94 Stepping 3, GenuineIntel byteorder: little LC_ALL: None LANG: None
pandas: 0.18.1 nose: None pip: 8.1.2 setuptools: 20.10.1 Cython: None numpy: 1.11.1 scipy: None statsmodels: None xarray: None IPython: 5.0.0 sphinx: None patsy: None dateutil: 2.5.3 pytz: 2016.4 blosc: None bottleneck: None tables: None numexpr: None matplotlib: 1.5.1 openpyxl: 2.3.5 xlrd: 1.0.0 xlwt: 1.1.2 xlsxwriter: None lxml: None bs4: 4.4.1 html5lib: None httplib2: None apiclient: None sqlalchemy: 0.9.8 pymysql: None psycopg2: None jinja2: 2.8 boto: None pandas_datareader: None
Comment From: jorisvandenbossche
The reason you get this error is because 1 is not contained in your integer index (hence the KeyError). And when having an integer index, ix
will always look at the labels and not fallback to integer positional indexing (while for the columns, which are not integers but strings, it does use integer positional indexing).
Comment From: jorisvandenbossche
See this part of the indexing docs: http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing, the part on .ix
mentions this behaviour.