计算具有缺失值的列的sin

时间:2013-11-06 11:00:41

标签: python pandas

我有一个pandas DataFrame,其中包含2个地理位置(long_1,lat_1,long_2,lat_2)的列,我想为它们之间的距离添加一列。近似的公式可以在互联网上找到,忽略细节,它涉及罪和cos,这就是我的问题所在。

我想计算np.sin( df["long_1"] )。如果所有值都不为null,则它可以工作但是一旦我有缺失值,它就会抛出错误而不是返回NaN。我收到以下错误:

In [97]: np.sin( df["long_1"] )
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-97-d95dd98ecd44> in <module>()
----> 1 np.sin( df["long_1"] )

AttributeError: sin

知道可能是什么问题吗?或者如果我在这里错了 - 在一个具有一些缺失值的系列上计算sin / cos的最佳方法是什么(当输入值丢失时,预期的行为应该是返回NaN。)

谢谢!

1 个答案:

答案 0 :(得分:6)

看起来你的dtype是对象意味着numpy尝试在数组的每个元素上调用属性sin。将它键入float应该可以。

e.g。

df["long_1"] = df["long_1"].astype(np.float64)

np.sin(df["long_1"].astype(np.float64))

HTH, 戴夫

相关问题