Skip to content

Fundamental issue with ix #328

@mcobzarenco

Description

@mcobzarenco

Using square bracket indexing [x] on a Series where x is an integer
seems to return:

  • the item at the index x if such index is present
  • the x-th item if the index does not exist
    To be precise:
    In [190]: ss = pandas.Series(range(10), index = range(0, 20, 2))
    ...: print ss
    ...:
    0 0
    2 1
    4 2
    6 3
    8 4
    10 5
    12 6
    14 7
    16 8
    18 9
    Name: None, Length: 10
    In [191]: ss[0]
    Out[191]: 0
    In [192]: ss[1]
    Out[192]: 1
    In [193]: ss[2]
    Out[193]: 1
    In [194]: ss[3]
    Out[194]: 3
    In [195]: ss[4]
    Out[195]: 2
    In [196]: ss[17]

KeyError Traceback (most recent call
last)
/home/marius/Code/ccapital/ in
()
----> 1 ss[17]
/usr/lib64/python2.7/site-packages/pandas/core/series.pyc in
getitem(self, key)
267 except Exception, _:
268 pass
--> 269 raise e1
270 except TypeError:
271 pass
KeyError: 17
In [197]: ss[18]
Out[197]: 9

Is this behaviour intended? To me it seems like a fundamental bug.

From the documentation:
Select row by location (int): df.ix[loc] Series
Which is not what ix seems to be doing.

If the labels are made strings, the behaviour is utterly different
(correct according to the documentation):
ss = pandas.Series(range(10), index = map(str, range(0, 20, 2)))
print ss
0 0
2 1
4 2
6 3
8 4
10 5
12 6
14 7
16 8
18 9
Name: None, Length: 10
ss[0]
Out[220]: 0
ss[1]
Out[221]: 1
ss[2]
Out[222]: 2

Many thanks,
-- Marius

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions