Casting int to bool in C/C++

0 values of basic types (1)(2)map to false. Other values map to true. This convention was established in original C, via its flow control statements; C didn’t have a boolean type at the time. It’s a common error to assume that as function return values, false indicates failure. But in particular from main it’s false … Read more

Convert Pandas series containing string to boolean

You can just use map: In [7]: df = pd.DataFrame({‘Status’:[‘Delivered’, ‘Delivered’, ‘Undelivered’, ‘SomethingElse’]}) In [8]: df Out[8]: Status 0 Delivered 1 Delivered 2 Undelivered 3 SomethingElse In [9]: d = {‘Delivered’: True, ‘Undelivered’: False} In [10]: df[‘Status’].map(d) Out[10]: 0 True 1 True 2 False 3 NaN Name: Status, dtype: object

Type-casting to boolean

It’s actually not as strange it seems. (bool) has higher precedence than ==, so this: var_dump((bool) 1==2); is equivalent to this: var_dump( ((bool) 1) == 2); or this: var_dump(true == 2); Due to type juggling, the 2 also essentially gets cast to bool (since this is a “loose comparison”), so it’s equivalent to this: var_dump(true … Read more

OpenGL bool uniform?

§ 4.1 Basic Types The OpenGL Shading Language supports the following basic data types, grouped as follows: bool a conditional type, taking on values of true or false bvec2 a two-component Boolean vector bvec3 a three-component Boolean vector bvec4 a four-component Boolean vector … § 4.1.2 Booleans To make conditional execution of code easier to … Read more

Why does bool(xml.etree.ElementTree.Element) evaluate to False?

As it turns out, Element objects are considered a False value if they have no children. I found this in the source: def __nonzero__(self): warnings.warn( “The behavior of this method will change in future versions. ” “Use specific ‘len(elem)’ or ‘elem is not None’ test instead.”, FutureWarning, stacklevel=2 ) return len(self._children) != 0 # emulate … Read more

Pandas: Why are double brackets needed to select column after boolean indexing

For pandas objects (Series, DataFrame), the indexing operator [] only accepts colname or list of colnames to select column(s) slicing or Boolean array to select row(s), i.e. it only refers to one dimension of the dataframe. For df[[colname(s)]], the interior brackets are for list, and the outside brackets are indexing operator, i.e. you must use … Read more