tskit icon indicating copy to clipboard operation
tskit copied to clipboard

Error if boolean passed as row index or slice?

Open benjeffery opened this issue 2 years ago • 3 comments

I was recently helping a new user who has R experience who tried tables.nodes['individual'==0] which gave the first node as it is equal to tables.nodes[False] which gets interpreted as tables.nodes[0]. (This is because isinstance(False, numbers.Integral) is True which was a surprise to me)

I think it should be a ValueError to __getitem__ with a bool? Or do what numpy does and return [].

benjeffery avatar Feb 02 '23 12:02 benjeffery

Good idea.

Is there a doc example of what would (should?) work: something like tables.nodes[np.where('individual' == 0)]?

molpopgen avatar Feb 02 '23 17:02 molpopgen

I don't think there is a doc example. The equivalent code would be tables.nodes[tables.nodes.individual == 0] but then for most use cases this would be a very inefficient way to access nodes.

benjeffery avatar Feb 03 '23 11:02 benjeffery

Yes, this is a good idea. I can't see any way that (e.g.) raising a warning instead would help (unless I guess people were somehow legitimately doing tables.nodes[False])?

jeromekelleher avatar Feb 03 '23 12:02 jeromekelleher

Closing for inactivity and labelling "future", please re-open if you plan to work on this.

benjeffery avatar Jun 09 '25 15:06 benjeffery