pandas Python / Numpy AttributeError:'float' 对象没有属性 'sin'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49971708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:30:07  来源:igfitidea点击:

Python / Numpy AttributeError: 'float' object has no attribute 'sin'

pythonpandasnumpyfloating-pointattributeerror

提问by Bill

I'm going to post this here because it's quite a chestnut and caused me some head-scratching. It's arguably a duplicate of this questionposted four years ago but I'll post it again in case someone has the specific pandas-numpy incompatibility that I have encountered here. Or maybe someone will come up with a better answer.

我要把这个贴在这里,因为它是一个栗子,让我有些头疼。这可以说是四年前发布的这个问题的副本,但我会再次发布,以防有​​人遇到我在这里遇到的特定的 pandas-numpy 不兼容问题。或者也许有人会想出更好的答案。

Code snippet:

代码片段:

#import pdb; pdb.set_trace()

# TODO: This raises AttributeError: 'float' object has no attribute 'sin'
xr = xw + L*np.sin(θr)

Output:

输出:

Traceback (most recent call last):
  File "MIP_MPC_demo.py", line 561, in <module>
    main()
  File "MIP_MPC_demo.py", line 557, in main
    animation = create_animation(model, data_recorder)
  File "MIP_MPC_demo.py", line 358, in create_animation
    xr = xw + L*np.sin(θr)
AttributeError: 'float' object has no attribute 'sin'

What I've tried so far:

到目前为止我尝试过的:

(Pdb) type(np)
<class 'module'>
(Pdb) np.sin
<ufunc 'sin'>
(Pdb) type(θr)
<class 'pandas.core.series.Series'>
(Pdb) np.sin(θr.values)
*** AttributeError: 'float' object has no attribute 'sin'
(Pdb) θr.dtype
dtype('O')
(Pdb) np.sin(θr)
*** AttributeError: 'float' object has no attribute 'sin'
(Pdb) θr.sin()
*** AttributeError: 'Series' object has no attribute 'sin'
(Pdb) θr.values.sin()
*** AttributeError: 'numpy.ndarray' object has no attribute 'sin'
(Pdb) θr.values.max()
nan
(Pdb) np.max(θr)
0.02343020407511865
(Pdb) np.sin(θr)
*** AttributeError: 'float' object has no attribute 'sin'
(Pdb) np.sin(θr[0])
0.0

On a side-note, the exception is misleading to say the least. It's been four years since the other person posted the issue. Does anyone else agree that this should be modified and any suggestions on how? What is the explanation for the exception? Is numpy doing some kind of map operation and trying to call the sinmethod of each element of θr?

附带说明一下,该例外至少可以说是误导。另一个人发布这个问题已经四年了。有没有其他人同意应该修改这点以及有关如何修改的任何建议?异常的解释是什么?numpy 是否在执行某种映射操作并尝试调用 的sin每个元素的方法θr

I will post the answer shortly...

我会尽快发布答案...

采纳答案by Eric

This fails for the same reason as:

这失败的原因与以下相同:

import numpy as np
arr = np.array([1.0, 2.0, 3.0], dtype=object)
np.sin(arr)
# AttributeError: 'float' object has no attribute 'sin'

When np.sinis called on an object array, it tries to call the sinmethod of each element.

np.sin在对象数组上调用时,它会尝试调用sin每个元素的方法。

If you know the dtype of θr.values, you can fix this with:

如果您知道 的 dtype θr.values,则可以使用以下方法解决此问题:

arr = np.array(θr.values).astype(np.float64)  # assuming the type is float64
np.sin(arr)  # ok!

回答by Bill

Quick fix if you know the possible data types of θr:

如果您知道 θr 的可能数据类型,请快速修复:

xr = xw + L*np.sin(θr.astype(float))

Better solution. Get the data type right from the beginning when creating the dataframe.

更好的解决方案。在创建数据帧时从一开始就获取数据类型。

Instead of:

代替:

self.data = pd.DataFrame(index=range(n), columns=columns)
...

data.iloc[i, :] = new_data_dict

(Which I was using)

(我正在使用的)

Use:

用:

data = pd.DataFrame(index=range(n), columns=columns, dtype=float)

or:

或者:

data = pd.DataFrame(data=np.empty((n, len(columns)), columns=columns)

or:

或者:

data = pd.DataFrame(np.full((n, len(columns), np.nan), columns=columns)