Search code examples
pythonpandasindexingseries

how does Pandas actually perform indexing on custom based indices (integer and non-integer)


temp = pd.Series(np.random.randint(1, 10, 5), index=['John', 'Joe', 'Bob', 'Alice', 'Kris'])
temp[-1]

this outputs to the value associated with 'Kris' however, when I do this:

temp2 = pd.Series(['John', 'Joe', 'Bob', 'Alice', 'Kris'], index=[5, 6, 7, 8, 9])
temp2[-1]

it throws a KeyError

I have a brief idea that Pandas performs indexing internally when the custom index is of non-integer type, but what I don't understand here, is how negative indexing works? does the -1 essentially mean that Pandas is referring to the custom index list and then getting the '-1'th or the last element, which is Kris, and then passes 'Kris' as a label to the index?

I do know that a single integer is treated as a label in indexing, so how does this work? and that too, only for custom indexes where the type is non-integer; what am I missing here?


Solution

  • This mixed type of indexing probably shouldn't exist and is definitely not reliable (it will actually be removed in future versions).

    Be explicit, use iloc for positional indexing:

    temp2.iloc[-1]
    

    Output: 'Kris'

    why does temp2[-1] fail?

    If you have an integer index, you could have negative values:

    temp3 = pd.Series(['John', 'Joe', 'Bob', 'Alice', 'Kris'], index=[5, -1, 7, 8, 9])
    temp3[-1] # 'Joe'