Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most idiomatic way to index an object with a boolean array in pandas?

Tags:

python

pandas

I am particularly talking about Pandas version 0.11 as I am busy replacing my uses of .ix with either .loc or .iloc. I like the fact that differentiating between .loc and .iloc communicates whether I am intending to index by label or integer position. I see that either one will accept a boolean array as well but I would like to keep their usage pure to clearly communicate my intent.

like image 277
snth Avatar asked May 17 '13 07:05

snth


2 Answers

In 11.0 all three methods work, the way suggested in the docs is simply to use df[mask]. However, this is not done on position, but purely using labels, so in my opinion loc best describes what's actually going on.

Update: I asked on github about this, the conclusion being that df.iloc[msk] will give a NotImplementedError (if integer indexed mask) or ValueError (if non-integer indexed) in pandas 11.1.

In [1]: df = pd.DataFrame(range(5), list('ABCDE'), columns=['a'])

In [2]: mask = (df.a%2 == 0)

In [3]: mask
Out[3]:
A     True
B    False
C     True
D    False
E     True
Name: a, dtype: bool

In [4]: df[mask]
Out[4]:
   a
A  0
C  2
E  4

In [5]: df.loc[mask]
Out[5]:
   a
A  0
C  2
E  4

In [6]: df.iloc[mask]  # Due to this question, this will give a ValueError (in 11.1)
Out[6]:
   a
A  0
C  2
E  4

Perhaps worth noting that if you gave mask integer index it would throw an error:

mask.index = range(5)
df.iloc[mask]  # or any of the others
IndexingError: Unalignable boolean Series key provided

This demonstrates that iloc isn't actually implemented, it uses label, hence why 11.1 will throw a NotImplementedError when we try this.

like image 88
Andy Hayden Avatar answered Oct 25 '22 18:10

Andy Hayden


I am currently using [], i.e. __getitem__(), e.g.

df = pd.DataFrame(dict(a=range(5)))
df[df.a%2==0]
like image 27
snth Avatar answered Oct 25 '22 19:10

snth