Understanding the | operator in my project

I am trying to understand how to fix my error message. Its saying that floats and booleans don’t work togather.
This makes sense. True False values are not decimals. So I am asking myself how do I bridge this gap? I am trying to get rows with Data Analysts and Data Scientists.
Codecademy Links:

https://www.codecademy.com/paths/learn-python-for-data-science/tracks/intro-to-python-for-data-science-lpfds/modules/sorting-and-filtering-rows/lessons/sorting-and-filtering-rows/exercises/combining-booleans-with-or

Github Link: GitHub - strikeouts27/jupyter-data_scientist_salary_projects: An analysis about how data scientist have been compensated in 2022

Traceback:

TypeError                                 Traceback (most recent call last)
File ~/anaconda3/lib/python3.11/site-packages/pandas/core/ops/array_ops.py:311, in na_logical_op(x, y, op)
    302 try:
    303     # For exposition, write:
    304     #  yarr = isinstance(y, np.ndarray)
   (...)
    309     # Then Cases where this goes through without raising include:
    310     #  (xint or xbool) and (yint or bool)
--> 311     result = op(x, y)
    312 except TypeError:

TypeError: unsupported operand type(s) for |: 'float' and 'bool'

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[153], line 1
----> 1 is_analyst_and_scientist = [is_data_scientist | is_data_analyst]
      2 print(is_analyst_and_scientist)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/ops/common.py:81, in _unpack_zerodim_and_defer.<locals>.new_method(self, other)
     77             return NotImplemented
     79 other = item_from_zerodim(other)
---> 81 return method(self, other)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/arraylike.py:78, in OpsMixin.__or__(self, other)
     76 @unpack_zerodim_and_defer("__or__")
     77 def __or__(self, other):
---> 78     return self._logical_method(other, operator.or_)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/frame.py:7457, in DataFrame._arith_method(self, other, op)
   7453 other = ops.maybe_prepare_scalar_for_op(other, (self.shape[axis],))
   7455 self, other = ops.align_method_FRAME(self, other, axis, flex=True, level=None)
-> 7457 new_data = self._dispatch_frame_op(other, op, axis=axis)
   7458 return self._construct_result(new_data)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/frame.py:7496, in DataFrame._dispatch_frame_op(self, right, func, axis)
   7490     # TODO: The previous assertion `assert right._indexed_same(self)`
   7491     #  fails in cases with empty columns reached via
   7492     #  _frame_arith_method_with_reindex
   7493 
   7494     # TODO operate_blockwise expects a manager of the same type
   7495     with np.errstate(all="ignore"):
-> 7496         bm = self._mgr.operate_blockwise(
   7497             # error: Argument 1 to "operate_blockwise" of "ArrayManager" has
   7498             # incompatible type "Union[ArrayManager, BlockManager]"; expected
   7499             # "ArrayManager"
   7500             # error: Argument 1 to "operate_blockwise" of "BlockManager" has
   7501             # incompatible type "Union[ArrayManager, BlockManager]"; expected
   7502             # "BlockManager"
   7503             right._mgr,  # type: ignore[arg-type]
   7504             array_op,
   7505         )
   7506     return self._constructor(bm)
   7508 elif isinstance(right, Series) and axis == 1:
   7509     # axis=1 means we want to operate row-by-row

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/internals/managers.py:1545, in BlockManager.operate_blockwise(self, other, array_op)
   1541 def operate_blockwise(self, other: BlockManager, array_op) -> BlockManager:
   1542     """
   1543     Apply array_op blockwise with another (aligned) BlockManager.
   1544     """
-> 1545     return operate_blockwise(self, other, array_op)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/internals/ops.py:63, in operate_blockwise(left, right, array_op)
     61 res_blks: list[Block] = []
     62 for lvals, rvals, locs, left_ea, right_ea, rblk in _iter_block_pairs(left, right):
---> 63     res_values = array_op(lvals, rvals)
     64     if left_ea and not right_ea and hasattr(res_values, "reshape"):
     65         res_values = res_values.reshape(1, -1)

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/ops/array_ops.py:401, in logical_op(left, right, op)
    397 # For int vs int `^`, `|`, `&` are bitwise operators and return
    398 #   integer dtypes.  Otherwise these are boolean ops
    399 filler = fill_int if is_self_int_dtype and is_other_int_dtype else fill_bool
--> 401 res_values = na_logical_op(lvalues, rvalues, op)
    402 # error: Cannot call function of unknown type
    403 res_values = filler(res_values)  # type: ignore[operator]

File ~/anaconda3/lib/python3.11/site-packages/pandas/core/ops/array_ops.py:318, in na_logical_op(x, y, op)
    316     x = ensure_object(x)
    317     y = ensure_object(y)
--> 318     result = libops.vec_binop(x.ravel(), y.ravel(), op)
    319 else:
    320     # let null fall thru
    321     assert lib.is_scalar(y)

File ~/anaconda3/lib/python3.11/site-packages/pandas/_libs/ops.pyx:252, in pandas._libs.ops.vec_binop()

File ~/anaconda3/lib/python3.11/site-packages/pandas/_libs/ops.pyx:245, in pandas._libs.ops.vec_binop()

TypeError: unsupported operand type(s) for |: 'float' and 'bool'

What is the code that produces this error? I don’t see it in the notebook. I see a key error at the bottom of the notebook for df.loc[['Data Scientist'],['job_title']]

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.