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

2024/10/8 4:27:43

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.

Answer

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: boolIn [4]: df[mask]
Out[4]:a
A  0
C  2
E  4In [5]: df.loc[mask]
Out[5]:a
A  0
C  2
E  4In [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.

https://en.xdnf.cn/q/70164.html

Related Q&A

Error: Command failed with rc=65536 python and mod_wsgi

im having this problem: im runing pythonbrew to get python2.7, and so i re-compiled mod_wsgi to use the 2.7 python. to that end, i followed this tutorial: code.google.com/p/modwsgi/wiki/QuickInstallati…

Why cant I freeze_panes on the xlsxwriter object pandas is creating for me?

I have a class whose objects contain pandas dataframes (self.before, and self.after below) and a save() method which uses xlsxwriter to export the data (which has two worksheets, "before" and…

Python multiprocessing pipe recv() doc unclear or did I miss anything?

I have been learning how to use the Python multiprocessing module recently, and reading the official doc. In 16.6.1.2. Exchanging objects between processes there is a simple example about using pipe t…

How would you unit test this SQLAlchemy Core query/function?

Im working on learning how to unit test properly. Given this function...def get_user_details(req_user_id):users = sa.Table(users, db.metadata, autoload=True)s = sa.select([users.c.username,users.c.favo…

Retrieve wall-time in Python using the standard library?

How can I retrieve wall-time in Python using the standard library?This question, and this question would suggest that something like clock_gettime(CLOCK_MONOTONIC_RAW) or /proc/uptime are most appropr…

NLTK: Package Errors? punkt and pickle?

Basically, I have no idea why Im getting this error. Just to have more than an image, here is a similar message in code format. As it is more recent, the answer of this thread has already been mentione…

Is there a bit-wise trick for checking the divisibility of a number by 2 or 3?

I am looking for a bit-wise test equivalent to (num%2) == 0 || (num%3) == 0.I can replace num%2 with num&1, but Im still stuck with num%3 and with the logical-or.This expression is also equivalent …

Check image urls using python-markdown

On a website Im creating Im using Python-Markdown to format news posts. To avoid issues with dead links and HTTP-content-on-HTTPS-page problems Im requiring editors to upload all images to the site and…

How to unittest command line arguments?

I am trying to supply command line arguments to Python unittest and facing some issues. I have searched on internet and found a way to supply arguments asunittest.main(argv=[myArg])The issue is this wo…

different foreground colors for each line in wxPython wxTextCtrl

I have a multilinewx.TextCtrl()object which I set its forground and Background colors for writing strings.I need to write different lines with different colors ,wx.TextCtrl.setForgroundcolor()changes a…