0

I'm trying to slice into a multi-indexed data frame. I'm confused about conditions that generate IndexingError: Too many indexers. I'm also skeptical because I've found some bug reports about what may be this issue. Specifically, this generates the error:

idx1 = [str(elem) for elem in [5, 6, 7, 8]]
idx2 = [str(elem) for elem in [10, 20, 30]]
index = pd.MultiIndex.from_product([idx1, idx2], names=('idx1', 'idx2'))
columns = ['m1', 'm2', 'm3']
df = pd.DataFrame(index=index, columns= columns)
df['m1'].loc[:,10]

That code above is trying to index into an index of dtypes of str, with an int, it seems to me. The error threw me off, as I don't understand why it says Too many indexers.

The below code works:

idx1 = [5, 6, 7, 8]
idx2 = [10, 20, 30]
index = pd.MultiIndex.from_product([idx1, idx2], names=('idx1', 'idx2'))
columns = ['m1', 'm2', 'm3']
df = pd.DataFrame(index=index, columns= columns)
df.loc[5,10] = [1,2,3]
df.loc[6,10] = [4,5,6]
df.loc[7,10] = [7,8,9]
type(df2['m1'])
df['m1'].loc[:,10]

There are some references to the same error: https://github.com/pandas-dev/pandas/issues/13597 which is marked closed and https://github.com/pandas-dev/pandas/issues/14885 which is open. Is it ok to slice (a multi-indexed series) as in the lines above, assuming I get the dtype right? Also "Too many indexers" with DataFrame.loc

My pandas version is 20.3.

Erik Iverson
  • 309
  • 1
  • 11
  • Hi Erik - I understand that you can't attach your actual data, but can you post a small data frame that would allow us to replicate the problem? – ASGM Oct 17 '17 at 04:10
  • Thanks for the reply, I think I was making trouble for myself by trying to index with strings, which is what I though the index dtypes were... I got something to work finally, but I'll add more in the morning – Erik Iverson Oct 17 '17 at 05:19
  • Hi there! your first case works as expected if you use df['m1'].loc[:,'10']. As https://github.com/pandas-dev/pandas/issues/14885 hints, this really should be a KeyError, as 10 is not in the index but '10' is. As both the gitub bug and the other SO question you've linked to state, if you specify the index axis instead of letting pandas guess, you get the expected KeyError, e.g.: df['m1'].loc(axis=0)[:, 10] – Rob Buckley Oct 17 '17 at 21:04
  • That morning did never come... – Recessive Jun 05 '19 at 05:10

0 Answers0