1 year ago

#263623

test-img

William

TypeError: unsupported operand type(s) for &: 'float' and 'bool' when using .loc

Given the following DataFrame -

json_path Reporting Group Entity/Grouping Entity ID Adjusted Value (Today, No Div, USD) Adjusted TWR (Current Quarter, No Div, USD) Adjusted TWR (YTD, No Div, USD) Annualized Adjusted TWR (Since Inception, No Div, USD) Adjusted Value (No Div, USD) TWR Audit Note
data.attributes.total.children.[0].children.[0].children.[0] Barrack Family William and Rupert Trust 9957007 -1.44 -1.44
data.attributes.total.children.[0].children.[0].children.[0].children.[0] Barrack Family Cash - -1.44 -1.44
data.attributes.total.children.[0].children.[0].children.[1] Barrack Family Gratia Holdings No. 2 LLC 8413655 55491732.66 -0.971018847 -0.971018847 11.52490309 55491732.66
data.attributes.total.children.[0].children.[0].children.[1].children.[0] Barrack Family Investment Grade Fixed Income - 18469768.6 18469768.6
data.attributes.total.children.[0].children.[0].children.[1].children.[1] Barrack Family High Yield Fixed Income - 3668982.44 -0.205356545 -0.205356545 4.441190127 3668982.44

I have a def databases_creation() function which uses .loc to strip out dataframe rows that don't meet a criteria, before saving the dataframe as perf_asset_class_df. Subsequently, the def twr_exceptions_logic() function is then supposed to strip out rows where the Entity/Grouping column value != 'Cash' and if either the Adjusted TWR (Current Quarter, No Div, USD), Adjusted TWR (YTD, No Div, USD)orAnnualized Adjusted TWR (Since Inception, No Div, USD)` is == " [empty]

The Functions: here are the functions, as described -

def databases_creation():
    df = data_cleansing()
    
#   CREATING DATAFRAME FOR PERF. BY ASSET CLASS (EX. ILLIQUID) - STANDARD REPORT PG.4
    perf_asset_class_df = df[df['json_path'].str.contains(r'(?:\.children\.\[\d+\]){4}')]

    return perf_asset_class_df

AND

def twr_exceptions_logic():
    perf_asset_class_df = databases_creation()

    m1 = perf_asset_class_df.loc[(perf_asset_class_df['Entity/Grouping']!= 'Cash')]
    m2 = perf_asset_class_df[['Adjusted TWR (Current Quarter, No Div, USD)',
                              'Adjusted TWR (YTD, No Div, USD)',
                              'Annualized Adjusted TWR (Since Inception, No Div, USD)']].eq('').any(1)
    perf_asset_class_df.loc[m1&m2]
    
    return perf_asset_class_df

Issue: I am getting a TypeError: unsupported operand type(s) for &: 'float' and 'bool' which points to def twr_exceptions_logic(), specifically the perf_asset_class_df.loc[m1&m2]

--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
~\.conda\envs\JPDevelopment\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op)
    300         #  (xint or xbool) and (yint or bool)
--> 301         result = op(x, y)
    302     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)
C:\Users\WILLIA~1.FOR\AppData\Local\Temp/ipykernel_18756/2156183286.py in <module>
      9 
     10     return reporting_group_df, unknown_df, perf_asset_class_df, perf_entity_df, perf_entity_group_df
---> 11 twr_exceptions_logic()

C:\Users\WILLIA~1.FOR\AppData\Local\Temp/ipykernel_18756/2156183286.py in twr_exceptions_logic()
      6                               'Adjusted TWR (YTD, No Div, USD)',
      7                               'Annualized Adjusted TWR (Since Inception, No Div, USD)']].eq('').any(1)
----> 8     perf_asset_class_df.loc[m1&m2]
      9 
     10     return reporting_group_df, unknown_df, perf_asset_class_df, perf_entity_df, perf_entity_group_df

~\.conda\envs\JPDevelopment\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
     68         other = item_from_zerodim(other)
     69 
---> 70         return method(self, other)
     71 
     72     return new_method

~\.conda\envs\JPDevelopment\lib\site-packages\pandas\core\arraylike.py in __and__(self, other)
     68     @unpack_zerodim_and_defer("__and__")
     69     def __and__(self, other):
---> 70         return self._logical_method(other, operator.and_)
     71 
     72     @unpack_zerodim_and_defer("__rand__")

~\.conda\envs\JPDevelopment\lib\site-packages\pandas\core\frame.py in _arith_method(self, other, op)
   6938         self, other = ops.align_method_FRAME(self, other, axis, flex=True, level=None)
   6939 
-> 6940         new_data = self._dispatch_frame_op(other, op, axis=axis)
   6941         return self._construct_result(new_data)
   6942 

~\.conda\envs\JPDevelopment\lib\site-packages\pandas\core\frame.py in _dispatch_frame_op(self, right, func, axis)
   6977             # TODO operate_blockwise expects a manager of the same type
   6978             with np.errstate(all="ignore"):
-> 6979                 bm = self._mgr.operate_blockwise(
   6980                     # error: Argument 1 to "operate_blockwise" of "ArrayManager" has
   6981                     # incompatible type "Union[ArrayManager, BlockManager]"; expected

~\.conda\envs\JPDevelopment\lib\site-packages\pandas\core\internals\managers.py in operate_blockwise(self, other, array_op)
   1407         Apply array_op blockwise with another (aligned) BlockManager.
   1408         """
-> 1409         return operate_blockwise(self, other, array_op)
   1410 
   1411     def _equal_values(self: BlockManager, other: BlockManager) -> bool:

~\.conda\envs\JPDevelopment\lib\site-packages\pandas\core\internals\ops.py 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)

~\.conda\envs\JPDevelopment\lib\site-packages\pandas\core\ops\array_ops.py in logical_op(left, right, op)
    389         filler = fill_int if is_self_int_dtype and is_other_int_dtype else fill_bool
    390 
--> 391         res_values = na_logical_op(lvalues, rvalues, op)
    392         # error: Cannot call function of unknown type
    393         res_values = filler(res_values)  # type: ignore[operator]

~\.conda\envs\JPDevelopment\lib\site-packages\pandas\core\ops\array_ops.py in na_logical_op(x, y, op)
    306             x = ensure_object(x)
    307             y = ensure_object(y)
--> 308             result = libops.vec_binop(x.ravel(), y.ravel(), op)
    309         else:
    310             # let null fall thru

~\.conda\envs\JPDevelopment\lib\site-packages\pandas\_libs\ops.pyx in pandas._libs.ops.vec_binop()

~\.conda\envs\JPDevelopment\lib\site-packages\pandas\_libs\ops.pyx in pandas._libs.ops.vec_binop()

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

Does anyone have any hints/tips on where my code might be faltering?

python

pandas

typeerror

operands

0 Answers

Your Answer

Accepted video resources