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.
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.
I am currently using [], i.e. __getitem__(), e.g.
df = pd.DataFrame(dict(a=range(5)))
df[df.a%2==0]
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With