我不能把deC定义为一个单位.

进入后

import pint
u=pint.UnitRegistry()

当声明1*u.degC时,它输出1 degree_Celsius.看起来还可以.

当我声明2*u.degC时,它输出以下错误...

>>> 2*u.degC
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "\Lib\site-packages\pint\facets\plain\unit.py", line 147, in __mul__
    return self._REGISTRY.Quantity(1, self._units) * other
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
  File "\Lib\site-packages\pint\facets\plain\quantity.py", line 1010, in __mul__
    return self._mul_div(other, operator.mul)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "\Lib\site-packages\pint\facets\plain\quantity.py", line 103, in wrapped
    return f(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "\Lib\site-packages\pint\facets\plain\quantity.py", line 77, in wrapped
    result = f(self, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "\Lib\site-packages\pint\facets\plain\quantity.py", line 958, in _mul_div
    raise OffsetUnitCalculusError(self._units, getattr(other, "units", ""))
pint.errors.OffsetUnitCalculusError: Ambiguous operation with offset unit (degree_Celsius). See https://pint.readthedocs.io/en/stable/user/nonmult.html for guidance.

有人能告诉我为什么会是这样吗?

我确实阅读了文档,并在浏览器中进行了一些搜索. 不幸的是,在解决方向上没有任何进展.

期望值:

  1. 要在具有多个幅值含义的Python中使用deC单位定义,而不仅仅是1.
  2. 使用DECC单位别名进行计算.

推荐答案

这里的问题是,温度度数是"非乘法的".这是有道理的,你可以有一个重量,然后增加一倍,但说一个温度是另一个温度的一定比例是行不通的,除非你说的是开尔文,在开尔文,2度可能是1度的两倍(但我不是物理学家,所以对此持保留态度).

文档的这一部分说明如何定义温度并在单位之间进行换算:

>>> from pint import UnitRegistry
>>> ureg = UnitRegistry()
>>> ureg.default_format = '.3f'
>>> Q_ = ureg.Quantity
>>> home = Q_(25.4, ureg.degC)
>>> print(home.to('degF'))
77.720 degree_Fahrenheit

这将让您定义不同的温度.

在你的情况下,它看起来像是

>>> Q_(2, u.degC)
<Quantity(2, 'degree_Celsius')>

我不确定您所说的"使用DEC别名进行计算"是什么意思,但是如果您可以用一个更具体的任务来重新表述这个问题,我也很乐意提供帮助.

Python相关问答推荐

Gekko:Spring-Mass系统的参数识别

如何记录脚本输出

如何使用表达式将字符串解压缩到Polars DataFrame中的多个列中?

为什么抓取的HTML与浏览器判断的元素不同?

如何设置视频语言时上传到YouTube与Python API客户端

使用密钥字典重新配置嵌套字典密钥名

AES—256—CBC加密在Python和PHP中返回不同的结果,HELPPP

下三角形掩码与seaborn clustermap bug

从列表中获取n个元素,其中list [i][0]== value''

如何按row_id/row_number过滤数据帧

pandas fill和bfill基于另一列中的条件

如何删除重复的文字翻拍?

获取git修订版中每个文件的最后修改时间的最有效方法是什么?

提取最内层嵌套链接

Seaborn散点图使用多个不同的标记而不是点

将相应的值从第2列合并到第1列(Pandas )

大型稀疏CSR二进制矩阵乘法结果中的错误

我如何为测试函数的参数化提供fixture 生成的数据?如果我可以的话,还有其他 Select 吗?

Pandas:使列中的列表大小与另一列中的列表大小相同

Pandas查找给定时间戳之前的最后一个值