Home > Blockchain >  Pandas - TypeError: Cannot perform 'rand_' with a dtyped [bool] array and scalar of type [
Pandas - TypeError: Cannot perform 'rand_' with a dtyped [bool] array and scalar of type [

Time:10-10

I wanted to change a value of a cell with the conditions of another cell value and used this code dfT.loc[dfT.state == "CANCELLED" & (dfT.Activity != "created"), "Activity"] = "cancelled"

This is an Example Table:

ID Activity state
1 created CANCELLED
1 completed CANCELLED
2 created FINNISHED
2 completed FINISHED
3 created REJECTED
3 rejected REJECTED

and There is a Type Error like this:

TypeError                                 Traceback (most recent call last)
~\miniconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op)
    264         #  (xint or xbool) and (yint or bool)
--> 265         result = op(x, y)
    266     except TypeError:

~\miniconda3\lib\site-packages\pandas\core\ops\roperator.py in rand_(left, right)
     51 def rand_(left, right):
---> 52     return operator.and_(right, left)
     53 

TypeError: ufunc 'bitwise_and' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
~\miniconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op)
    278             try:
--> 279                 result = libops.scalar_binop(x, y, op)
    280             except (

pandas\_libs\ops.pyx in pandas._libs.ops.scalar_binop()

ValueError: Buffer dtype mismatch, expected 'Python object' but got 'bool'

The above exception was the direct cause of the following exception:

TypeError                                 Traceback (most recent call last)
<ipython-input-6-350c55a06fa7> in <module>
      4 # dfT2 = dfT1[dfT1.Activity != 'created']
      5 # df.loc[(df.state == "CANCELLED") & (df.Activity != "created"), "Activity"] = "cancelled"
----> 6 dfT.loc[dfT.state == "CANCELLED" & (dfT.Activity != "created"), "Activity"] = "cancelled"
      7 dfT

~\miniconda3\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
     63         other = item_from_zerodim(other)
     64 
---> 65         return method(self, other)
     66 
     67     return new_method

~\miniconda3\lib\site-packages\pandas\core\arraylike.py in __rand__(self, other)
     61     @unpack_zerodim_and_defer("__rand__")
     62     def __rand__(self, other):
---> 63         return self._logical_method(other, roperator.rand_)
     64 
     65     @unpack_zerodim_and_defer("__or__")

~\miniconda3\lib\site-packages\pandas\core\series.py in _logical_method(self, other, op)
   4987         rvalues = extract_array(other, extract_numpy=True)
   4988 
-> 4989         res_values = ops.logical_op(lvalues, rvalues, op)
   4990         return self._construct_result(res_values, name=res_name)
   4991 

~\miniconda3\lib\site-packages\pandas\core\ops\array_ops.py in logical_op(left, right, op)
    353         filler = fill_int if is_self_int_dtype and is_other_int_dtype else fill_bool
    354 
--> 355         res_values = na_logical_op(lvalues, rvalues, op)
    356         # error: Cannot call function of unknown type
    357         res_values = filler(res_values)  # type: ignore[operator]

~\miniconda3\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op)
    286             ) as err:
    287                 typ = type(y).__name__
--> 288                 raise TypeError(
    289                     f"Cannot perform '{op.__name__}' with a dtyped [{x.dtype}] array "
    290                     f"and scalar of type [{typ}]"

If anyone understand what's my mistake is please help. Thanks Before! -Alde

CodePudding user response:

You need to wrap your conditions inside () Use:

dfT.loc[(dfT.state == "CANCELLED") & (dfT.Activity != "created"), "Activity"] = "cancelled"
  • Related