MSData
MSData

Reputation: 35

Inconsistent behavior of ix selection with duplicate indices

Consider the pandas data frame

df = DataFrame({'somedata': [13,24,54]}, index=[1,1,2])

    somedata
1   13
1   24
2   54

Executing

df.ix[1, 'somedata']

will return an object

1    13
1    24
Name: somedata, dtype: int64

which has an index:

df.ix[1, 'somedata'].index
Int64Index([1, 1], dtype='int64')

However, executing

df.ix[2, 'somedata']

will return just the number 54, which has no index:

df.ix[2, 'somedata'].index
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-274-3c6e4b1e6441> in <module>()
----> 1 df.ix[2, 'somedata'].index

AttributeError: 'numpy.int64' object has no attribute 'index'

I do not understand this (seemingly?) inconsistent behavior. Is it on purpose? I would expect objects that are returned from the same operation to have the same structure. Further, I need to build my code around this issue, so my question is, how do I detect what kind of object is returned by an ix selection? Currently I am checking for the returned object's len. I wonder if there is a more elegant way, or if one can force the selection, instead of returning just the number 54, to return the similar form

2    54
Name: somedata, dtype: int64

Sorry if this is a stupid question, I could not find an answer to this anywhere.

Upvotes: 2

Views: 62

Answers (1)

DSM
DSM

Reputation: 353359

If you pass a list of indices instead of a single index, you can guarantee that you're going to get a Series back. In other words, instead of

>>> df.loc[1, 'somedata']
1    13
1    24
Name: somedata, dtype: int64
>>> df.loc[2, 'somedata']
54

you could use

>>> df.loc[[1], 'somedata']
1    13
1    24
Name: somedata, dtype: int64
>>> df.loc[[2], 'somedata']
2    54
Name: somedata, dtype: int64

(Note that it's usually a better idea to use loc (or iloc) rather than ix because it's less magical, although that isn't what's causing your issue here.)

Upvotes: 4

Related Questions