TypeError: unsupported operand type(s) for +: 'float' and 'decimal.Decimal'

本文探讨了在Python中使用Decimal类型与浮点数进行加法运算时出现的TypeError错误,提供了错误示例并讨论了解决方案。原文链接:https://www.cnblogs.com/yanxiatingyu/p/10452625.html

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TypeError: unsupported operand type(s) for +: 'float' and 'decimal.Decimal'



浮点型和双精度类型 相加报错
from decimal import Decimal
Decimal( float )+Decimal( float )
 

 

转载于:https://www.cnblogs.com/yanxiatingyu/p/10452625.html

Traceback (most recent call last): File "D:\django_pro\.venv\lib\site-packages\pandas\core\ops\array_ops.py", line 218, in _na_arithmetic_op result = func(left, right) File "D:\django_pro\.venv\lib\site-packages\pandas\core\computation\expressions.py", line 242, in evaluate return _evaluate(op, op_str, a, b) # type: ignore[misc] File "D:\django_pro\.venv\lib\site-packages\pandas\core\computation\expressions.py", line 73, in _evaluate_standard return op(a, b) TypeError: unsupported operand type(s) for -: 'decimal.Decimal' and 'float' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\django_pro\predict\FactoryLevel_gaselemodel.py", line 196, in <module> predicted_consumptions = regression(raw_data) File "D:\django_pro\predict\FactoryLevel_gaselemodel.py", line 32, in regression noise_ratio = np.var(y_train - model.predict(x_train)) / np.var(y_train) File "D:\django_pro\.venv\lib\site-packages\pandas\core\ops\common.py", line 76, in new_method return method(self, other) File "D:\django_pro\.venv\lib\site-packages\pandas\core\arraylike.py", line 194, in __sub__ return self._arith_method(other, operator.sub) File "D:\django_pro\.venv\lib\site-packages\pandas\core\frame.py", line 7913, in _arith_method new_data = self._dispatch_frame_op(other, op, axis=axis) File "D:\django_pro\.venv\lib\site-packages\pandas\core\frame.py", line 7956, in _dispatch_frame_op bm = self._mgr.operate_blockwise( File "D:\django_pro\.venv\lib\site-packages\pandas\core\internals\managers.py", line 1511, in operate_blockwise return operate_blockwise(self, other, array_op) File "D:\django_pro\.venv\lib\site-packages\pandas\core\internals\ops.py", line 65, in operate_blockwise res_values = array_op(lvals, rvals) File "D:\django_pro\.venv\lib\site-packages\pandas\core\ops\array_ops.py", line 283, in arithmetic_op res_values = _na_arithmetic_op(left, right, op) # type: ignore[arg-type] File "D:\django_pro\.venv\lib\site-packages\pandas\core\ops\array_ops.py", line 227, in _na_arithmetic_op result = _masked_arith_op(left, right, op) File "D:\django_pro\.venv\lib\site-packages\pandas\core\ops\array_ops.py", line 163, in _masked_arith_op result[mask] = op(xrav[mask], yrav[mask]) TypeError: unsupported operand type(s) for -: 'decimal.Decimal' and 'float'
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值