Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: What's the proper way to convert between python bool and numpy bool_?

Tags:

boolean

numpy

I kept getting errors with a numpy ndarray with booleans not being accepted as a mask by a pandas structure when it occured to me that I may have the 'wrong' booleans. Edit: it was not a raw numpy array but a pandas.Index.

While I was able to find a solution, the only one that worked was quite ugly:

mymask = mymask.astype(np.bool_) #ver.1 does not work, elements remain <class 'bool'>
mymask = mymask==True #ver.2, does work, elements become <class 'numpy.bool_'>
mypdstructure[mymask] 

What's the proper way to typecast the values?

like image 593
John Smith Avatar asked Apr 27 '26 22:04

John Smith


1 Answers

Ok, I found the problem. My original post was not fully correct: my mask was a pandas.Index.

It seems that the pands.Index.astype is behaving unexpectedly (for me), as I get different behavior for the following:

mask = pindex.map(myfun).astype(np.bool_) # doesn't cast
mask = pindex.map(myfun).astype(np.bool_,copy=False) # doesn't cast
mask = pindex.map(myfun).values.astype(np.bool_) # does cast

Maybe it is actually a pandas bug? This result is surprising to me because I was under the impression that pandas is usually just calling the functions of the numpy arrays that it is based on. This is clearly not the case here.

like image 50
John Smith Avatar answered May 01 '26 01:05

John Smith