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

Fundamental issue with ix #328

Closed
mcobzarenco opened this issue Nov 3, 2011 · 3 comments

Comments

@mcobzarenco
Copy link

commented Nov 3, 2011

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

@changhiskhan

This comment has been minimized.

Copy link
Contributor

commented Nov 13, 2011

This is intended behavior. The whole issue arises because there is confusion when you use integer indices. i.e., when you pass in an integer key x, do you want the item corresponding to key x or the x-th item? The same logic applies in your second example, it's just that there is no item corresponding to key x because x is an integer while your index is all str.

I think you would rather allow a careful user to use integer keys when necessary than automatically converting all integer keys to str. In your second example where you convert the index into str, you would get this behavior:

0 0
10 5
12 6
14 7
16 8
18 9
2 1
...

@wesm

This comment has been minimized.

Copy link
Member

commented Jan 6, 2012

I'm considering making this API change for 0.7.0. I feel it would be for the greater good. Tag @adamklein on the discussion

wesm added a commit that referenced this issue Jan 6, 2012
wesm added a commit that referenced this issue Jan 7, 2012
@wesm

This comment has been minimized.

Copy link
Member

commented Jan 7, 2012

Alright, I went ahead and made this API change. It appears it was only necessary with Series-- everything else seems to be OK.

@wesm wesm closed this Jan 7, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.