类型错误:& 不支持的操作数类型:'float'和“布尔”使用 .loc 时
给定以下 DataFrame -
json_path | 报告组 | 实体/分组 | 实体 ID | 调整后的值(今天,没有Div,美元) | 调整后的 TWR(本季度,无 Div,美元) | 调整后的 TWR(年初至今,无 Div,美元) | 年化调整后TWR(自成立以来,无 Div,美元) | 调整值(无 Div,美元) | TWR 审计说明 |
---|---|---|---|---|---|---|---|---|---|
data.attributes.total.children。[0] .孩子。[0].孩子。[0] | 巴拉克家族 | 威廉和鲁珀特信托 | 9957007 | -1.44 | -1.44 | ||||
data.attributes.total.children.[0].children.[0].children.[0].children.[0] | 兵营家庭 | 现金 | - | -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 | 投资级固定收益 | - | 18469768.6 | 18469768.6 | ||||
data.attributes.total.children.[0].children.[0].children.[1].children.[1] | Barrack Family | 高收益固定收益 | - | 3668982.44 | -0.205356545 | -0.205356545 | 4.441190127 | 3668982.44 |
我有一个 def dataset_creation()
函数,它使用 .loc
剔除不符合条件的数据帧行在将数据框保存为之前的条件perf_asset_class_df
。随后,def twr_exceptions_logic()
函数应该删除 Entity/Grouping
列值 != 'Cash' 的行,并且如果调整后的 TWR(本季度,无 Div,美元),
调整后的 TWR(年初至今,无 Div,美元)或
年调整值TWR (Since Inception, No Div, USD)` is == " [empty]
功能:这里是功能,如所述 -
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
问题: 我得到一个TypeError: unsupported operand type(s) for &: 'float' and 'bool'
指向 def twr_exceptions_logic()
,特别是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'
有人对我的代码可能出现问题的地方有任何提示/技巧吗?
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)or
Annualized 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论